argenta 0.1.3__py3-none-any.whl → 0.2.0__py3-none-any.whl

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/app/entity.py CHANGED
@@ -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 = 'GoodBye',
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._registered_commands: list[dict[str, str | list[dict[str, Callable[[], None] | str]] | Router]] = []
32
- self._main_app_router: Router | None = None
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.print_func(self.initial_greeting)
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._main_app_router:
79
- raise MissingMainRouterException()
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._main_app_router.unknown_command_func:
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._main_app_router is not router:
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
- registered_commands: list[dict[str, str | list[dict[str, Callable[[], None] | str]] | Router]] = self._registered_commands
102
- for router in registered_commands:
103
- for command_entity in router['commands']:
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 router['router'].ignore_command_register:
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._main_app_router.unknown_command_handler(command)
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 router in self._registered_commands:
118
- self.print_func(router['name'])
119
- for command_entity in router['commands']:
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: bool = False) -> None:
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._main_app_router:
134
- self._main_app_router = router
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(router)
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
- registered_commands: list[dict[str, Callable[[], None] | str]] = router.get_registered_commands()
142
- self._registered_commands.append({'name': router.get_name(),
143
- 'router': router,
144
- 'commands': registered_commands})
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
 
argenta/app/exceptions.py CHANGED
@@ -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"
argenta/router/entity.py CHANGED
@@ -1,36 +1,37 @@
1
1
  from typing import Callable, Any
2
2
  from ..router.exceptions import (InvalidCommandInstanceException,
3
3
  UnknownCommandHandlerHasAlreadyBeenCreatedException,
4
- InvalidDescriptionInstanceException)
4
+ InvalidDescriptionInstanceException,
5
+ RepeatedCommandException)
5
6
 
6
7
 
7
8
  class Router:
8
9
  def __init__(self,
9
- name: str,
10
- ignore_command_register: bool = False):
10
+ title: str = 'Commands group title:',
11
+ name: str = 'subordinate'):
11
12
 
12
- self.ignore_command_register = ignore_command_register
13
+ self.title = title
13
14
  self.name = name
14
15
 
15
- self._processed_commands: list[dict[str, Callable[[], None] | str]] = []
16
+ self._command_entities: list[dict[str, Callable[[], None] | str]] = []
16
17
  self.unknown_command_func: Callable[[str], None] | None = None
17
18
  self._is_main_router: bool = False
19
+ self.ignore_command_register: bool = False
18
20
 
19
21
 
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
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
34
35
 
35
36
 
36
37
  def unknown_command(self, func):
@@ -45,27 +46,78 @@ class Router:
45
46
 
46
47
 
47
48
  def input_command_handler(self, input_command):
48
- for command_entity in self._processed_commands:
49
+ for command_entity in self._command_entities:
49
50
  if input_command.lower() == command_entity['command'].lower():
50
51
  if self.ignore_command_register:
51
- return command_entity['func']()
52
+ return command_entity['handler_func']()
52
53
  else:
53
54
  if input_command == command_entity['command']:
54
- return command_entity['func']()
55
+ return command_entity['handler_func']()
55
56
 
56
57
 
57
58
  def unknown_command_handler(self, unknown_command):
58
59
  self.unknown_command_func(unknown_command)
59
60
 
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
+
61
82
  def set_router_as_main(self):
83
+ if self.name == 'subordinate':
84
+ self.name = 'main'
62
85
  self._is_main_router = True
63
86
 
64
87
 
65
- def get_registered_commands(self) -> list[dict[str, Callable[[], None] | str]]:
66
- return self._processed_commands
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
67
94
 
68
95
 
69
96
  def get_name(self) -> str:
70
97
  return self.name
71
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,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: argenta
3
- Version: 0.1.3
3
+ Version: 0.2.0
4
4
  Summary: python library for creating cli apps
5
5
  License: MIT
6
6
  Author: kolo
@@ -0,0 +1,11 @@
1
+ argenta/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ argenta/app/__init__.py,sha256=WGPNy7LzkE6vjgRIITFYuJIB96vgDMFPRLOh4SV3R3E,443
3
+ argenta/app/entity.py,sha256=5-qTMxxWRpN5AkfJDCTzaZ7yvZf1PWW_S17ShIzdr9s,8569
4
+ argenta/app/exceptions.py,sha256=krz4CEeh7GorhV9rrv3mUmjYPYK16oYN6I-LJ1-Bz4k,2058
5
+ argenta/router/__init__.py,sha256=gjneUFVbKVgwil8TbHRvCN89wT_U9CaakSbFmMC_fxk,227
6
+ argenta/router/entity.py,sha256=1cI8I2ps6ZaiDZJ8Vt2PDAsVjaWPPdSVHfZk11KAPdY,4266
7
+ argenta/router/exceptions.py,sha256=fgJEsMfdN6sjbfie8Ea9jbopF7HKQi-6vKCCRQrVlTA,543
8
+ argenta-0.2.0.dist-info/LICENSE,sha256=zmqoGh2n5rReBv4s8wPxF_gZEZDgauJYSPMuPczgOiU,1082
9
+ argenta-0.2.0.dist-info/METADATA,sha256=eTlehhqe2yXacQeaQWqPq7cDVm9y9Ag-lws6fjvkvg8,525
10
+ argenta-0.2.0.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
11
+ argenta-0.2.0.dist-info/RECORD,,
@@ -1,11 +0,0 @@
1
- argenta/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- argenta/app/__init__.py,sha256=WGPNy7LzkE6vjgRIITFYuJIB96vgDMFPRLOh4SV3R3E,443
3
- argenta/app/entity.py,sha256=TTkVvQ2TBRDGacMAbT0R9gUsr6UHYajVrZYnvAIObUU,6008
4
- argenta/app/exceptions.py,sha256=IW_o0hfAbsj4aeWifzT7tL3BL-6tBCqm0R9MwMXTIRE,1551
5
- argenta/router/__init__.py,sha256=gjneUFVbKVgwil8TbHRvCN89wT_U9CaakSbFmMC_fxk,227
6
- argenta/router/entity.py,sha256=_o3jT5RkD5XGDGOlN2LGnHQ3entopr8xVtlWuSBvayw,2549
7
- argenta/router/exceptions.py,sha256=iNZIM84oTdiowF-r4RRlfxkN4pjZuGlg8k3vouOZCT0,414
8
- argenta-0.1.3.dist-info/LICENSE,sha256=zmqoGh2n5rReBv4s8wPxF_gZEZDgauJYSPMuPczgOiU,1082
9
- argenta-0.1.3.dist-info/METADATA,sha256=Ts3DIjwtLi7uyiFQ4j9nJ96EsGsIHCBFKpGM17sJtfk,525
10
- argenta-0.1.3.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
11
- argenta-0.1.3.dist-info/RECORD,,