argenta 0.1.3__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {argenta-0.1.3 → argenta-0.2.0}/PKG-INFO +1 -1
- {argenta-0.1.3 → argenta-0.2.0}/argenta/app/entity.py +81 -26
- {argenta-0.1.3 → argenta-0.2.0}/argenta/app/exceptions.py +17 -0
- argenta-0.2.0/argenta/router/entity.py +123 -0
- {argenta-0.1.3 → argenta-0.2.0}/argenta/router/exceptions.py +5 -0
- {argenta-0.1.3 → argenta-0.2.0}/pyproject.toml +1 -1
- argenta-0.1.3/argenta/router/entity.py +0 -71
- {argenta-0.1.3 → argenta-0.2.0}/LICENSE +0 -0
- {argenta-0.1.3 → argenta-0.2.0}/README.md +0 -0
- {argenta-0.1.3 → argenta-0.2.0}/argenta/__init__.py +0 -0
- {argenta-0.1.3 → argenta-0.2.0}/argenta/app/__init__.py +0 -0
- {argenta-0.1.3 → argenta-0.2.0}/argenta/router/__init__.py +0 -0
@@ -5,7 +5,10 @@ from .exceptions import (InvalidRouterInstanceException,
|
|
5
5
|
OnlyOneMainRouterIsAllowedException,
|
6
6
|
MissingMainRouterException,
|
7
7
|
MissingHandlersForUnknownCommandsOnMainRouterException,
|
8
|
-
HandlerForUnknownCommandsCanOnlyBeDeclaredForMainRouterException
|
8
|
+
HandlerForUnknownCommandsCanOnlyBeDeclaredForMainRouterException,
|
9
|
+
NoRegisteredRoutersException,
|
10
|
+
NoRegisteredHandlersException,
|
11
|
+
RepeatedCommandInDifferentRoutersException)
|
9
12
|
|
10
13
|
|
11
14
|
class App:
|
@@ -14,9 +17,10 @@ class App:
|
|
14
17
|
exit_command: str = 'q',
|
15
18
|
ignore_exit_command_register: bool = True,
|
16
19
|
initial_greeting: str = '\nHello, I am Argenta\n',
|
17
|
-
farewell_message: str = '
|
20
|
+
farewell_message: str = '\nGoodBye\n',
|
18
21
|
line_separate: str = '',
|
19
22
|
command_group_description_separate: str = '',
|
23
|
+
ignore_command_register: bool = False,
|
20
24
|
print_func: Callable[[str], None] = print) -> None:
|
21
25
|
self.prompt = prompt
|
22
26
|
self.print_func = print_func
|
@@ -26,16 +30,21 @@ class App:
|
|
26
30
|
self.initial_greeting = initial_greeting
|
27
31
|
self.line_separate = line_separate
|
28
32
|
self.command_group_description_separate = command_group_description_separate
|
33
|
+
self.ignore_command_register = ignore_command_register
|
29
34
|
|
30
35
|
self._routers: list[Router] = []
|
31
|
-
self.
|
32
|
-
self.
|
36
|
+
self._registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | str]] | Router]] = []
|
37
|
+
self._app_main_router: Router | None = None
|
33
38
|
self._description_message_pattern: str = '[{command}] *=*=* {description}'
|
34
39
|
|
35
40
|
|
36
41
|
def start_polling(self) -> None:
|
37
|
-
self.
|
42
|
+
self._validate_number_of_routers()
|
43
|
+
self._validate_included_routers()
|
38
44
|
self._validate_main_router()
|
45
|
+
self._validate_all_router_commands()
|
46
|
+
|
47
|
+
self.print_func(self.initial_greeting)
|
39
48
|
|
40
49
|
while True:
|
41
50
|
self._print_command_group_description()
|
@@ -47,12 +56,12 @@ class App:
|
|
47
56
|
self.print_func(self.line_separate)
|
48
57
|
|
49
58
|
is_unknown_command: bool = self._check_is_command_unknown(command)
|
50
|
-
|
51
59
|
if is_unknown_command:
|
52
60
|
continue
|
53
61
|
|
54
62
|
for router in self._routers:
|
55
63
|
router.input_command_handler(command)
|
64
|
+
|
56
65
|
self.print_func(self.line_separate)
|
57
66
|
self.print_func(self.command_group_description_separate)
|
58
67
|
|
@@ -74,18 +83,62 @@ class App:
|
|
74
83
|
self._description_message_pattern: str = pattern
|
75
84
|
|
76
85
|
|
86
|
+
def get_main_router(self) -> Router:
|
87
|
+
return self._app_main_router
|
88
|
+
|
89
|
+
|
90
|
+
def get_all_app_commands(self) -> list[str]:
|
91
|
+
all_commands: list[str] = []
|
92
|
+
for router in self._routers:
|
93
|
+
all_commands.extend(router.get_all_commands())
|
94
|
+
|
95
|
+
return all_commands
|
96
|
+
|
97
|
+
|
98
|
+
def _validate_number_of_routers(self) -> None:
|
99
|
+
if not self._routers:
|
100
|
+
raise NoRegisteredRoutersException()
|
101
|
+
|
102
|
+
|
103
|
+
def _validate_included_routers(self) -> None:
|
104
|
+
for router in self._routers:
|
105
|
+
if not router.get_command_entities():
|
106
|
+
raise NoRegisteredHandlersException(router.get_name())
|
107
|
+
|
108
|
+
|
77
109
|
def _validate_main_router(self):
|
78
|
-
if not self.
|
79
|
-
|
110
|
+
if not self._app_main_router:
|
111
|
+
if len(self._routers) > 1:
|
112
|
+
raise MissingMainRouterException()
|
113
|
+
else:
|
114
|
+
router = self._routers[0]
|
115
|
+
router.set_router_as_main()
|
116
|
+
self._app_main_router = router
|
80
117
|
|
81
|
-
if not self.
|
118
|
+
if not self._app_main_router.unknown_command_func:
|
82
119
|
raise MissingHandlersForUnknownCommandsOnMainRouterException()
|
83
120
|
|
84
121
|
for router in self._routers:
|
85
|
-
if router.unknown_command_func and self.
|
122
|
+
if router.unknown_command_func and self._app_main_router is not router:
|
86
123
|
raise HandlerForUnknownCommandsCanOnlyBeDeclaredForMainRouterException()
|
87
124
|
|
88
125
|
|
126
|
+
def _validate_all_router_commands(self) -> None:
|
127
|
+
for idx in range(len(self._registered_router_entities)):
|
128
|
+
current_router: Router = self._registered_router_entities[idx]['entity']
|
129
|
+
routers_without_current_router = self._registered_router_entities.copy()
|
130
|
+
routers_without_current_router.pop(idx)
|
131
|
+
|
132
|
+
current_router_all_commands: list[str] = current_router.get_all_commands()
|
133
|
+
|
134
|
+
for router_entity in routers_without_current_router:
|
135
|
+
if len(set(current_router_all_commands).intersection(set(router_entity['entity'].get_all_commands()))) > 0:
|
136
|
+
raise RepeatedCommandInDifferentRoutersException()
|
137
|
+
if self.ignore_command_register:
|
138
|
+
if len(set([x.lower() for x in current_router_all_commands]).intersection(set([x.lower() for x in router_entity['entity'].get_all_commands()]))) > 0:
|
139
|
+
raise RepeatedCommandInDifferentRoutersException()
|
140
|
+
|
141
|
+
|
89
142
|
def _checking_command_for_exit_command(self, command: str):
|
90
143
|
if command.lower() == self.exit_command.lower():
|
91
144
|
if self.ignore_exit_command_register:
|
@@ -98,25 +151,25 @@ class App:
|
|
98
151
|
|
99
152
|
|
100
153
|
def _check_is_command_unknown(self, command: str):
|
101
|
-
|
102
|
-
for
|
103
|
-
for command_entity in
|
154
|
+
registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | str]] | Router]] = self._registered_router_entities
|
155
|
+
for router_entity in registered_router_entities:
|
156
|
+
for command_entity in router_entity['commands']:
|
104
157
|
if command_entity['command'].lower() == command.lower():
|
105
|
-
if
|
158
|
+
if self.ignore_command_register:
|
106
159
|
return False
|
107
160
|
else:
|
108
161
|
if command_entity['command'] == command:
|
109
162
|
return False
|
110
|
-
self.
|
163
|
+
self._app_main_router.unknown_command_handler(command)
|
111
164
|
self.print_func(self.line_separate)
|
112
165
|
self.print_func(self.command_group_description_separate)
|
113
166
|
return True
|
114
167
|
|
115
168
|
|
116
169
|
def _print_command_group_description(self):
|
117
|
-
for
|
118
|
-
self.print_func(
|
119
|
-
for command_entity in
|
170
|
+
for router_entity in self._registered_router_entities:
|
171
|
+
self.print_func(router_entity['title'])
|
172
|
+
for command_entity in router_entity['commands']:
|
120
173
|
self.print_func(self._description_message_pattern.format(
|
121
174
|
command=command_entity['command'],
|
122
175
|
description=command_entity['description']
|
@@ -125,21 +178,23 @@ class App:
|
|
125
178
|
self.print_func(self.command_group_description_separate)
|
126
179
|
|
127
180
|
|
128
|
-
def include_router(self, router: Router, is_main:
|
181
|
+
def include_router(self, router: Router, is_main: True | False = False) -> None:
|
129
182
|
if not isinstance(router, Router):
|
130
183
|
raise InvalidRouterInstanceException()
|
131
184
|
|
132
185
|
if is_main:
|
133
|
-
if not self.
|
134
|
-
self.
|
186
|
+
if not self._app_main_router:
|
187
|
+
self._app_main_router = router
|
135
188
|
router.set_router_as_main()
|
136
189
|
else:
|
137
|
-
raise OnlyOneMainRouterIsAllowedException(
|
190
|
+
raise OnlyOneMainRouterIsAllowedException(self._app_main_router.get_name())
|
138
191
|
|
192
|
+
router.set_ignore_command_register(self.ignore_command_register)
|
139
193
|
self._routers.append(router)
|
140
194
|
|
141
|
-
|
142
|
-
self.
|
143
|
-
|
144
|
-
|
195
|
+
command_entities: list[dict[str, Callable[[], None] | str]] = router.get_command_entities()
|
196
|
+
self._registered_router_entities.append({'name': router.get_name(),
|
197
|
+
'title': router.get_title(),
|
198
|
+
'entity': router,
|
199
|
+
'commands': command_entities})
|
145
200
|
|
@@ -37,3 +37,20 @@ class MissingHandlersForUnknownCommandsOnMainRouterException(Exception):
|
|
37
37
|
class HandlerForUnknownCommandsCanOnlyBeDeclaredForMainRouterException(Exception):
|
38
38
|
def __str__(self):
|
39
39
|
return '\nThe handler for unknown commands can only be declared for the main router'
|
40
|
+
|
41
|
+
|
42
|
+
class NoRegisteredRoutersException(Exception):
|
43
|
+
def __str__(self):
|
44
|
+
return "No Registered Router Found"
|
45
|
+
|
46
|
+
|
47
|
+
class NoRegisteredHandlersException(Exception):
|
48
|
+
def __init__(self, router_name):
|
49
|
+
self.router_name = router_name
|
50
|
+
def __str__(self):
|
51
|
+
return f"No Registered Handlers Found For '{self.router_name}'"
|
52
|
+
|
53
|
+
|
54
|
+
class RepeatedCommandInDifferentRoutersException(Exception):
|
55
|
+
def __str__(self):
|
56
|
+
return "Commands in different handlers cannot be repeated"
|
@@ -0,0 +1,123 @@
|
|
1
|
+
from typing import Callable, Any
|
2
|
+
from ..router.exceptions import (InvalidCommandInstanceException,
|
3
|
+
UnknownCommandHandlerHasAlreadyBeenCreatedException,
|
4
|
+
InvalidDescriptionInstanceException,
|
5
|
+
RepeatedCommandException)
|
6
|
+
|
7
|
+
|
8
|
+
class Router:
|
9
|
+
def __init__(self,
|
10
|
+
title: str = 'Commands group title:',
|
11
|
+
name: str = 'subordinate'):
|
12
|
+
|
13
|
+
self.title = title
|
14
|
+
self.name = name
|
15
|
+
|
16
|
+
self._command_entities: list[dict[str, Callable[[], None] | str]] = []
|
17
|
+
self.unknown_command_func: Callable[[str], None] | None = None
|
18
|
+
self._is_main_router: bool = False
|
19
|
+
self.ignore_command_register: bool = False
|
20
|
+
|
21
|
+
|
22
|
+
def command(self, command: str, description: str = None) -> Callable[[Any], Any]:
|
23
|
+
processed_description = Router._validate_description(command, description)
|
24
|
+
self._validate_command(command)
|
25
|
+
|
26
|
+
def command_decorator(func):
|
27
|
+
self._command_entities.append({'handler_func': func,
|
28
|
+
'command': command,
|
29
|
+
'description': processed_description})
|
30
|
+
def wrapper(*args, **kwargs):
|
31
|
+
return func(*args, **kwargs)
|
32
|
+
return wrapper
|
33
|
+
|
34
|
+
return command_decorator
|
35
|
+
|
36
|
+
|
37
|
+
def unknown_command(self, func):
|
38
|
+
if self.unknown_command_func is not None:
|
39
|
+
raise UnknownCommandHandlerHasAlreadyBeenCreatedException()
|
40
|
+
|
41
|
+
self.unknown_command_func: Callable = func
|
42
|
+
|
43
|
+
def wrapper(*args, **kwargs):
|
44
|
+
return func(*args, **kwargs)
|
45
|
+
return wrapper
|
46
|
+
|
47
|
+
|
48
|
+
def input_command_handler(self, input_command):
|
49
|
+
for command_entity in self._command_entities:
|
50
|
+
if input_command.lower() == command_entity['command'].lower():
|
51
|
+
if self.ignore_command_register:
|
52
|
+
return command_entity['handler_func']()
|
53
|
+
else:
|
54
|
+
if input_command == command_entity['command']:
|
55
|
+
return command_entity['handler_func']()
|
56
|
+
|
57
|
+
|
58
|
+
def unknown_command_handler(self, unknown_command):
|
59
|
+
self.unknown_command_func(unknown_command)
|
60
|
+
|
61
|
+
|
62
|
+
def _validate_command(self, command: str):
|
63
|
+
if not isinstance(command, str):
|
64
|
+
raise InvalidCommandInstanceException()
|
65
|
+
if command in self.get_all_commands():
|
66
|
+
raise RepeatedCommandException()
|
67
|
+
if self.ignore_command_register:
|
68
|
+
if command.lower() in [x.lower() for x in self.get_all_commands()]:
|
69
|
+
raise RepeatedCommandException()
|
70
|
+
|
71
|
+
|
72
|
+
@staticmethod
|
73
|
+
def _validate_description(command: str, description: str):
|
74
|
+
if not isinstance(description, str):
|
75
|
+
if description is None:
|
76
|
+
description = f'description for "{command}" command'
|
77
|
+
else:
|
78
|
+
raise InvalidDescriptionInstanceException()
|
79
|
+
return description
|
80
|
+
|
81
|
+
|
82
|
+
def set_router_as_main(self):
|
83
|
+
if self.name == 'subordinate':
|
84
|
+
self.name = 'main'
|
85
|
+
self._is_main_router = True
|
86
|
+
|
87
|
+
|
88
|
+
def set_ignore_command_register(self, ignore_command_register: bool):
|
89
|
+
self.ignore_command_register = ignore_command_register
|
90
|
+
|
91
|
+
|
92
|
+
def get_command_entities(self) -> list[dict[str, Callable[[], None] | str]]:
|
93
|
+
return self._command_entities
|
94
|
+
|
95
|
+
|
96
|
+
def get_name(self) -> str:
|
97
|
+
return self.name
|
98
|
+
|
99
|
+
|
100
|
+
def get_title(self) -> str:
|
101
|
+
return self.title
|
102
|
+
|
103
|
+
|
104
|
+
def get_router_info(self) -> dict:
|
105
|
+
return {
|
106
|
+
'title': self.title,
|
107
|
+
'name': self.name,
|
108
|
+
'ignore_command_register': self.ignore_command_register,
|
109
|
+
'attributes': {
|
110
|
+
'command_entities': self._command_entities,
|
111
|
+
'unknown_command_func': self.unknown_command_func,
|
112
|
+
'is_main_router': self._is_main_router
|
113
|
+
}
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
def get_all_commands(self) -> list[str]:
|
119
|
+
all_commands: list[str] = []
|
120
|
+
for command_entity in self._command_entities:
|
121
|
+
all_commands.append(command_entity['command'])
|
122
|
+
|
123
|
+
return all_commands
|
@@ -11,3 +11,8 @@ class InvalidDescriptionInstanceException(Exception):
|
|
11
11
|
class UnknownCommandHandlerHasAlreadyBeenCreatedException(Exception):
|
12
12
|
def __str__(self):
|
13
13
|
return "Only one unknown command handler can be declared"
|
14
|
+
|
15
|
+
|
16
|
+
class RepeatedCommandException(Exception):
|
17
|
+
def __str__(self):
|
18
|
+
return "Commands in handler cannot be repeated"
|
@@ -1,71 +0,0 @@
|
|
1
|
-
from typing import Callable, Any
|
2
|
-
from ..router.exceptions import (InvalidCommandInstanceException,
|
3
|
-
UnknownCommandHandlerHasAlreadyBeenCreatedException,
|
4
|
-
InvalidDescriptionInstanceException)
|
5
|
-
|
6
|
-
|
7
|
-
class Router:
|
8
|
-
def __init__(self,
|
9
|
-
name: str,
|
10
|
-
ignore_command_register: bool = False):
|
11
|
-
|
12
|
-
self.ignore_command_register = ignore_command_register
|
13
|
-
self.name = name
|
14
|
-
|
15
|
-
self._processed_commands: list[dict[str, Callable[[], None] | str]] = []
|
16
|
-
self.unknown_command_func: Callable[[str], None] | None = None
|
17
|
-
self._is_main_router: bool = False
|
18
|
-
|
19
|
-
|
20
|
-
def command(self, command: str, description: str) -> Callable[[Any], Any]:
|
21
|
-
if not isinstance(command, str):
|
22
|
-
raise InvalidCommandInstanceException()
|
23
|
-
if not isinstance(description, str):
|
24
|
-
raise InvalidDescriptionInstanceException()
|
25
|
-
else:
|
26
|
-
def command_decorator(func):
|
27
|
-
self._processed_commands.append({'func': func,
|
28
|
-
'command': command,
|
29
|
-
'description': description})
|
30
|
-
def wrapper(*args, **kwargs):
|
31
|
-
return func(*args, **kwargs)
|
32
|
-
return wrapper
|
33
|
-
return command_decorator
|
34
|
-
|
35
|
-
|
36
|
-
def unknown_command(self, func):
|
37
|
-
if self.unknown_command_func is not None:
|
38
|
-
raise UnknownCommandHandlerHasAlreadyBeenCreatedException()
|
39
|
-
|
40
|
-
self.unknown_command_func: Callable = func
|
41
|
-
|
42
|
-
def wrapper(*args, **kwargs):
|
43
|
-
return func(*args, **kwargs)
|
44
|
-
return wrapper
|
45
|
-
|
46
|
-
|
47
|
-
def input_command_handler(self, input_command):
|
48
|
-
for command_entity in self._processed_commands:
|
49
|
-
if input_command.lower() == command_entity['command'].lower():
|
50
|
-
if self.ignore_command_register:
|
51
|
-
return command_entity['func']()
|
52
|
-
else:
|
53
|
-
if input_command == command_entity['command']:
|
54
|
-
return command_entity['func']()
|
55
|
-
|
56
|
-
|
57
|
-
def unknown_command_handler(self, unknown_command):
|
58
|
-
self.unknown_command_func(unknown_command)
|
59
|
-
|
60
|
-
|
61
|
-
def set_router_as_main(self):
|
62
|
-
self._is_main_router = True
|
63
|
-
|
64
|
-
|
65
|
-
def get_registered_commands(self) -> list[dict[str, Callable[[], None] | str]]:
|
66
|
-
return self._processed_commands
|
67
|
-
|
68
|
-
|
69
|
-
def get_name(self) -> str:
|
70
|
-
return self.name
|
71
|
-
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|