-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcli_memory_process_handler.py
71 lines (62 loc) · 2.36 KB
/
cli_memory_process_handler.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
""" This module contains the ProcessHandler for Cli that uses Memory repository
"""
from typing import Dict
from src.app.cli_memory.interfaces.cli_memory_controller_interface \
import CliMemoryControllerInterface
from src.app.cli_memory.controllers.exit_controller \
import ExitController
from src.app.cli_memory.controllers.create_profession_controller \
import CreateProfessionController
from src.interactor.errors.error_classes import FieldValueNotPermittedException
from src.interactor.interfaces.logger.logger import LoggerInterface
from src.infra.loggers.logger_default import LoggerDefault
class CliMemoryProcessHandler:
""" The ProcessHandler for Cli that uses Memory repository
"""
def __init__(self, logger: LoggerInterface) -> None:
self.logger = logger
self.options: Dict = {}
def add_option(
self,
option: str,
controller: CliMemoryControllerInterface
) -> None:
""" Add an option to the ProcessHandler
:param option: The option
:param controller: The controller for the option
:return: None
"""
self.options[option] = controller
def show_options(self):
""" Print the options to the ProcessHandler
:return: None
"""
for option, controller in self.options.items():
print(f"{option}: {controller.__class__.__name__}")
def execute(self) -> None:
""" Execute the ProcessHandler
:return: None
"""
while True:
print("Please choose an option:")
self.show_options()
choice = input("> ")
option = self.options.get(choice)
if option:
try:
option.execute()
except (
ValueError,
FieldValueNotPermittedException
) as exception:
print(f'\nERROR: {str(exception)}\n')
self.logger.log_exception(str(exception))
else:
print("Invalid choice.")
self.logger.log_info(f"Invalid user choice: {option}")
if __name__ == "__main__":
logger_default = LoggerDefault()
process = CliMemoryProcessHandler(logger_default)
process.add_option("0", ExitController())
process.add_option("1", CreateProfessionController(logger_default))
process.execute()