argenta 0.4.1__py3-none-any.whl → 0.4.6__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/__init__.py +3 -1
- argenta/app/defaults.py +8 -0
- argenta/app/exceptions.py +0 -10
- argenta/app/models.py +214 -0
- argenta/app/registered_routers/__init__.py +0 -0
- argenta/app/registered_routers/entity.py +21 -0
- argenta/command/__init__.py +3 -1
- argenta/command/exceptions.py +10 -6
- argenta/command/flag/__init__.py +4 -2
- argenta/command/flag/defaults.py +12 -12
- argenta/command/flag/models.py +140 -0
- argenta/command/models.py +106 -0
- argenta/router/__init__.py +3 -0
- argenta/router/command_handler/__init__.py +0 -0
- argenta/router/command_handler/entity.py +21 -0
- argenta/router/command_handlers/__init__.py +0 -0
- argenta/router/command_handlers/entity.py +21 -0
- argenta/router/entity.py +47 -49
- argenta/router/exceptions.py +1 -6
- {argenta-0.4.1.dist-info → argenta-0.4.6.dist-info}/METADATA +19 -15
- argenta-0.4.6.dist-info/RECORD +25 -0
- argenta/app/entity.py +0 -282
- argenta/command/entity.py +0 -110
- argenta/command/flag/entity.py +0 -49
- argenta/command/flag/flags_group/__init__.py +0 -1
- argenta/command/flag/flags_group/entity.py +0 -35
- argenta-0.4.1.dist-info/RECORD +0 -20
- {argenta-0.4.1.dist-info → argenta-0.4.6.dist-info}/LICENSE +0 -0
- {argenta-0.4.1.dist-info → argenta-0.4.6.dist-info}/WHEEL +0 -0
argenta/router/entity.py
CHANGED
@@ -1,28 +1,27 @@
|
|
1
1
|
from typing import Callable, Any
|
2
2
|
from inspect import getfullargspec
|
3
3
|
|
4
|
-
from
|
5
|
-
from argenta.command.
|
6
|
-
from argenta.
|
7
|
-
from
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
4
|
+
from argenta.command import Command
|
5
|
+
from argenta.command.models import InputCommand
|
6
|
+
from argenta.router.command_handlers.entity import CommandHandlers
|
7
|
+
from argenta.router.command_handler.entity import CommandHandler
|
8
|
+
from argenta.command.flag.models import Flag, Flags, InputFlags
|
9
|
+
from argenta.router.exceptions import (RepeatedFlagNameException,
|
10
|
+
TooManyTransferredArgsException,
|
11
|
+
RequiredArgumentNotPassedException,
|
12
|
+
IncorrectNumberOfHandlerArgsException,
|
13
|
+
TriggerCannotContainSpacesException)
|
13
14
|
|
14
15
|
|
15
16
|
class Router:
|
16
17
|
def __init__(self,
|
17
18
|
title: str = 'Commands group title:',
|
18
19
|
name: str = 'Default'):
|
19
|
-
|
20
20
|
self._title = title
|
21
21
|
self._name = name
|
22
22
|
|
23
|
-
self.
|
23
|
+
self._command_handlers: CommandHandlers = CommandHandlers()
|
24
24
|
self._ignore_command_register: bool = False
|
25
|
-
|
26
25
|
self._not_valid_flag_handler: Callable[[Flag], None] = lambda flag: print(f"Undefined or incorrect input flag: {flag.get_string_entity()}{(' '+flag.get_value()) if flag.get_value() else ''}")
|
27
26
|
|
28
27
|
|
@@ -31,14 +30,15 @@ class Router:
|
|
31
30
|
|
32
31
|
def command_decorator(func):
|
33
32
|
Router._validate_func_args(command, func)
|
34
|
-
self.
|
35
|
-
|
33
|
+
self._command_handlers.add_command_handler(CommandHandler(func, command))
|
34
|
+
|
36
35
|
def wrapper(*args, **kwargs):
|
37
36
|
return func(*args, **kwargs)
|
38
37
|
return wrapper
|
39
38
|
|
40
39
|
return command_decorator
|
41
40
|
|
41
|
+
|
42
42
|
def set_invalid_input_flag_handler(self, func):
|
43
43
|
processed_args = getfullargspec(func).args
|
44
44
|
if len(processed_args) != 1:
|
@@ -47,40 +47,46 @@ class Router:
|
|
47
47
|
self._not_valid_flag_handler = func
|
48
48
|
|
49
49
|
|
50
|
-
def input_command_handler(self, input_command:
|
50
|
+
def input_command_handler(self, input_command: InputCommand):
|
51
51
|
input_command_name: str = input_command.get_trigger()
|
52
|
-
input_command_flags:
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
return command_entity['handler_func'](input_command_flags.unparse_to_dict())
|
52
|
+
input_command_flags: InputFlags = input_command.get_input_flags()
|
53
|
+
|
54
|
+
for command_handler in self._command_handlers:
|
55
|
+
handle_command = command_handler.get_handled_command()
|
56
|
+
if input_command_name.lower() == handle_command.get_trigger().lower():
|
57
|
+
if handle_command.get_registered_flags().get_flags():
|
58
|
+
if input_command_flags.get_flags():
|
59
|
+
if self._validate_input_flags(handle_command, input_command_flags):
|
60
|
+
command_handler.handling(input_command_flags)
|
61
|
+
return
|
63
62
|
else:
|
64
|
-
|
63
|
+
command_handler.handling(input_command_flags)
|
64
|
+
return
|
65
65
|
else:
|
66
|
-
if input_command_flags:
|
66
|
+
if input_command_flags.get_flags():
|
67
67
|
self._not_valid_flag_handler(input_command_flags[0])
|
68
68
|
return
|
69
69
|
else:
|
70
|
-
|
70
|
+
command_handler.handling()
|
71
|
+
return
|
71
72
|
|
72
73
|
|
73
|
-
def
|
74
|
+
def _validate_input_flags(self, handle_command: Command, input_flags: InputFlags):
|
75
|
+
for flag in input_flags:
|
76
|
+
is_valid = handle_command.validate_input_flag(flag)
|
77
|
+
if not is_valid:
|
78
|
+
self._not_valid_flag_handler(flag)
|
79
|
+
return False
|
80
|
+
return True
|
81
|
+
|
82
|
+
|
83
|
+
@staticmethod
|
84
|
+
def _validate_command(command: Command):
|
74
85
|
command_name: str = command.get_trigger()
|
75
86
|
if command_name.find(' ') != -1:
|
76
87
|
raise TriggerCannotContainSpacesException()
|
77
|
-
if command_name in self.get_all_commands():
|
78
|
-
raise RepeatedCommandException()
|
79
|
-
if self._ignore_command_register:
|
80
|
-
if command_name.lower() in [x.lower() for x in self.get_all_commands()]:
|
81
|
-
raise RepeatedCommandException()
|
82
88
|
|
83
|
-
flags:
|
89
|
+
flags: Flags = command.get_registered_flags()
|
84
90
|
if flags:
|
85
91
|
flags_name: list = [x.get_string_entity().lower() for x in flags]
|
86
92
|
if len(set(flags_name)) < len(flags_name):
|
@@ -91,12 +97,12 @@ class Router:
|
|
91
97
|
def _validate_func_args(command: Command, func: Callable):
|
92
98
|
registered_args = command.get_registered_flags()
|
93
99
|
transferred_args = getfullargspec(func).args
|
94
|
-
if registered_args and transferred_args:
|
100
|
+
if registered_args.get_flags() and transferred_args:
|
95
101
|
if len(transferred_args) != 1:
|
96
102
|
raise TooManyTransferredArgsException()
|
97
|
-
elif registered_args and not transferred_args:
|
103
|
+
elif registered_args.get_flags() and not transferred_args:
|
98
104
|
raise RequiredArgumentNotPassedException()
|
99
|
-
elif not registered_args and transferred_args:
|
105
|
+
elif not registered_args.get_flags() and transferred_args:
|
100
106
|
raise TooManyTransferredArgsException()
|
101
107
|
|
102
108
|
|
@@ -104,8 +110,8 @@ class Router:
|
|
104
110
|
self._ignore_command_register = ignore_command_register
|
105
111
|
|
106
112
|
|
107
|
-
def
|
108
|
-
return self.
|
113
|
+
def get_command_handlers(self) -> CommandHandlers:
|
114
|
+
return self._command_handlers
|
109
115
|
|
110
116
|
|
111
117
|
def get_name(self) -> str:
|
@@ -118,11 +124,3 @@ class Router:
|
|
118
124
|
|
119
125
|
def set_title(self, title: str):
|
120
126
|
self._title = title
|
121
|
-
|
122
|
-
|
123
|
-
def get_all_commands(self) -> list[str]:
|
124
|
-
all_commands: list[str] = []
|
125
|
-
for command_entity in self._command_entities:
|
126
|
-
all_commands.append(command_entity['command'].get_trigger())
|
127
|
-
|
128
|
-
return all_commands
|
argenta/router/exceptions.py
CHANGED
@@ -1,11 +1,6 @@
|
|
1
|
-
class RepeatedCommandException(Exception):
|
2
|
-
def __str__(self):
|
3
|
-
return "Commands in handler cannot be repeated"
|
4
|
-
|
5
|
-
|
6
1
|
class RepeatedFlagNameException(Exception):
|
7
2
|
def __str__(self):
|
8
|
-
return "Repeated
|
3
|
+
return "Repeated registered_flag name in register command"
|
9
4
|
|
10
5
|
|
11
6
|
class TooManyTransferredArgsException(Exception):
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.3
|
2
2
|
Name: argenta
|
3
|
-
Version: 0.4.
|
3
|
+
Version: 0.4.6
|
4
4
|
Summary: python library for creating custom shells
|
5
5
|
License: MIT
|
6
6
|
Author: kolo
|
@@ -11,6 +11,8 @@ Classifier: Programming Language :: Python :: 3
|
|
11
11
|
Classifier: Programming Language :: Python :: 3.11
|
12
12
|
Classifier: Programming Language :: Python :: 3.12
|
13
13
|
Classifier: Programming Language :: Python :: 3.13
|
14
|
+
Requires-Dist: art (>=6.4,<7.0)
|
15
|
+
Requires-Dist: rich (>=14.0.0,<15.0.0)
|
14
16
|
Description-Content-Type: text/markdown
|
15
17
|
|
16
18
|
# Argenta
|
@@ -20,6 +22,9 @@ Description-Content-Type: text/markdown
|
|
20
22
|
## Описание
|
21
23
|
**Argenta** — Python library for creating custom shells
|
22
24
|
|
25
|
+

|
26
|
+
Пример внешнего вида TUI, написанного с помощью Argenta
|
27
|
+
|
23
28
|
---
|
24
29
|
|
25
30
|
# Установка
|
@@ -71,14 +76,14 @@ if __name__ == '__main__':
|
|
71
76
|
import re
|
72
77
|
from argenta.router import Router
|
73
78
|
from argenta.command import Command
|
74
|
-
from argenta.command.flag import
|
79
|
+
from argenta.command.flag import Flags, Flag, InputFlags
|
75
80
|
|
76
81
|
router = Router()
|
77
82
|
|
78
|
-
registered_flags =
|
79
|
-
Flag(
|
80
|
-
|
81
|
-
|
83
|
+
registered_flags = Flags(
|
84
|
+
Flag(name='host',
|
85
|
+
prefix='--',
|
86
|
+
possible_values=re.compile(r'^192.168.\d{1,3}.\d{1,3}$')),
|
82
87
|
Flag('port', '--', re.compile(r'^[0-9]{1,4}$')))
|
83
88
|
|
84
89
|
|
@@ -90,10 +95,10 @@ def handler():
|
|
90
95
|
@router.command(Command(trigger="ssh",
|
91
96
|
description='connect via ssh',
|
92
97
|
flags=registered_flags))
|
93
|
-
def handler_with_flags(flags:
|
98
|
+
def handler_with_flags(flags: InputFlags):
|
94
99
|
for flag in flags:
|
95
|
-
print(f'Flag name: {flag
|
96
|
-
f'Flag value: {flag
|
100
|
+
print(f'Flag name: {flag.get_name()}\n'
|
101
|
+
f'Flag value: {flag.get_value()}')
|
97
102
|
```
|
98
103
|
|
99
104
|
---
|
@@ -314,7 +319,6 @@ Router(title: str = 'Commands group title:',
|
|
314
319
|
---
|
315
320
|
|
316
321
|
### Исключения
|
317
|
-
- `RepeatedCommandException` - Одна и та же команда зарегистрирована в одном роутере
|
318
322
|
- `RepeatedFlagNameException` - Повторяющиеся зарегистрированные флаги в команде
|
319
323
|
- `TooManyTransferredArgsException` - Слишком много зарегистрированных аргументов у обработчика команды
|
320
324
|
- `RequiredArgumentNotPassedException` - Не зарегистрирован обязательный аргумент у обработчика команды(аргумент, через который будут переданы флаги введённой команды)
|
@@ -330,14 +334,14 @@ Router(title: str = 'Commands group title:',
|
|
330
334
|
```python
|
331
335
|
Command(trigger: str,
|
332
336
|
description: str = None,
|
333
|
-
flags: Flag |
|
337
|
+
flags: Flag | Flags = None)
|
334
338
|
```
|
335
339
|
|
336
340
|
**Аргументы:**
|
337
341
|
- **name : mean**
|
338
342
|
- `trigger` (`str`): Строковый триггер
|
339
343
|
- `description` (`str`): Описание команды, которое будет выведено в консоль при запуске оболочки
|
340
|
-
- `flags` (`Flag |
|
344
|
+
- `flags` (`Flag | Flags`): Флаги, которые будут обработаны при их наличии во вводе юзера
|
341
345
|
|
342
346
|
---
|
343
347
|
|
@@ -353,7 +357,7 @@ Command(trigger: str,
|
|
353
357
|
|
354
358
|
---
|
355
359
|
|
356
|
-
#### **.get_registered_flags() -> `
|
360
|
+
#### **.get_registered_flags() -> `Flags | None`**
|
357
361
|
|
358
362
|
*method mean* **::** возвращает зарегистрированные флаги экземпляра
|
359
363
|
|
@@ -417,14 +421,14 @@ Flag(flag_name: str,
|
|
417
421
|
|
418
422
|
---
|
419
423
|
|
420
|
-
## *class* :: `
|
424
|
+
## *class* :: `Flags`
|
421
425
|
Класс, объединяющий список флагов в один объект, используется в качестве
|
422
426
|
передаваемого аргумента `flags` экземпляру класса `Command`, при регистрации
|
423
427
|
хэндлера
|
424
428
|
|
425
429
|
### Конструктор
|
426
430
|
```python
|
427
|
-
|
431
|
+
Flags(*flagы: Flag)
|
428
432
|
```
|
429
433
|
|
430
434
|
---
|
@@ -0,0 +1,25 @@
|
|
1
|
+
argenta/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
2
|
+
argenta/app/__init__.py,sha256=Slm_0b1yaXeJ78zUpo_sDSEzYuTSj3_DhRU6wU8XU9Q,46
|
3
|
+
argenta/app/defaults.py,sha256=7ej4G-4dieMlichfuQhw5XgabF15X-vAa8k02ZfkdUs,234
|
4
|
+
argenta/app/exceptions.py,sha256=uCkb1VqEIZQuVDY0ZsfDc3yCbySwLpV5CdIT7iaGYRM,928
|
5
|
+
argenta/app/models.py,sha256=fZtJQEbGZWZfyNw1gfjJ4m_lx_ZyJgsn6b9ojjZpuNE,9574
|
6
|
+
argenta/app/registered_routers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
7
|
+
argenta/app/registered_routers/entity.py,sha256=OQZyrF4eoCoDHzRJ22zZxhNEx-bOUDu7NZIFDfO-fuY,688
|
8
|
+
argenta/command/__init__.py,sha256=Yx5Zl5Diwhjs8SZrsYEBNJscTMxWkLP0nqPvAJZtu30,52
|
9
|
+
argenta/command/exceptions.py,sha256=HOgddtXLDgk9Wx6c_GnzW3bMAMU5CuUnUyxjW3cVHRo,687
|
10
|
+
argenta/command/flag/__init__.py,sha256=Ew-ZRFVY7sC_PMvavN0AEcsvdYGHkLAPOYMrReY3NC0,116
|
11
|
+
argenta/command/flag/defaults.py,sha256=ktKmDT0rSSBoFUghTlEQ6OletoFxCiD37hRzO73mUUc,875
|
12
|
+
argenta/command/flag/models.py,sha256=X1MFpAtnUX6mi1huycAtwAPXPeh9YFO-lH91TNyD3G4,3755
|
13
|
+
argenta/command/models.py,sha256=4MoO22EijeoMGmwYi88BSnBrip8fre2KpULGt8-NouY,4434
|
14
|
+
argenta/router/__init__.py,sha256=uP58EfcmtK2NuMBQaspD_Gmq3LqgDXus4rfB6hp9Uig,52
|
15
|
+
argenta/router/command_handler/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
16
|
+
argenta/router/command_handler/entity.py,sha256=8sWhP89c0FavFBITJmH9c8wNn2ipW_6-_obzjkwXueU,646
|
17
|
+
argenta/router/command_handlers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
18
|
+
argenta/router/command_handlers/entity.py,sha256=KgFKjAMUr_mOcn9xahTxMUKB6lIxXgqCuAsuc4TgP6E,747
|
19
|
+
argenta/router/defaults.py,sha256=huftOg1HMjrT_R2SHHOL4eJ5uZHspNEYBSg-mCq9xhU,126
|
20
|
+
argenta/router/entity.py,sha256=afQn5jrDBrcd5QUzeBwRPfazOO6x0yYSXcmvHYhJpDw,4997
|
21
|
+
argenta/router/exceptions.py,sha256=tdeaR8zDvnytgRYo_wQWKHt3if2brapgauIhhMIsTsA,678
|
22
|
+
argenta-0.4.6.dist-info/LICENSE,sha256=zmqoGh2n5rReBv4s8wPxF_gZEZDgauJYSPMuPczgOiU,1082
|
23
|
+
argenta-0.4.6.dist-info/METADATA,sha256=OUOooU874KmkCuynY9KXha_FNTB64q7ZCQeI9TZYcM4,18243
|
24
|
+
argenta-0.4.6.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
|
25
|
+
argenta-0.4.6.dist-info/RECORD,,
|
argenta/app/entity.py
DELETED
@@ -1,282 +0,0 @@
|
|
1
|
-
from typing import Callable
|
2
|
-
from inspect import getfullargspec
|
3
|
-
import re
|
4
|
-
|
5
|
-
from argenta.command import Command
|
6
|
-
from argenta.router import Router
|
7
|
-
from argenta.router.defaults import system_router
|
8
|
-
from argenta.command.exceptions import (UnprocessedInputFlagException,
|
9
|
-
RepeatedInputFlagsException,
|
10
|
-
EmptyInputCommandException)
|
11
|
-
from .exceptions import (InvalidRouterInstanceException,
|
12
|
-
InvalidDescriptionMessagePatternException,
|
13
|
-
NoRegisteredRoutersException,
|
14
|
-
NoRegisteredHandlersException,
|
15
|
-
RepeatedCommandInDifferentRoutersException,
|
16
|
-
IncorrectNumberOfHandlerArgsException)
|
17
|
-
|
18
|
-
|
19
|
-
class App:
|
20
|
-
def __init__(self,
|
21
|
-
prompt: str = 'Enter a command',
|
22
|
-
initial_message: str = '\nHello, I am Argenta\n',
|
23
|
-
farewell_message: str = '\nGoodBye\n',
|
24
|
-
invalid_input_flags_message: str = 'Invalid input flags',
|
25
|
-
exit_command: str = 'Q',
|
26
|
-
exit_command_description: str = 'Exit command',
|
27
|
-
system_points_title: str = 'System points:',
|
28
|
-
ignore_exit_command_register: bool = True,
|
29
|
-
ignore_command_register: bool = False,
|
30
|
-
line_separate: str = '',
|
31
|
-
command_group_description_separate: str = '',
|
32
|
-
repeat_command_groups: bool = True,
|
33
|
-
messages_on_startup: list[str] = None,
|
34
|
-
print_func: Callable[[str], None] = print) -> None:
|
35
|
-
self.prompt = prompt
|
36
|
-
self.print_func = print_func
|
37
|
-
self.exit_command = exit_command
|
38
|
-
self.exit_command_description = exit_command_description
|
39
|
-
self.system_points_title = system_points_title
|
40
|
-
self.ignore_exit_command_register = ignore_exit_command_register
|
41
|
-
self.farewell_message = farewell_message
|
42
|
-
self.initial_message = initial_message
|
43
|
-
self.invalid_input_flags_message = invalid_input_flags_message
|
44
|
-
self.line_separate = line_separate
|
45
|
-
self.command_group_description_separate = command_group_description_separate
|
46
|
-
self.ignore_command_register = ignore_command_register
|
47
|
-
self.repeat_command_groups = repeat_command_groups
|
48
|
-
self.messages_on_startup = messages_on_startup if messages_on_startup else []
|
49
|
-
|
50
|
-
self._routers: list[Router] = []
|
51
|
-
self._description_message_pattern: str = '[{command}] *=*=* {description}'
|
52
|
-
self._registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | Command]] | Router]] = []
|
53
|
-
self._invalid_input_flags_handler: Callable[[str], None] = lambda raw_command: print_func(f'Incorrect flag syntax: "{raw_command}"')
|
54
|
-
self._repeated_input_flags_handler: Callable[[str], None] = lambda raw_command: print_func(f'Repeated input flags: "{raw_command}"')
|
55
|
-
self._empty_input_command_handler: Callable[[], None] = lambda: print_func(f'Empty input command')
|
56
|
-
self._unknown_command_handler: Callable[[Command], None] = lambda command: print_func(f"Unknown command: {command.get_trigger()}")
|
57
|
-
self._exit_command_handler: Callable[[], None] = lambda: print_func(self.farewell_message)
|
58
|
-
|
59
|
-
|
60
|
-
def start_polling(self) -> None:
|
61
|
-
self._setup_system_router()
|
62
|
-
self._validate_number_of_routers()
|
63
|
-
self._validate_included_routers()
|
64
|
-
self._validate_all_router_commands()
|
65
|
-
|
66
|
-
self.print_func(self.initial_message)
|
67
|
-
|
68
|
-
for message in self.messages_on_startup:
|
69
|
-
self.print_func(message)
|
70
|
-
|
71
|
-
if not self.repeat_command_groups:
|
72
|
-
self._print_command_group_description()
|
73
|
-
self.print_func(self.prompt)
|
74
|
-
|
75
|
-
while True:
|
76
|
-
if self.repeat_command_groups:
|
77
|
-
self._print_command_group_description()
|
78
|
-
self.print_func(self.prompt)
|
79
|
-
|
80
|
-
raw_command: str = input()
|
81
|
-
|
82
|
-
try:
|
83
|
-
input_command: Command = Command.parse_input_command(raw_command=raw_command)
|
84
|
-
except UnprocessedInputFlagException:
|
85
|
-
self.print_func(self.line_separate)
|
86
|
-
self._invalid_input_flags_handler(raw_command)
|
87
|
-
self.print_func(self.line_separate)
|
88
|
-
|
89
|
-
if not self.repeat_command_groups:
|
90
|
-
self.print_func(self.prompt)
|
91
|
-
continue
|
92
|
-
|
93
|
-
except RepeatedInputFlagsException:
|
94
|
-
self.print_func(self.line_separate)
|
95
|
-
self._repeated_input_flags_handler(raw_command)
|
96
|
-
self.print_func(self.line_separate)
|
97
|
-
|
98
|
-
if not self.repeat_command_groups:
|
99
|
-
self.print_func(self.prompt)
|
100
|
-
continue
|
101
|
-
|
102
|
-
except EmptyInputCommandException:
|
103
|
-
self.print_func(self.line_separate)
|
104
|
-
self._empty_input_command_handler()
|
105
|
-
self.print_func(self.line_separate)
|
106
|
-
|
107
|
-
if not self.repeat_command_groups:
|
108
|
-
self.print_func(self.prompt)
|
109
|
-
continue
|
110
|
-
|
111
|
-
is_exit = self._is_exit_command(input_command)
|
112
|
-
if is_exit:
|
113
|
-
return
|
114
|
-
|
115
|
-
self.print_func(self.line_separate)
|
116
|
-
is_unknown_command: bool = self._check_is_command_unknown(input_command)
|
117
|
-
|
118
|
-
if is_unknown_command:
|
119
|
-
self.print_func(self.line_separate)
|
120
|
-
self.print_func(self.command_group_description_separate)
|
121
|
-
if not self.repeat_command_groups:
|
122
|
-
self.print_func(self.prompt)
|
123
|
-
continue
|
124
|
-
|
125
|
-
for router in self._routers:
|
126
|
-
router.input_command_handler(input_command)
|
127
|
-
|
128
|
-
self.print_func(self.line_separate)
|
129
|
-
self.print_func(self.command_group_description_separate)
|
130
|
-
if not self.repeat_command_groups:
|
131
|
-
self.print_func(self.prompt)
|
132
|
-
|
133
|
-
|
134
|
-
def set_initial_message(self, message: str) -> None:
|
135
|
-
self.initial_message: str = message
|
136
|
-
|
137
|
-
|
138
|
-
def set_farewell_message(self, message: str) -> None:
|
139
|
-
self.farewell_message: str = message
|
140
|
-
|
141
|
-
|
142
|
-
def set_description_message_pattern(self, pattern: str) -> None:
|
143
|
-
first_check = re.match(r'.*{command}.*', pattern)
|
144
|
-
second_check = re.match(r'.*{description}.*', pattern)
|
145
|
-
|
146
|
-
if bool(first_check) and bool(second_check):
|
147
|
-
self._description_message_pattern: str = pattern
|
148
|
-
else:
|
149
|
-
raise InvalidDescriptionMessagePatternException(pattern)
|
150
|
-
|
151
|
-
|
152
|
-
def set_invalid_input_flags_handler(self, handler: Callable[[str], None]) -> None:
|
153
|
-
args = getfullargspec(handler).args
|
154
|
-
if len(args) != 1:
|
155
|
-
raise IncorrectNumberOfHandlerArgsException()
|
156
|
-
else:
|
157
|
-
self._invalid_input_flags_handler = handler
|
158
|
-
|
159
|
-
|
160
|
-
def set_repeated_input_flags_handler(self, handler: Callable[[str], None]) -> None:
|
161
|
-
args = getfullargspec(handler).args
|
162
|
-
if len(args) != 1:
|
163
|
-
raise IncorrectNumberOfHandlerArgsException()
|
164
|
-
else:
|
165
|
-
self._repeated_input_flags_handler = handler
|
166
|
-
|
167
|
-
|
168
|
-
def set_unknown_command_handler(self, handler: Callable[[str], None]) -> None:
|
169
|
-
args = getfullargspec(handler).args
|
170
|
-
if len(args) != 1:
|
171
|
-
raise IncorrectNumberOfHandlerArgsException()
|
172
|
-
else:
|
173
|
-
self._unknown_command_handler = handler
|
174
|
-
|
175
|
-
|
176
|
-
def set_empty_command_handler(self, handler: Callable[[str], None]) -> None:
|
177
|
-
args = getfullargspec(handler).args
|
178
|
-
if len(args) != 1:
|
179
|
-
raise IncorrectNumberOfHandlerArgsException()
|
180
|
-
else:
|
181
|
-
self._empty_input_command_handler = handler
|
182
|
-
|
183
|
-
|
184
|
-
def set_exit_command_handler(self, handler: Callable[[], None]) -> None:
|
185
|
-
args = getfullargspec(handler).args
|
186
|
-
if len(args) != 0:
|
187
|
-
raise IncorrectNumberOfHandlerArgsException()
|
188
|
-
else:
|
189
|
-
self._exit_command_handler = handler
|
190
|
-
|
191
|
-
|
192
|
-
def add_message_on_startup(self, message: str) -> None:
|
193
|
-
self.messages_on_startup.append(message)
|
194
|
-
|
195
|
-
|
196
|
-
def include_router(self, router: Router) -> None:
|
197
|
-
if not isinstance(router, Router):
|
198
|
-
raise InvalidRouterInstanceException()
|
199
|
-
|
200
|
-
router.set_ignore_command_register(self.ignore_command_register)
|
201
|
-
self._routers.append(router)
|
202
|
-
|
203
|
-
command_entities: list[dict[str, Callable[[], None] | Command]] = router.get_command_entities()
|
204
|
-
self._registered_router_entities.append({'name': router.get_name(),
|
205
|
-
'title': router.get_title(),
|
206
|
-
'entity': router,
|
207
|
-
'commands': command_entities})
|
208
|
-
|
209
|
-
|
210
|
-
def _validate_number_of_routers(self) -> None:
|
211
|
-
if not self._routers:
|
212
|
-
raise NoRegisteredRoutersException()
|
213
|
-
|
214
|
-
|
215
|
-
def _validate_included_routers(self) -> None:
|
216
|
-
for router in self._routers:
|
217
|
-
if not router.get_command_entities():
|
218
|
-
raise NoRegisteredHandlersException(router.get_name())
|
219
|
-
|
220
|
-
|
221
|
-
def _validate_all_router_commands(self) -> None:
|
222
|
-
for idx in range(len(self._registered_router_entities)):
|
223
|
-
current_router: Router = self._registered_router_entities[idx]['entity']
|
224
|
-
routers_without_current_router = self._registered_router_entities.copy()
|
225
|
-
routers_without_current_router.pop(idx)
|
226
|
-
|
227
|
-
current_router_all_commands: list[str] = current_router.get_all_commands()
|
228
|
-
|
229
|
-
for router_entity in routers_without_current_router:
|
230
|
-
if len(set(current_router_all_commands).intersection(set(router_entity['entity'].get_all_commands()))) > 0:
|
231
|
-
raise RepeatedCommandInDifferentRoutersException()
|
232
|
-
if self.ignore_command_register:
|
233
|
-
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:
|
234
|
-
raise RepeatedCommandInDifferentRoutersException()
|
235
|
-
|
236
|
-
|
237
|
-
def _setup_system_router(self):
|
238
|
-
system_router.set_title(self.system_points_title)
|
239
|
-
@system_router.command(Command(self.exit_command, self.exit_command_description))
|
240
|
-
def exit_command():
|
241
|
-
self._exit_command_handler()
|
242
|
-
|
243
|
-
if system_router not in [router['entity'] for router in self._registered_router_entities]:
|
244
|
-
self.include_router(system_router)
|
245
|
-
|
246
|
-
|
247
|
-
def _is_exit_command(self, command: Command):
|
248
|
-
if command.get_trigger().lower() == self.exit_command.lower():
|
249
|
-
if self.ignore_exit_command_register:
|
250
|
-
system_router.input_command_handler(command)
|
251
|
-
return True
|
252
|
-
else:
|
253
|
-
if command.get_trigger() == self.exit_command:
|
254
|
-
system_router.input_command_handler(command)
|
255
|
-
return True
|
256
|
-
return False
|
257
|
-
|
258
|
-
|
259
|
-
def _check_is_command_unknown(self, command: Command):
|
260
|
-
registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | Command]] | Router]] = self._registered_router_entities
|
261
|
-
for router_entity in registered_router_entities:
|
262
|
-
for command_entity in router_entity['commands']:
|
263
|
-
if command_entity['command'].get_trigger().lower() == command.get_trigger().lower():
|
264
|
-
if self.ignore_command_register:
|
265
|
-
return False
|
266
|
-
else:
|
267
|
-
if command_entity['command'].get_trigger() == command.get_trigger():
|
268
|
-
return False
|
269
|
-
self._unknown_command_handler(command)
|
270
|
-
return True
|
271
|
-
|
272
|
-
|
273
|
-
def _print_command_group_description(self):
|
274
|
-
for router_entity in self._registered_router_entities:
|
275
|
-
self.print_func(router_entity['title'])
|
276
|
-
for command_entity in router_entity['commands']:
|
277
|
-
self.print_func(self._description_message_pattern.format(
|
278
|
-
command=command_entity['command'].get_trigger(),
|
279
|
-
description=command_entity['command'].get_description()
|
280
|
-
)
|
281
|
-
)
|
282
|
-
self.print_func(self.command_group_description_separate)
|