argenta 0.1.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/__init__.py +2 -0
- argenta/app/__init__.py +0 -0
- argenta/app/entity.py +145 -0
- argenta/app/exceptions.py +39 -0
- argenta/router/__init__.py +0 -0
- argenta/router/entity.py +70 -0
- argenta/router/exceptions.py +13 -0
- argenta-0.1.0.dist-info/LICENSE +21 -0
- argenta-0.1.0.dist-info/METADATA +18 -0
- argenta-0.1.0.dist-info/RECORD +11 -0
- argenta-0.1.0.dist-info/WHEEL +4 -0
argenta/__init__.py
ADDED
argenta/app/__init__.py
ADDED
File without changes
|
argenta/app/entity.py
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
from typing import Callable
|
2
|
+
from ..router.entity import Router
|
3
|
+
from .exceptions import (InvalidRouterInstanceException,
|
4
|
+
InvalidDescriptionMessagePatternException,
|
5
|
+
OnlyOneMainRouterIsAllowedException,
|
6
|
+
MissingMainRouterException,
|
7
|
+
MissingHandlersForUnknownCommandsOnMainRouterException,
|
8
|
+
HandlerForUnknownCommandsCanOnlyBeDeclaredForMainRouterException)
|
9
|
+
|
10
|
+
|
11
|
+
class App:
|
12
|
+
def __init__(self,
|
13
|
+
prompt: str = 'Enter a command',
|
14
|
+
exit_command: str = 'q',
|
15
|
+
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',
|
20
|
+
print_func: Callable[[str], None] = print) -> None:
|
21
|
+
self.prompt = prompt
|
22
|
+
self.print_func = print_func
|
23
|
+
self.exit_command = exit_command
|
24
|
+
self.ignore_exit_command_register = ignore_exit_command_register
|
25
|
+
self.goodbye_message = goodbye_message
|
26
|
+
self.initial_greeting = initial_greeting
|
27
|
+
self.line_separate = line_separate
|
28
|
+
self.command_group_description_separate = command_group_description_separate
|
29
|
+
|
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}'
|
34
|
+
|
35
|
+
|
36
|
+
def start_polling(self) -> None:
|
37
|
+
self.print_func(self.initial_greeting)
|
38
|
+
self.validate_main_router()
|
39
|
+
|
40
|
+
while True:
|
41
|
+
self.print_command_group_description()
|
42
|
+
self.print_func(self.prompt)
|
43
|
+
|
44
|
+
command: str = input()
|
45
|
+
|
46
|
+
self.checking_command_for_exit_command(command)
|
47
|
+
self.print_func(self.line_separate)
|
48
|
+
|
49
|
+
is_unknown_command: bool = self.check_is_command_unknown(command)
|
50
|
+
|
51
|
+
if is_unknown_command:
|
52
|
+
continue
|
53
|
+
|
54
|
+
for router in self.routers:
|
55
|
+
router.input_command_handler(command)
|
56
|
+
self.print_func(self.line_separate)
|
57
|
+
self.print_func(self.command_group_description_separate)
|
58
|
+
|
59
|
+
|
60
|
+
def set_initial_greeting(self, greeting: str) -> None:
|
61
|
+
self.initial_greeting = greeting
|
62
|
+
|
63
|
+
|
64
|
+
def set_goodbye_message(self, message: str) -> None:
|
65
|
+
self.goodbye_message = message
|
66
|
+
|
67
|
+
|
68
|
+
def set_description_message_pattern(self, pattern: str) -> None:
|
69
|
+
try:
|
70
|
+
pattern.format(command='command',
|
71
|
+
description='description')
|
72
|
+
except KeyError:
|
73
|
+
raise InvalidDescriptionMessagePatternException(pattern)
|
74
|
+
self._description_message_pattern = pattern
|
75
|
+
|
76
|
+
|
77
|
+
def validate_main_router(self):
|
78
|
+
if not self.main_app_router:
|
79
|
+
raise MissingMainRouterException()
|
80
|
+
|
81
|
+
if not self.main_app_router.unknown_command_func:
|
82
|
+
raise MissingHandlersForUnknownCommandsOnMainRouterException()
|
83
|
+
|
84
|
+
for router in self.routers:
|
85
|
+
if router.unknown_command_func and self.main_app_router is not router:
|
86
|
+
raise HandlerForUnknownCommandsCanOnlyBeDeclaredForMainRouterException()
|
87
|
+
|
88
|
+
|
89
|
+
def checking_command_for_exit_command(self, command: str):
|
90
|
+
if command.lower() == self.exit_command.lower():
|
91
|
+
if self.ignore_exit_command_register:
|
92
|
+
self.print_func(self.goodbye_message)
|
93
|
+
exit(0)
|
94
|
+
else:
|
95
|
+
if command == self.exit_command:
|
96
|
+
self.print_func(self.goodbye_message)
|
97
|
+
exit(0)
|
98
|
+
|
99
|
+
|
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']:
|
104
|
+
if command_entity['command'].lower() == command.lower():
|
105
|
+
if router['router'].ignore_command_register:
|
106
|
+
return False
|
107
|
+
else:
|
108
|
+
if command_entity['command'] == command:
|
109
|
+
return False
|
110
|
+
self.main_app_router.unknown_command_handler(command)
|
111
|
+
self.print_func(self.line_separate)
|
112
|
+
self.print_func(self.command_group_description_separate)
|
113
|
+
return True
|
114
|
+
|
115
|
+
|
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']:
|
120
|
+
self.print_func(self._description_message_pattern.format(
|
121
|
+
command=command_entity['command'],
|
122
|
+
description=command_entity['description']
|
123
|
+
)
|
124
|
+
)
|
125
|
+
self.print_func(self.command_group_description_separate)
|
126
|
+
|
127
|
+
|
128
|
+
def include_router(self, router: Router, is_main: bool = False) -> None:
|
129
|
+
if not isinstance(router, Router):
|
130
|
+
raise InvalidRouterInstanceException()
|
131
|
+
|
132
|
+
if is_main:
|
133
|
+
if not self.main_app_router:
|
134
|
+
self.main_app_router = router
|
135
|
+
router.set_router_as_main()
|
136
|
+
else:
|
137
|
+
raise OnlyOneMainRouterIsAllowedException(router)
|
138
|
+
|
139
|
+
self.routers.append(router)
|
140
|
+
|
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})
|
145
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class InvalidRouterInstanceException(Exception):
|
2
|
+
def __str__(self):
|
3
|
+
return "Invalid Router Instance"
|
4
|
+
|
5
|
+
|
6
|
+
class InvalidDescriptionMessagePatternException(Exception):
|
7
|
+
def __init__(self, pattern: str):
|
8
|
+
self.pattern = pattern
|
9
|
+
def __str__(self):
|
10
|
+
return ("Invalid Description Message Pattern\n"
|
11
|
+
"Correct pattern example: [{command}] *=*=* {description}\n"
|
12
|
+
"The pattern must contain two variables: `command` and `description` - description of the command\n"
|
13
|
+
f"Your pattern: {self.pattern}")
|
14
|
+
|
15
|
+
|
16
|
+
class OnlyOneMainRouterIsAllowedException(Exception):
|
17
|
+
def __init__(self, existing_main_router):
|
18
|
+
self.existing_main_router = existing_main_router
|
19
|
+
|
20
|
+
def __str__(self):
|
21
|
+
return ("Only One Main Router Allowed\n"
|
22
|
+
f"Existing main router is: {self.existing_main_router}")
|
23
|
+
|
24
|
+
|
25
|
+
class MissingMainRouterException(Exception):
|
26
|
+
def __str__(self):
|
27
|
+
return ("Missing Main Router\n"
|
28
|
+
"One of the registered routers must be the main one")
|
29
|
+
|
30
|
+
|
31
|
+
class MissingHandlersForUnknownCommandsOnMainRouterException(Exception):
|
32
|
+
def __str__(self):
|
33
|
+
return ("Missing Handlers For Unknown Commands On The Main Router\n"
|
34
|
+
"The main router must have a declared handler for unknown commands")
|
35
|
+
|
36
|
+
|
37
|
+
class HandlerForUnknownCommandsCanOnlyBeDeclaredForMainRouterException(Exception):
|
38
|
+
def __str__(self):
|
39
|
+
return '\nThe handler for unknown commands can only be declared for the main router'
|
File without changes
|
argenta/router/entity.py
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
from typing import Callable, Any
|
2
|
+
from src.core.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: bool = 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 = 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
|
+
def unknown_command_handler(self, unknown_command):
|
57
|
+
self.unknown_command_func(unknown_command)
|
58
|
+
|
59
|
+
|
60
|
+
def set_router_as_main(self):
|
61
|
+
self._is_main_router = True
|
62
|
+
|
63
|
+
|
64
|
+
def get_registered_commands(self) -> list[dict[str, Callable[[], None] | str]]:
|
65
|
+
return self.processed_commands
|
66
|
+
|
67
|
+
|
68
|
+
def get_name(self) -> str:
|
69
|
+
return self._name
|
70
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class InvalidCommandInstanceException(Exception):
|
2
|
+
def __str__(self):
|
3
|
+
return "Invalid Command Instance"
|
4
|
+
|
5
|
+
|
6
|
+
class InvalidDescriptionInstanceException(Exception):
|
7
|
+
def __str__(self):
|
8
|
+
return "Invalid Description Instance"
|
9
|
+
|
10
|
+
|
11
|
+
class UnknownCommandHandlerHasAlreadyBeenCreatedException(Exception):
|
12
|
+
def __str__(self):
|
13
|
+
return "Only one unknown command handler can be declared"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 kolo
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
@@ -0,0 +1,18 @@
|
|
1
|
+
Metadata-Version: 2.3
|
2
|
+
Name: argenta
|
3
|
+
Version: 0.1.0
|
4
|
+
Summary: python library for creating cli apps
|
5
|
+
License: MIT
|
6
|
+
Author: kolo
|
7
|
+
Author-email: kolo.is.main@gmail.com
|
8
|
+
Requires-Python: >=3.11
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
14
|
+
Description-Content-Type: text/markdown
|
15
|
+
|
16
|
+
# Argenta
|
17
|
+
python library for creating cli apps
|
18
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
argenta/__init__.py,sha256=FebXtkVKUBK3d0JGA3Lov1mpjcOjDeT-vmcV93iaoYg,41
|
2
|
+
argenta/app/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
|
+
argenta/app/entity.py,sha256=IYUeVGIYQS5J8vQ4gUz3TfXIxOUpDpTMjdxMd30AODU,5869
|
4
|
+
argenta/app/exceptions.py,sha256=IW_o0hfAbsj4aeWifzT7tL3BL-6tBCqm0R9MwMXTIRE,1551
|
5
|
+
argenta/router/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
6
|
+
argenta/router/entity.py,sha256=t3bkIW0pJPov6lQim388y0DdrekYWRE6VtXj4RQrtyk,2562
|
7
|
+
argenta/router/exceptions.py,sha256=iNZIM84oTdiowF-r4RRlfxkN4pjZuGlg8k3vouOZCT0,414
|
8
|
+
argenta-0.1.0.dist-info/LICENSE,sha256=zmqoGh2n5rReBv4s8wPxF_gZEZDgauJYSPMuPczgOiU,1082
|
9
|
+
argenta-0.1.0.dist-info/METADATA,sha256=hu5dbaIOJV6Krp9AYQ4jFnpGeAdxxk1R5K_jPfI6N_8,525
|
10
|
+
argenta-0.1.0.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
|
11
|
+
argenta-0.1.0.dist-info/RECORD,,
|