argenta 0.1.3__py3-none-any.whl → 0.2.1__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,40 +5,61 @@ 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:
12
15
  def __init__(self,
13
16
  prompt: str = 'Enter a command',
14
- exit_command: str = 'q',
15
- ignore_exit_command_register: bool = True,
16
17
  initial_greeting: str = '\nHello, I am Argenta\n',
17
- farewell_message: str = 'GoodBye',
18
+ farewell_message: str = '\nGoodBye\n',
19
+ exit_command: str = 'Q',
20
+ exit_command_description: str = 'Exit command',
21
+ exit_command_title: str = 'System points:',
22
+ ignore_exit_command_register: bool = True,
23
+ ignore_command_register: bool = False,
18
24
  line_separate: str = '',
19
25
  command_group_description_separate: str = '',
26
+ repeat_command_groups: bool = True,
20
27
  print_func: Callable[[str], None] = print) -> None:
21
28
  self.prompt = prompt
22
29
  self.print_func = print_func
23
30
  self.exit_command = exit_command
31
+ self.exit_command_description = exit_command_description
32
+ self.exit_command_title = exit_command_title
24
33
  self.ignore_exit_command_register = ignore_exit_command_register
25
34
  self.farewell_message = farewell_message
26
35
  self.initial_greeting = initial_greeting
27
36
  self.line_separate = line_separate
28
37
  self.command_group_description_separate = command_group_description_separate
38
+ self.ignore_command_register = ignore_command_register
39
+ self.repeat_command_groups = repeat_command_groups
29
40
 
30
41
  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
42
+ self._registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | str]] | Router]] = []
43
+ self._app_main_router: Router | None = None
33
44
  self._description_message_pattern: str = '[{command}] *=*=* {description}'
34
45
 
35
46
 
36
47
  def start_polling(self) -> None:
37
- self.print_func(self.initial_greeting)
48
+ self._validate_number_of_routers()
49
+ self._validate_included_routers()
38
50
  self._validate_main_router()
51
+ self._validate_all_router_commands()
39
52
 
40
- while True:
53
+ self.print_func(self.initial_greeting)
54
+
55
+ if not self.repeat_command_groups:
41
56
  self._print_command_group_description()
57
+ if self.repeat_command_groups:
58
+ self.print_func(self.prompt)
59
+
60
+ while True:
61
+ if self.repeat_command_groups:
62
+ self._print_command_group_description()
42
63
  self.print_func(self.prompt)
43
64
 
44
65
  command: str = input()
@@ -47,12 +68,12 @@ class App:
47
68
  self.print_func(self.line_separate)
48
69
 
49
70
  is_unknown_command: bool = self._check_is_command_unknown(command)
50
-
51
71
  if is_unknown_command:
52
72
  continue
53
73
 
54
74
  for router in self._routers:
55
75
  router.input_command_handler(command)
76
+
56
77
  self.print_func(self.line_separate)
57
78
  self.print_func(self.command_group_description_separate)
58
79
 
@@ -74,18 +95,62 @@ class App:
74
95
  self._description_message_pattern: str = pattern
75
96
 
76
97
 
98
+ def get_main_router(self) -> Router:
99
+ return self._app_main_router
100
+
101
+
102
+ def get_all_app_commands(self) -> list[str]:
103
+ all_commands: list[str] = []
104
+ for router in self._routers:
105
+ all_commands.extend(router.get_all_commands())
106
+
107
+ return all_commands
108
+
109
+
110
+ def _validate_number_of_routers(self) -> None:
111
+ if not self._routers:
112
+ raise NoRegisteredRoutersException()
113
+
114
+
115
+ def _validate_included_routers(self) -> None:
116
+ for router in self._routers:
117
+ if not router.get_command_entities():
118
+ raise NoRegisteredHandlersException(router.get_name())
119
+
120
+
77
121
  def _validate_main_router(self):
78
- if not self._main_app_router:
79
- raise MissingMainRouterException()
122
+ if not self._app_main_router:
123
+ if len(self._routers) > 1:
124
+ raise MissingMainRouterException()
125
+ else:
126
+ router = self._routers[0]
127
+ router.set_router_as_main()
128
+ self._app_main_router = router
80
129
 
81
- if not self._main_app_router.unknown_command_func:
130
+ if not self._app_main_router.unknown_command_func:
82
131
  raise MissingHandlersForUnknownCommandsOnMainRouterException()
83
132
 
84
133
  for router in self._routers:
85
- if router.unknown_command_func and self._main_app_router is not router:
134
+ if router.unknown_command_func and self._app_main_router is not router:
86
135
  raise HandlerForUnknownCommandsCanOnlyBeDeclaredForMainRouterException()
87
136
 
88
137
 
138
+ def _validate_all_router_commands(self) -> None:
139
+ for idx in range(len(self._registered_router_entities)):
140
+ current_router: Router = self._registered_router_entities[idx]['entity']
141
+ routers_without_current_router = self._registered_router_entities.copy()
142
+ routers_without_current_router.pop(idx)
143
+
144
+ current_router_all_commands: list[str] = current_router.get_all_commands()
145
+
146
+ for router_entity in routers_without_current_router:
147
+ if len(set(current_router_all_commands).intersection(set(router_entity['entity'].get_all_commands()))) > 0:
148
+ raise RepeatedCommandInDifferentRoutersException()
149
+ if self.ignore_command_register:
150
+ 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:
151
+ raise RepeatedCommandInDifferentRoutersException()
152
+
153
+
89
154
  def _checking_command_for_exit_command(self, command: str):
90
155
  if command.lower() == self.exit_command.lower():
91
156
  if self.ignore_exit_command_register:
@@ -98,25 +163,25 @@ class App:
98
163
 
99
164
 
100
165
  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']:
166
+ registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | str]] | Router]] = self._registered_router_entities
167
+ for router_entity in registered_router_entities:
168
+ for command_entity in router_entity['commands']:
104
169
  if command_entity['command'].lower() == command.lower():
105
- if router['router'].ignore_command_register:
170
+ if self.ignore_command_register:
106
171
  return False
107
172
  else:
108
173
  if command_entity['command'] == command:
109
174
  return False
110
- self._main_app_router.unknown_command_handler(command)
175
+ self._app_main_router.unknown_command_handler(command)
111
176
  self.print_func(self.line_separate)
112
177
  self.print_func(self.command_group_description_separate)
113
178
  return True
114
179
 
115
180
 
116
181
  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']:
182
+ for router_entity in self._registered_router_entities:
183
+ self.print_func(router_entity['title'])
184
+ for command_entity in router_entity['commands']:
120
185
  self.print_func(self._description_message_pattern.format(
121
186
  command=command_entity['command'],
122
187
  description=command_entity['description']
@@ -124,22 +189,32 @@ class App:
124
189
  )
125
190
  self.print_func(self.command_group_description_separate)
126
191
 
192
+ self.print_func(self.exit_command_title)
193
+ self.print_func(self._description_message_pattern.format(
194
+ command=self.exit_command,
195
+ description=self.exit_command_description
196
+ )
197
+ )
198
+ self.print_func(self.command_group_description_separate)
199
+
127
200
 
128
- def include_router(self, router: Router, is_main: bool = False) -> None:
201
+ def include_router(self, router: Router, is_main: True | False = False) -> None:
129
202
  if not isinstance(router, Router):
130
203
  raise InvalidRouterInstanceException()
131
204
 
132
205
  if is_main:
133
- if not self._main_app_router:
134
- self._main_app_router = router
206
+ if not self._app_main_router:
207
+ self._app_main_router = router
135
208
  router.set_router_as_main()
136
209
  else:
137
- raise OnlyOneMainRouterIsAllowedException(router)
210
+ raise OnlyOneMainRouterIsAllowedException(self._app_main_router.get_name())
138
211
 
212
+ router.set_ignore_command_register(self.ignore_command_register)
139
213
  self._routers.append(router)
140
214
 
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})
215
+ command_entities: list[dict[str, Callable[[], None] | str]] = router.get_command_entities()
216
+ self._registered_router_entities.append({'name': router.get_name(),
217
+ 'title': router.get_title(),
218
+ 'entity': router,
219
+ 'commands': command_entities})
145
220
 
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.1
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=6EufZeJBctdsID5lExFvxUKw-lf8Nd8N0vW3dqRQucU,9474
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.1.dist-info/LICENSE,sha256=zmqoGh2n5rReBv4s8wPxF_gZEZDgauJYSPMuPczgOiU,1082
9
+ argenta-0.2.1.dist-info/METADATA,sha256=ShrLAkL-AeIygQ4qCqmPgb1FBYSzhx7u-U0elRNExRQ,525
10
+ argenta-0.2.1.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
11
+ argenta-0.2.1.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,,