argenta 0.1.2__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:
@@ -13,56 +16,62 @@ class App:
13
16
  prompt: str = 'Enter a command',
14
17
  exit_command: str = 'q',
15
18
  ignore_exit_command_register: bool = True,
16
- initial_greeting: str = 'Hello',
17
- goodbye_message: str = 'GoodBye',
18
- line_separate: str = '\n',
19
- command_group_description_separate: str = '\n',
19
+ initial_greeting: str = '\nHello, I am Argenta\n',
20
+ farewell_message: str = '\nGoodBye\n',
21
+ line_separate: str = '',
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
23
27
  self.exit_command = exit_command
24
28
  self.ignore_exit_command_register = ignore_exit_command_register
25
- self.goodbye_message = goodbye_message
29
+ self.farewell_message = farewell_message
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
- 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
33
- self._description_message_pattern = '[{command}] *=*=* {description}'
35
+ self._routers: list[Router] = []
36
+ self._registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | str]] | Router]] = []
37
+ self._app_main_router: Router | None = None
38
+ self._description_message_pattern: str = '[{command}] *=*=* {description}'
34
39
 
35
40
 
36
41
  def start_polling(self) -> None:
42
+ self._validate_number_of_routers()
43
+ self._validate_included_routers()
44
+ self._validate_main_router()
45
+ self._validate_all_router_commands()
46
+
37
47
  self.print_func(self.initial_greeting)
38
- self.validate_main_router()
39
48
 
40
49
  while True:
41
- self.print_command_group_description()
50
+ self._print_command_group_description()
42
51
  self.print_func(self.prompt)
43
52
 
44
53
  command: str = input()
45
54
 
46
- self.checking_command_for_exit_command(command)
55
+ self._checking_command_for_exit_command(command)
47
56
  self.print_func(self.line_separate)
48
57
 
49
- is_unknown_command: bool = self.check_is_command_unknown(command)
50
-
58
+ is_unknown_command: bool = self._check_is_command_unknown(command)
51
59
  if is_unknown_command:
52
60
  continue
53
61
 
54
- for router in self.routers:
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
 
59
68
 
60
69
  def set_initial_greeting(self, greeting: str) -> None:
61
- self.initial_greeting = greeting
70
+ self.initial_greeting: str = greeting
62
71
 
63
72
 
64
- def set_goodbye_message(self, message: str) -> None:
65
- self.goodbye_message = message
73
+ def set_farewell_message(self, message: str) -> None:
74
+ self.farewell_message: str = message
66
75
 
67
76
 
68
77
  def set_description_message_pattern(self, pattern: str) -> None:
@@ -71,52 +80,96 @@ class App:
71
80
  description='description')
72
81
  except KeyError:
73
82
  raise InvalidDescriptionMessagePatternException(pattern)
74
- self._description_message_pattern = pattern
83
+ self._description_message_pattern: str = pattern
84
+
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
+
75
97
 
98
+ def _validate_number_of_routers(self) -> None:
99
+ if not self._routers:
100
+ raise NoRegisteredRoutersException()
76
101
 
77
- def validate_main_router(self):
78
- if not self.main_app_router:
79
- raise MissingMainRouterException()
80
102
 
81
- if not self.main_app_router.unknown_command_func:
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
+
109
+ def _validate_main_router(self):
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
117
+
118
+ if not self._app_main_router.unknown_command_func:
82
119
  raise MissingHandlersForUnknownCommandsOnMainRouterException()
83
120
 
84
- for router in self.routers:
85
- if router.unknown_command_func and self.main_app_router is not router:
121
+ for router in self._routers:
122
+ if router.unknown_command_func and self._app_main_router is not router:
86
123
  raise HandlerForUnknownCommandsCanOnlyBeDeclaredForMainRouterException()
87
124
 
88
125
 
89
- def checking_command_for_exit_command(self, command: str):
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
+
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:
92
- self.print_func(self.goodbye_message)
145
+ self.print_func(self.farewell_message)
93
146
  exit(0)
94
147
  else:
95
148
  if command == self.exit_command:
96
- self.print_func(self.goodbye_message)
149
+ self.print_func(self.farewell_message)
97
150
  exit(0)
98
151
 
99
152
 
100
- def check_is_command_unknown(self, command: str):
101
- registered_commands = self.registered_commands
102
- for router in registered_commands:
103
- for command_entity in router['commands']:
153
+ def _check_is_command_unknown(self, command: str):
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
- 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']:
169
+ def _print_command_group_description(self):
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
 
139
- self.routers.append(router)
192
+ router.set_ignore_command_register(self.ignore_command_register)
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,43 +1,44 @@
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: bool = ignore_command_register
13
- self._name = name
13
+ self.title = title
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):
37
38
  if self.unknown_command_func is not None:
38
39
  raise UnknownCommandHandlerHasAlreadyBeenCreatedException()
39
40
 
40
- self.unknown_command_func = func
41
+ self.unknown_command_func: Callable = func
41
42
 
42
43
  def wrapper(*args, **kwargs):
43
44
  return func(*args, **kwargs)
@@ -45,26 +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']()
56
+
55
57
 
56
58
  def unknown_command_handler(self, unknown_command):
57
59
  self.unknown_command_func(unknown_command)
58
60
 
59
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
+
60
82
  def set_router_as_main(self):
83
+ if self.name == 'subordinate':
84
+ self.name = 'main'
61
85
  self._is_main_router = True
62
86
 
63
87
 
64
- def get_registered_commands(self) -> list[dict[str, Callable[[], None] | str]]:
65
- 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
66
94
 
67
95
 
68
96
  def get_name(self) -> str:
69
- return self._name
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'])
70
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.2
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=IYUeVGIYQS5J8vQ4gUz3TfXIxOUpDpTMjdxMd30AODU,5869
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=G63P6vj4qxNI9PVw7Cdc9y9gFDlkdv7zA2ydaOcabHM,2541
7
- argenta/router/exceptions.py,sha256=iNZIM84oTdiowF-r4RRlfxkN4pjZuGlg8k3vouOZCT0,414
8
- argenta-0.1.2.dist-info/LICENSE,sha256=zmqoGh2n5rReBv4s8wPxF_gZEZDgauJYSPMuPczgOiU,1082
9
- argenta-0.1.2.dist-info/METADATA,sha256=Xkp04g7UmihvCL9-qizuEZva6wpews8aPrCycWBJ8jY,525
10
- argenta-0.1.2.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
11
- argenta-0.1.2.dist-info/RECORD,,