# AUTOGENERATED! DO NOT EDIT! File to edit: 03_main.ipynb (unless otherwise specified).

__all__ = ['get_direction_idx', 'run']

# Cell
from .direction import Direction
from .robot import *
from argparse import ArgumentParser
import re

# Cell
def get_direction_idx(direction, directions=['NORTH', 'EAST', 'SOUTH', 'WEST']):
    "returns the index of direction passed as input from `directions`."
    return directions.index(direction.upper())

# Cell
def run():
    "main function to run the package from command line."

    parser = ArgumentParser()
    parser.add_argument(
        '-path',
        '--cmd_file_path',
        required=True,
        help="absolute path to the `.txt` file containing commands for the ToyRobot."
    )
    args = parser.parse_args()
    robot = None

    # open text file from path passed as arg
    f = open(args.cmd_file_path, 'r')

    # read line by line and execute commands based on constraints in `problem.md`
    for line in f.readlines():

        # remove any extra whitespace and commas to get command
        args = re.findall(r'[^,\s]+', line)

        if args[0]=='PLACE' and robot is None:
            robot = ToyRobot.from_placement(int(args[1]), int(args[2]), get_direction_idx(args[3]))
        elif robot is not None and args[0] != 'PLACE':
            cmd = f"robot.{args[0].lower()}()"
            exec(cmd)