argenta 0.5.0b2__py3-none-any.whl → 1.0.0a1__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/autocompleter/entity.py +32 -7
- argenta/app/defaults.py +4 -1
- argenta/app/dividing_line/models.py +55 -11
- argenta/app/exceptions.py +4 -6
- argenta/app/models.py +231 -105
- argenta/app/registered_routers/entity.py +21 -2
- argenta/command/exceptions.py +12 -0
- argenta/command/flag/defaults.py +9 -6
- argenta/command/flag/models.py +84 -6
- argenta/command/models.py +59 -7
- argenta/orchestrator/__init__.py +4 -0
- argenta/orchestrator/argparser/__init__.py +4 -0
- argenta/orchestrator/argparser/arguments/__init__.py +6 -0
- argenta/orchestrator/argparser/arguments/models.py +55 -0
- argenta/orchestrator/argparser/entity.py +49 -0
- argenta/orchestrator/entity.py +36 -0
- argenta/router/__init__.py +1 -1
- argenta/router/command_handler/entity.py +63 -5
- argenta/router/defaults.py +1 -2
- argenta/router/entity.py +99 -44
- argenta/router/exceptions.py +14 -7
- argenta-1.0.0a1.dist-info/METADATA +1438 -0
- argenta-1.0.0a1.dist-info/RECORD +33 -0
- {argenta-0.5.0b2.dist-info → argenta-1.0.0a1.dist-info}/WHEEL +1 -1
- argenta/router/command_handlers/__init__.py +0 -0
- argenta/router/command_handlers/entity.py +0 -21
- argenta-0.5.0b2.dist-info/METADATA +0 -601
- argenta-0.5.0b2.dist-info/RECORD +0 -29
- {argenta-0.5.0b2.dist-info → argenta-1.0.0a1.dist-info/licenses}/LICENSE +0 -0
@@ -3,15 +3,34 @@ from argenta.router import Router
|
|
3
3
|
|
4
4
|
class RegisteredRouters:
|
5
5
|
def __init__(self, registered_routers: list[Router] = None) -> None:
|
6
|
+
"""
|
7
|
+
Private. Combines registered routers
|
8
|
+
:param registered_routers: list of the registered routers
|
9
|
+
:return: None
|
10
|
+
"""
|
6
11
|
self._registered_routers = registered_routers if registered_routers else []
|
7
12
|
|
8
13
|
def get_registered_routers(self) -> list[Router]:
|
14
|
+
"""
|
15
|
+
Private. Returns the registered routers
|
16
|
+
:return: registered routers as list[Router]
|
17
|
+
"""
|
9
18
|
return self._registered_routers
|
10
19
|
|
11
|
-
def add_registered_router(self, router: Router):
|
20
|
+
def add_registered_router(self, router: Router) -> None:
|
21
|
+
"""
|
22
|
+
Private. Adds a new registered router
|
23
|
+
:param router: registered router
|
24
|
+
:return: None
|
25
|
+
"""
|
12
26
|
self._registered_routers.append(router)
|
13
27
|
|
14
|
-
def add_registered_routers(self, *routers: Router):
|
28
|
+
def add_registered_routers(self, *routers: Router) -> None:
|
29
|
+
"""
|
30
|
+
Private. Adds new registered routers
|
31
|
+
:param routers: registered routers
|
32
|
+
:return: None
|
33
|
+
"""
|
15
34
|
self._registered_routers.extend(routers)
|
16
35
|
|
17
36
|
def __iter__(self):
|
argenta/command/exceptions.py
CHANGED
@@ -2,15 +2,24 @@ from argenta.command.flag.models import InputFlag, Flag
|
|
2
2
|
|
3
3
|
|
4
4
|
class BaseInputCommandException(Exception):
|
5
|
+
"""
|
6
|
+
Private. Base exception class for all exceptions raised when parse input command
|
7
|
+
"""
|
5
8
|
pass
|
6
9
|
|
7
10
|
|
8
11
|
class UnprocessedInputFlagException(BaseInputCommandException):
|
12
|
+
"""
|
13
|
+
Private. Raised when an unprocessed input flag is detected
|
14
|
+
"""
|
9
15
|
def __str__(self):
|
10
16
|
return "Unprocessed Input Flags"
|
11
17
|
|
12
18
|
|
13
19
|
class RepeatedInputFlagsException(BaseInputCommandException):
|
20
|
+
"""
|
21
|
+
Private. Raised when repeated input flags are detected
|
22
|
+
"""
|
14
23
|
def __init__(self, flag: Flag | InputFlag):
|
15
24
|
self.flag = flag
|
16
25
|
def __str__(self):
|
@@ -19,5 +28,8 @@ class RepeatedInputFlagsException(BaseInputCommandException):
|
|
19
28
|
|
20
29
|
|
21
30
|
class EmptyInputCommandException(BaseInputCommandException):
|
31
|
+
"""
|
32
|
+
Private. Raised when an empty input command is detected
|
33
|
+
"""
|
22
34
|
def __str__(self):
|
23
35
|
return "Input Command is empty"
|
argenta/command/flag/defaults.py
CHANGED
@@ -4,18 +4,21 @@ import re
|
|
4
4
|
|
5
5
|
|
6
6
|
@dataclass
|
7
|
-
class
|
7
|
+
class PredefinedFlags:
|
8
|
+
"""
|
9
|
+
Public. A dataclass with predefined flags and most frequently used flags for quick use
|
10
|
+
"""
|
8
11
|
HELP = Flag(name='help', possible_values=False)
|
9
|
-
SHORT_HELP = Flag(name='
|
12
|
+
SHORT_HELP = Flag(name='H', prefix='-', possible_values=False)
|
10
13
|
|
11
14
|
INFO = Flag(name='info', possible_values=False)
|
12
|
-
SHORT_INFO = Flag(name='
|
15
|
+
SHORT_INFO = Flag(name='I', prefix='-', possible_values=False)
|
13
16
|
|
14
17
|
ALL = Flag(name='all', possible_values=False)
|
15
|
-
SHORT_ALL = Flag(name='
|
18
|
+
SHORT_ALL = Flag(name='A', prefix='-', possible_values=False)
|
16
19
|
|
17
20
|
HOST = Flag(name='host', possible_values=re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$'))
|
18
|
-
SHORT_HOST = Flag(name='
|
21
|
+
SHORT_HOST = Flag(name='H', prefix='-', possible_values=re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$'))
|
19
22
|
|
20
23
|
PORT = Flag(name='port', possible_values=re.compile(r'^\d{1,5}$'))
|
21
|
-
SHORT_PORT = Flag(name='
|
24
|
+
SHORT_PORT = Flag(name='P', prefix='-', possible_values=re.compile(r'^\d{1,5}$'))
|
argenta/command/flag/models.py
CHANGED
@@ -2,20 +2,38 @@ from typing import Literal, Pattern
|
|
2
2
|
from abc import ABC, abstractmethod
|
3
3
|
|
4
4
|
|
5
|
-
class BaseFlag:
|
5
|
+
class BaseFlag(ABC):
|
6
6
|
def __init__(self, name: str,
|
7
|
-
prefix: Literal['-', '--', '---'] = '--'):
|
7
|
+
prefix: Literal['-', '--', '---'] = '--') -> None:
|
8
|
+
"""
|
9
|
+
Private. Base class for flags
|
10
|
+
:param name: the name of the flag
|
11
|
+
:param prefix: the prefix of the flag
|
12
|
+
:return: None
|
13
|
+
"""
|
8
14
|
self._name = name
|
9
15
|
self._prefix = prefix
|
10
16
|
|
11
|
-
def get_string_entity(self):
|
17
|
+
def get_string_entity(self) -> str:
|
18
|
+
"""
|
19
|
+
Public. Returns a string representation of the flag
|
20
|
+
:return: string representation of the flag as str
|
21
|
+
"""
|
12
22
|
string_entity: str = self._prefix + self._name
|
13
23
|
return string_entity
|
14
24
|
|
15
|
-
def get_name(self):
|
25
|
+
def get_name(self) -> str:
|
26
|
+
"""
|
27
|
+
Public. Returns the name of the flag
|
28
|
+
:return: the name of the flag as str
|
29
|
+
"""
|
16
30
|
return self._name
|
17
31
|
|
18
|
-
def get_prefix(self):
|
32
|
+
def get_prefix(self) -> str:
|
33
|
+
"""
|
34
|
+
Public. Returns the prefix of the flag
|
35
|
+
:return: the prefix of the flag as str
|
36
|
+
"""
|
19
37
|
return self._prefix
|
20
38
|
|
21
39
|
|
@@ -24,13 +42,29 @@ class InputFlag(BaseFlag):
|
|
24
42
|
def __init__(self, name: str,
|
25
43
|
prefix: Literal['-', '--', '---'] = '--',
|
26
44
|
value: str = None):
|
45
|
+
"""
|
46
|
+
Public. The entity of the flag of the entered command
|
47
|
+
:param name: the name of the input flag
|
48
|
+
:param prefix: the prefix of the input flag
|
49
|
+
:param value: the value of the input flag
|
50
|
+
:return: None
|
51
|
+
"""
|
27
52
|
super().__init__(name, prefix)
|
28
53
|
self._flag_value = value
|
29
54
|
|
30
55
|
def get_value(self) -> str | None:
|
56
|
+
"""
|
57
|
+
Public. Returns the value of the flag
|
58
|
+
:return: the value of the flag as str
|
59
|
+
"""
|
31
60
|
return self._flag_value
|
32
61
|
|
33
62
|
def set_value(self, value):
|
63
|
+
"""
|
64
|
+
Private. Sets the value of the flag
|
65
|
+
:param value: the fag value to set
|
66
|
+
:return: None
|
67
|
+
"""
|
34
68
|
self._flag_value = value
|
35
69
|
|
36
70
|
|
@@ -38,11 +72,23 @@ class InputFlag(BaseFlag):
|
|
38
72
|
class Flag(BaseFlag):
|
39
73
|
def __init__(self, name: str,
|
40
74
|
prefix: Literal['-', '--', '---'] = '--',
|
41
|
-
possible_values: list[str] | Pattern[str] | False = True):
|
75
|
+
possible_values: list[str] | Pattern[str] | False = True) -> None:
|
76
|
+
"""
|
77
|
+
Public. The entity of the flag being registered for subsequent processing
|
78
|
+
:param name: The name of the flag
|
79
|
+
:param prefix: The prefix of the flag
|
80
|
+
:param possible_values: The possible values of the flag, if False then the flag cannot have a value
|
81
|
+
:return: None
|
82
|
+
"""
|
42
83
|
super().__init__(name, prefix)
|
43
84
|
self.possible_values = possible_values
|
44
85
|
|
45
86
|
def validate_input_flag_value(self, input_flag_value: str | None):
|
87
|
+
"""
|
88
|
+
Private. Validates the input flag value
|
89
|
+
:param input_flag_value: The input flag value to validate
|
90
|
+
:return: whether the entered flag is valid as bool
|
91
|
+
"""
|
46
92
|
if self.possible_values is False:
|
47
93
|
if input_flag_value is None:
|
48
94
|
return True
|
@@ -69,22 +115,44 @@ class Flag(BaseFlag):
|
|
69
115
|
|
70
116
|
|
71
117
|
class BaseFlags(ABC):
|
118
|
+
"""
|
119
|
+
Private. Base class for groups of flags
|
120
|
+
"""
|
72
121
|
__slots__ = ('_flags',)
|
73
122
|
|
74
123
|
@abstractmethod
|
75
124
|
def get_flags(self):
|
125
|
+
"""
|
126
|
+
Public. Returns a list of flags
|
127
|
+
:return: list of flags
|
128
|
+
"""
|
76
129
|
pass
|
77
130
|
|
78
131
|
@abstractmethod
|
79
132
|
def add_flag(self, flag: Flag | InputFlag):
|
133
|
+
"""
|
134
|
+
Public. Adds a flag to the list of flags
|
135
|
+
:param flag: flag to add
|
136
|
+
:return: None
|
137
|
+
"""
|
80
138
|
pass
|
81
139
|
|
82
140
|
@abstractmethod
|
83
141
|
def add_flags(self, flags: list[Flag] | list[InputFlag]):
|
142
|
+
"""
|
143
|
+
Public. Adds a list of flags to the list of flags
|
144
|
+
:param flags: list of flags to add
|
145
|
+
:return: None
|
146
|
+
"""
|
84
147
|
pass
|
85
148
|
|
86
149
|
@abstractmethod
|
87
150
|
def get_flag(self, name: str):
|
151
|
+
"""
|
152
|
+
Public. Returns the flag entity by its name or None if not found
|
153
|
+
:param name: the name of the flag to get
|
154
|
+
:return: entity of the flag or None
|
155
|
+
"""
|
88
156
|
pass
|
89
157
|
|
90
158
|
def __iter__(self):
|
@@ -100,6 +168,11 @@ class BaseFlags(ABC):
|
|
100
168
|
|
101
169
|
class Flags(BaseFlags, ABC):
|
102
170
|
def __init__(self, *flags: Flag):
|
171
|
+
"""
|
172
|
+
Public. A model that combines the registered flags
|
173
|
+
:param flags: the flags that will be registered
|
174
|
+
:return: None
|
175
|
+
"""
|
103
176
|
self._flags = flags if flags else []
|
104
177
|
|
105
178
|
def get_flags(self) -> list[Flag]:
|
@@ -121,6 +194,11 @@ class Flags(BaseFlags, ABC):
|
|
121
194
|
|
122
195
|
class InputFlags(BaseFlags, ABC):
|
123
196
|
def __init__(self, *flags: InputFlag):
|
197
|
+
"""
|
198
|
+
Public. A model that combines the input flags of the input command
|
199
|
+
:param flags: all input flags
|
200
|
+
:return: None
|
201
|
+
"""
|
124
202
|
self._flags = flags if flags else []
|
125
203
|
|
126
204
|
def get_flags(self) -> list[InputFlag]:
|
argenta/command/models.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
from argenta.command.flag.models import Flag, InputFlag, Flags, InputFlags
|
2
2
|
from argenta.command.exceptions import (UnprocessedInputFlagException,
|
3
|
-
|
4
|
-
|
3
|
+
RepeatedInputFlagsException,
|
4
|
+
EmptyInputCommandException)
|
5
5
|
from typing import Generic, TypeVar, cast, Literal
|
6
6
|
|
7
7
|
|
@@ -9,10 +9,18 @@ InputCommandType = TypeVar('InputCommandType')
|
|
9
9
|
|
10
10
|
|
11
11
|
class BaseCommand:
|
12
|
-
def __init__(self, trigger: str):
|
12
|
+
def __init__(self, trigger: str) -> None:
|
13
|
+
"""
|
14
|
+
Private. Base class for all commands
|
15
|
+
:param trigger: A string trigger, which, when entered by the user, indicates that the input corresponds to the command
|
16
|
+
"""
|
13
17
|
self._trigger = trigger
|
14
18
|
|
15
19
|
def get_trigger(self) -> str:
|
20
|
+
"""
|
21
|
+
Public. Returns the trigger of the command
|
22
|
+
:return: the trigger of the command as str
|
23
|
+
"""
|
16
24
|
return self._trigger
|
17
25
|
|
18
26
|
|
@@ -21,18 +29,38 @@ class Command(BaseCommand):
|
|
21
29
|
description: str = None,
|
22
30
|
flags: Flag | Flags = None,
|
23
31
|
aliases: list[str] = None):
|
32
|
+
"""
|
33
|
+
Public. The command that can and should be registered in the Router
|
34
|
+
:param trigger: A string trigger, which, when entered by the user, indicates that the input corresponds to the command
|
35
|
+
:param description: the description of the command
|
36
|
+
:param flags: processed commands
|
37
|
+
:param aliases: string synonyms for the main trigger
|
38
|
+
"""
|
24
39
|
super().__init__(trigger)
|
25
40
|
self._registered_flags: Flags = flags if isinstance(flags, Flags) else Flags(flags) if isinstance(flags, Flag) else Flags()
|
26
41
|
self._description = f'Description for "{self._trigger}" command' if not description else description
|
27
|
-
self._aliases = aliases
|
42
|
+
self._aliases = aliases if isinstance(aliases, list) else []
|
28
43
|
|
29
44
|
def get_registered_flags(self) -> Flags:
|
45
|
+
"""
|
46
|
+
Private. Returns the registered flags
|
47
|
+
:return: the registered flags as Flags
|
48
|
+
"""
|
30
49
|
return self._registered_flags
|
31
50
|
|
32
|
-
def get_aliases(self) -> list[str] |
|
51
|
+
def get_aliases(self) -> list[str] | list:
|
52
|
+
"""
|
53
|
+
Public. Returns the aliases of the command
|
54
|
+
:return: the aliases of the command as list[str] | list
|
55
|
+
"""
|
33
56
|
return self._aliases
|
34
57
|
|
35
|
-
def validate_input_flag(self, flag: InputFlag):
|
58
|
+
def validate_input_flag(self, flag: InputFlag) -> bool:
|
59
|
+
"""
|
60
|
+
Private. Validates the input flag
|
61
|
+
:param flag: input flag for validation
|
62
|
+
:return: is input flag valid as bool
|
63
|
+
"""
|
36
64
|
registered_flags: Flags | None = self.get_registered_flags()
|
37
65
|
if registered_flags:
|
38
66
|
if isinstance(registered_flags, Flag):
|
@@ -49,6 +77,10 @@ class Command(BaseCommand):
|
|
49
77
|
return False
|
50
78
|
|
51
79
|
def get_description(self) -> str:
|
80
|
+
"""
|
81
|
+
Private. Returns the description of the command
|
82
|
+
:return: the description of the command as str
|
83
|
+
"""
|
52
84
|
return self._description
|
53
85
|
|
54
86
|
|
@@ -56,18 +88,38 @@ class Command(BaseCommand):
|
|
56
88
|
class InputCommand(BaseCommand, Generic[InputCommandType]):
|
57
89
|
def __init__(self, trigger: str,
|
58
90
|
input_flags: InputFlag | InputFlags = None):
|
91
|
+
"""
|
92
|
+
Private. The model of the input command, after parsing
|
93
|
+
:param trigger:the trigger of the command
|
94
|
+
:param input_flags: the input flags
|
95
|
+
:return: None
|
96
|
+
"""
|
59
97
|
super().__init__(trigger)
|
60
98
|
self._input_flags: InputFlags = input_flags if isinstance(input_flags, InputFlags) else InputFlags(input_flags) if isinstance(input_flags, InputFlag) else InputFlags()
|
61
99
|
|
62
|
-
def _set_input_flags(self, input_flags: InputFlags):
|
100
|
+
def _set_input_flags(self, input_flags: InputFlags) -> None:
|
101
|
+
"""
|
102
|
+
Private. Sets the input flags
|
103
|
+
:param input_flags: the input flags to set
|
104
|
+
:return: None
|
105
|
+
"""
|
63
106
|
self._input_flags = input_flags
|
64
107
|
|
65
108
|
def get_input_flags(self) -> InputFlags:
|
109
|
+
"""
|
110
|
+
Private. Returns the input flags
|
111
|
+
:return: the input flags as InputFlags
|
112
|
+
"""
|
66
113
|
return self._input_flags
|
67
114
|
|
68
115
|
|
69
116
|
@staticmethod
|
70
117
|
def parse(raw_command: str) -> InputCommandType:
|
118
|
+
"""
|
119
|
+
Private. Parse the raw input command
|
120
|
+
:param raw_command: raw input command
|
121
|
+
:return: model of the input command, after parsing as InputCommand
|
122
|
+
"""
|
71
123
|
if not raw_command:
|
72
124
|
raise EmptyInputCommandException()
|
73
125
|
|
@@ -0,0 +1,55 @@
|
|
1
|
+
from abc import ABC, abstractmethod
|
2
|
+
from typing import Literal
|
3
|
+
|
4
|
+
|
5
|
+
class BaseArgument(ABC):
|
6
|
+
"""
|
7
|
+
Private. Base class for all arguments
|
8
|
+
"""
|
9
|
+
@abstractmethod
|
10
|
+
def get_string_entity(self) -> str:
|
11
|
+
"""
|
12
|
+
Public. Returns the string representation of the argument
|
13
|
+
:return: the string representation as a str
|
14
|
+
"""
|
15
|
+
pass
|
16
|
+
|
17
|
+
|
18
|
+
class PositionalArgument(BaseArgument):
|
19
|
+
def __init__(self, name: str):
|
20
|
+
"""
|
21
|
+
Public. Required argument at startup
|
22
|
+
:param name: name of the argument, must not start with minus (-)
|
23
|
+
"""
|
24
|
+
self.name = name
|
25
|
+
|
26
|
+
def get_string_entity(self):
|
27
|
+
return self.name
|
28
|
+
|
29
|
+
|
30
|
+
class OptionalArgument(BaseArgument):
|
31
|
+
def __init__(self, name: str, prefix: Literal['-', '--', '---'] = '--'):
|
32
|
+
"""
|
33
|
+
Public. Optional argument, must have the value
|
34
|
+
:param name: name of the argument
|
35
|
+
:param prefix: prefix of the argument
|
36
|
+
"""
|
37
|
+
self.name = name
|
38
|
+
self.prefix = prefix
|
39
|
+
|
40
|
+
def get_string_entity(self):
|
41
|
+
return self.prefix + self.name
|
42
|
+
|
43
|
+
|
44
|
+
class BooleanArgument(BaseArgument):
|
45
|
+
def __init__(self, name: str, prefix: Literal['-', '--', '---'] = '--'):
|
46
|
+
"""
|
47
|
+
Public. Boolean argument, does not require a value
|
48
|
+
:param name: name of the argument
|
49
|
+
:param prefix: prefix of the argument
|
50
|
+
"""
|
51
|
+
self.name = name
|
52
|
+
self.prefix = prefix
|
53
|
+
|
54
|
+
def get_string_entity(self):
|
55
|
+
return self.prefix + self.name
|
@@ -0,0 +1,49 @@
|
|
1
|
+
from argparse import ArgumentParser
|
2
|
+
|
3
|
+
from argenta.orchestrator.argparser.arguments.models import (BooleanArgument,
|
4
|
+
OptionalArgument,
|
5
|
+
PositionalArgument)
|
6
|
+
|
7
|
+
|
8
|
+
class ArgParser:
|
9
|
+
def __init__(self,
|
10
|
+
processed_args: list[PositionalArgument | OptionalArgument | BooleanArgument],
|
11
|
+
name: str = 'Argenta',
|
12
|
+
description: str = 'Argenta available arguments',
|
13
|
+
epilog: str = 'github.com/koloideal/Argenta | made by kolo') -> None:
|
14
|
+
"""
|
15
|
+
Public. Cmd argument parser and configurator at startup
|
16
|
+
:param name: the name of the ArgParse instance
|
17
|
+
:param description: the description of the ArgParse instance
|
18
|
+
:param epilog: the epilog of the ArgParse instance
|
19
|
+
:param processed_args: registered and processed arguments
|
20
|
+
"""
|
21
|
+
self.name = name
|
22
|
+
self.description = description
|
23
|
+
self.epilog = epilog
|
24
|
+
|
25
|
+
self.entity: ArgumentParser = ArgumentParser(prog=name, description=description, epilog=epilog)
|
26
|
+
self.args: list[PositionalArgument | OptionalArgument | BooleanArgument] | None = processed_args
|
27
|
+
|
28
|
+
def set_args(self, *args: PositionalArgument | OptionalArgument | BooleanArgument) -> None:
|
29
|
+
"""
|
30
|
+
Public. Sets the arguments to be processed
|
31
|
+
:param args: processed arguments
|
32
|
+
:return: None
|
33
|
+
"""
|
34
|
+
self.args.extend(args)
|
35
|
+
|
36
|
+
def register_args(self) -> None:
|
37
|
+
"""
|
38
|
+
Private. Registers initialized command line arguments
|
39
|
+
:return: None
|
40
|
+
"""
|
41
|
+
if not self.args:
|
42
|
+
return
|
43
|
+
for arg in self.args:
|
44
|
+
if type(arg) is PositionalArgument:
|
45
|
+
self.entity.add_argument(arg.get_string_entity())
|
46
|
+
elif type(arg) is OptionalArgument:
|
47
|
+
self.entity.add_argument(arg.get_string_entity())
|
48
|
+
elif type(arg) is BooleanArgument:
|
49
|
+
self.entity.add_argument(arg.get_string_entity(), action='store_true')
|
@@ -0,0 +1,36 @@
|
|
1
|
+
from argparse import Namespace
|
2
|
+
|
3
|
+
from argenta.app import App
|
4
|
+
from argenta.orchestrator.argparser import ArgParser
|
5
|
+
|
6
|
+
|
7
|
+
class Orchestrator:
|
8
|
+
def __init__(self, arg_parser: ArgParser = False):
|
9
|
+
"""
|
10
|
+
Public. An orchestrator and configurator that defines the behavior of an integrated system, one level higher than the App
|
11
|
+
:param arg_parser: Cmd argument parser and configurator at startup
|
12
|
+
:return: None
|
13
|
+
"""
|
14
|
+
self.arg_parser: ArgParser | False = arg_parser
|
15
|
+
if arg_parser:
|
16
|
+
self.arg_parser.register_args()
|
17
|
+
|
18
|
+
@staticmethod
|
19
|
+
def start_polling(app: App) -> None:
|
20
|
+
"""
|
21
|
+
Public. Starting the user input processing cycle
|
22
|
+
:param app: a running application
|
23
|
+
:return: None
|
24
|
+
"""
|
25
|
+
app.run_polling()
|
26
|
+
|
27
|
+
def get_input_args(self) -> Namespace | None:
|
28
|
+
"""
|
29
|
+
Public. Returns the arguments parsed
|
30
|
+
:return: None
|
31
|
+
"""
|
32
|
+
if self.arg_parser:
|
33
|
+
return self.arg_parser.entity.parse_args()
|
34
|
+
else:
|
35
|
+
return None
|
36
|
+
|
argenta/router/__init__.py
CHANGED
@@ -1,21 +1,79 @@
|
|
1
1
|
from typing import Callable
|
2
|
+
|
2
3
|
from argenta.command import Command
|
3
|
-
from argenta.command.flag
|
4
|
+
from argenta.command.flag import InputFlags
|
5
|
+
|
4
6
|
|
5
7
|
|
6
8
|
class CommandHandler:
|
7
9
|
def __init__(self, handler: Callable[[], None] | Callable[[InputFlags], None], handled_command: Command):
|
10
|
+
"""
|
11
|
+
Private. Entity of the model linking the handler and the command being processed
|
12
|
+
:param handler: the handler being called
|
13
|
+
:param handled_command: the command being processed
|
14
|
+
"""
|
8
15
|
self._handler = handler
|
9
16
|
self._handled_command = handled_command
|
10
17
|
|
11
|
-
def handling(self, input_flags: InputFlags = None):
|
18
|
+
def handling(self, input_flags: InputFlags = None) -> None:
|
19
|
+
"""
|
20
|
+
Private. Direct processing of an input command
|
21
|
+
:param input_flags: the flags of the input command
|
22
|
+
:return: None
|
23
|
+
"""
|
12
24
|
if input_flags is not None:
|
13
25
|
self._handler(input_flags)
|
14
26
|
else:
|
15
27
|
self._handler()
|
16
28
|
|
17
|
-
def get_handler(self):
|
29
|
+
def get_handler(self) -> Callable[[], None] | Callable[[InputFlags], None]:
|
30
|
+
"""
|
31
|
+
Private. Returns the handler being called
|
32
|
+
:return: the handler being called as Callable[[], None] or Callable[[InputFlags], None]
|
33
|
+
"""
|
18
34
|
return self._handler
|
19
35
|
|
20
|
-
def get_handled_command(self):
|
21
|
-
|
36
|
+
def get_handled_command(self) -> Command:
|
37
|
+
"""
|
38
|
+
Private. Returns the command being processed
|
39
|
+
:return: the command being processed as Command
|
40
|
+
"""
|
41
|
+
return self._handled_command
|
42
|
+
|
43
|
+
|
44
|
+
class CommandHandlers:
|
45
|
+
def __init__(self, command_handlers: list[CommandHandler] = None):
|
46
|
+
"""
|
47
|
+
Private. The model that unites all CommandHandler of the routers
|
48
|
+
:param command_handlers: list of CommandHandlers for register
|
49
|
+
"""
|
50
|
+
self.command_handlers = command_handlers if command_handlers else []
|
51
|
+
|
52
|
+
def get_command_handlers(self) -> list[CommandHandler]:
|
53
|
+
"""
|
54
|
+
Private. Returns the list of CommandHandlers
|
55
|
+
:return: the list of CommandHandlers as list[CommandHandler]
|
56
|
+
"""
|
57
|
+
return self.command_handlers
|
58
|
+
|
59
|
+
def add_command_handler(self, command_handler: CommandHandler) -> None:
|
60
|
+
"""
|
61
|
+
Private. Adds a CommandHandler to the list of CommandHandlers
|
62
|
+
:param command_handler: CommandHandler to be added
|
63
|
+
:return: None
|
64
|
+
"""
|
65
|
+
self.command_handlers.append(command_handler)
|
66
|
+
|
67
|
+
def add_command_handlers(self, *command_handlers: CommandHandler) -> None:
|
68
|
+
"""
|
69
|
+
Private. Extend a many CommandHandler to the list of CommandHandlers
|
70
|
+
:param command_handlers: many CommandHandler to be added
|
71
|
+
:return: None
|
72
|
+
"""
|
73
|
+
self.command_handlers.extend(command_handlers)
|
74
|
+
|
75
|
+
def __iter__(self):
|
76
|
+
return iter(self.command_handlers)
|
77
|
+
|
78
|
+
def __next__(self):
|
79
|
+
return next(iter(self.command_handlers))
|
argenta/router/defaults.py
CHANGED