-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
77 lines (66 loc) · 2.71 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os, sys
import logging
import argparse
from helper import Helper
from parking_system.exceptions import InvalidParams
from parking_system.main import RunParkingSystem
logger = logging.getLogger(__name__)
class CommandExecutor:
def __init__(self):
self.command = RunParkingSystem()
def run_single_command(self, cmd):
"""Run with single command"""
inputs = cmd.split()
command = inputs[0]
input_param = inputs[1:]
input_values = Helper.fetch_input_values(command, input_param)
if not input_values:
logger.debug("Something wrong with input")
return
required_param_name = Helper.fetch_required_param_name(cmd=command)
self.command.run(command=command, **dict(zip(required_param_name, input_values)))
def run_from_file(self, file):
"""Run with filepath"""
if not os.path.exists(file):
logger.debug(f"File {file} doesn't exist")
return None
file_obj = open(file)
try:
while True:
line = next(file_obj)
if line.endswith('\n'):
line = line[:-1]
if line == '':
continue
self.run_single_command(line)
except StopIteration:
file_obj.close()
except Exception as e:
logger.debug(f"Exception occured : {e}")
def run_interactive(self):
"""Run in interactive mode in terminal"""
try:
while True:
std_in = input("Enter command for Parking system: \n")
self.run_single_command(std_in)
except KeyboardInterrupt:
sys.exit()
except Exception as e:
logger.debug(f"Something undefined occur while STD_IN \n Traceback: {e}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Run parking lot system algorithm either with file or \
interactive mode')
parser.add_argument('--filepath', metavar='path', help='Enter input filepath', required=False)
parser.add_argument('--interactive', default=False, action='store_true', help='Interactive mode in terminal',
required=False)
args = parser.parse_args()
command_executor = CommandExecutor()
if args.filepath and args.interactive:
error_msg = f'Cannot take params interactive mode and open {args.filepath} together'
raise InvalidParams(error_msg)
if args.interactive:
command_executor.run_interactive()
if args.filepath:
command_executor.run_from_file(args.filepath)
else:
logger.debug("filepath in null")