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/router/entity.py CHANGED
@@ -1,31 +1,36 @@
1
- from typing import Callable, Any
1
+ from typing import Callable
2
2
  from inspect import getfullargspec
3
-
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)
3
+ from src.argenta.command import Command
4
+ from src.argenta.command.models import InputCommand
5
+ from src.argenta.router.command_handler.entity import CommandHandlers, CommandHandler
6
+ from src.argenta.command.flag.models import Flag, Flags, InputFlags
7
+ from src.argenta.router.exceptions import (RepeatedFlagNameException,
8
+ TooManyTransferredArgsException,
9
+ RequiredArgumentNotPassedException,
10
+ TriggerContainSpacesException)
14
11
 
15
12
 
16
13
  class Router:
17
14
  def __init__(self,
18
- title: str = None,
19
- name: str = 'Default'):
15
+ title: str = None):
16
+ """
17
+ Public. Directly configures and manages handlers
18
+ :param title: the title of the router, displayed when displaying the available commands
19
+ :return: None
20
+ """
20
21
  self._title = title
21
- self._name = name
22
22
 
23
23
  self._command_handlers: CommandHandlers = CommandHandlers()
24
24
  self._ignore_command_register: bool = False
25
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 ''}")
26
26
 
27
27
 
28
- def command(self, command: Command) -> Callable[[Any], Any]:
28
+ def command(self, command: Command) -> Callable:
29
+ """
30
+ Public. Registers handler
31
+ :param command: Registered command
32
+ :return: decorated handler as Callable[[Any], Any]
33
+ """
29
34
  self._validate_command(command)
30
35
 
31
36
  def command_decorator(func):
@@ -39,28 +44,39 @@ class Router:
39
44
  return command_decorator
40
45
 
41
46
 
42
- def set_invalid_input_flag_handler(self, func):
43
- processed_args = getfullargspec(func).args
44
- if len(processed_args) != 1:
45
- raise IncorrectNumberOfHandlerArgsException()
46
- else:
47
- self._not_valid_flag_handler = func
47
+ def set_invalid_input_flag_handler(self, func: Callable[[Flag], None]) -> None:
48
+ """
49
+ Public. Registers handler for invalid input flag
50
+ :param func: registered handler
51
+ :return: None
52
+ """
53
+ self._not_valid_flag_handler = func
48
54
 
49
55
 
50
- def input_command_handler(self, input_command: InputCommand):
56
+ def finds_appropriate_handler(self, input_command: InputCommand) -> None:
57
+ """
58
+ Private. Finds the appropriate handler for given input command and passes control to it
59
+ :param input_command: input command as InputCommand
60
+ :return: None
61
+ """
51
62
  input_command_name: str = input_command.get_trigger()
52
63
  input_command_flags: InputFlags = input_command.get_input_flags()
53
64
 
54
65
  for command_handler in self._command_handlers:
55
66
  handle_command = command_handler.get_handled_command()
56
67
  if input_command_name.lower() == handle_command.get_trigger().lower():
57
- self._validate_input_command(input_command_flags, command_handler)
58
- elif handle_command.get_aliases():
59
- if input_command_name.lower() in handle_command.get_aliases():
60
- self._validate_input_command(input_command_flags, command_handler)
61
-
62
-
63
- def _validate_input_command(self, input_command_flags: InputFlags, command_handler: CommandHandler):
68
+ self.process_input_command(input_command_flags, command_handler)
69
+ if input_command_name.lower() in handle_command.get_aliases():
70
+ self.process_input_command(input_command_flags, command_handler)
71
+
72
+
73
+ def process_input_command(self, input_command_flags: InputFlags, command_handler: CommandHandler) -> None:
74
+ """
75
+ Private. Processes input command with the appropriate handler
76
+ :param input_command_flags: input command flags as InputFlags
77
+ :param command_handler: command handler for input command as CommandHandler
78
+ :return: None
79
+ """
64
80
  handle_command = command_handler.get_handled_command()
65
81
  if handle_command.get_registered_flags().get_flags():
66
82
  if input_command_flags.get_flags():
@@ -79,9 +95,15 @@ class Router:
79
95
  return
80
96
 
81
97
 
82
- def _validate_input_flags(self, handle_command: Command, input_flags: InputFlags):
98
+ def _validate_input_flags(self, handled_command: Command, input_flags: InputFlags) -> bool:
99
+ """
100
+ Private. Validates flags of input command
101
+ :param handled_command: entity of the handled command
102
+ :param input_flags:
103
+ :return: is flags of input command valid as bool
104
+ """
83
105
  for flag in input_flags:
84
- is_valid = handle_command.validate_input_flag(flag)
106
+ is_valid: bool = handled_command.validate_input_flag(flag)
85
107
  if not is_valid:
86
108
  self._not_valid_flag_handler(flag)
87
109
  return False
@@ -89,10 +111,15 @@ class Router:
89
111
 
90
112
 
91
113
  @staticmethod
92
- def _validate_command(command: Command):
114
+ def _validate_command(command: Command) -> None:
115
+ """
116
+ Private. Validates the command registered in handler
117
+ :param command: validated command
118
+ :return: None if command is valid else raise exception
119
+ """
93
120
  command_name: str = command.get_trigger()
94
121
  if command_name.find(' ') != -1:
95
- raise TriggerCannotContainSpacesException()
122
+ raise TriggerContainSpacesException()
96
123
 
97
124
  flags: Flags = command.get_registered_flags()
98
125
  if flags:
@@ -102,7 +129,13 @@ class Router:
102
129
 
103
130
 
104
131
  @staticmethod
105
- def _validate_func_args(command: Command, func: Callable):
132
+ def _validate_func_args(command: Command, func: Callable) -> None:
133
+ """
134
+ Private. Validates the arguments of the handler
135
+ :param command: registered command in handler
136
+ :param func: entity of the handler func
137
+ :return: None if func is valid else raise exception
138
+ """
106
139
  registered_args = command.get_registered_flags()
107
140
  transferred_args = getfullargspec(func).args
108
141
  if registered_args.get_flags() and transferred_args:
@@ -114,18 +147,31 @@ class Router:
114
147
  raise TooManyTransferredArgsException()
115
148
 
116
149
 
117
- def set_ignore_command_register(self, ignore_command_register: bool):
118
- self._ignore_command_register = ignore_command_register
150
+ def set_command_register_ignore(self, _: bool) -> None:
151
+ """
152
+ Private. Sets the router behavior on the input commands register
153
+ :param _: is command register ignore
154
+ :return: None
155
+ """
156
+ self._ignore_command_register = _
119
157
 
120
158
 
121
- def get_triggers(self):
159
+ def get_triggers(self) -> list[str]:
160
+ """
161
+ Public. Gets registered triggers
162
+ :return: registered in router triggers as list[str]
163
+ """
122
164
  all_triggers: list[str] = []
123
165
  for command_handler in self._command_handlers:
124
166
  all_triggers.append(command_handler.get_handled_command().get_trigger())
125
167
  return all_triggers
126
168
 
127
169
 
128
- def get_aliases(self):
170
+ def get_aliases(self) -> list[str]:
171
+ """
172
+ Public. Gets registered aliases
173
+ :return: registered in router aliases as list[str]
174
+ """
129
175
  all_aliases: list[str] = []
130
176
  for command_handler in self._command_handlers:
131
177
  if command_handler.get_handled_command().get_aliases():
@@ -134,16 +180,25 @@ class Router:
134
180
 
135
181
 
136
182
  def get_command_handlers(self) -> CommandHandlers:
183
+ """
184
+ Private. Gets registered command handlers
185
+ :return: registered command handlers as CommandHandlers
186
+ """
137
187
  return self._command_handlers
138
188
 
139
189
 
140
- def get_name(self) -> str:
141
- return self._name
142
-
143
-
144
190
  def get_title(self) -> str | None:
191
+ """
192
+ Public. Gets title of the router
193
+ :return: the title of the router as str or None
194
+ """
145
195
  return self._title
146
196
 
147
197
 
148
- def set_title(self, title: str):
198
+ def set_title(self, title: str) -> None:
199
+ """
200
+ Public. Sets the title of the router
201
+ :param title: title that will be setted
202
+ :return: None
203
+ """
149
204
  self._title = title
@@ -1,23 +1,30 @@
1
1
  class RepeatedFlagNameException(Exception):
2
+ """
3
+ Private. Raised when a repeated flag name is registered
4
+ """
2
5
  def __str__(self):
3
- return "Repeated registered_flag name in register command"
6
+ return "Repeated registered flag names in register command"
4
7
 
5
8
 
6
9
  class TooManyTransferredArgsException(Exception):
10
+ """
11
+ Private. Raised when too many arguments are passed
12
+ """
7
13
  def __str__(self):
8
14
  return "Too many transferred arguments"
9
15
 
10
16
 
11
17
  class RequiredArgumentNotPassedException(Exception):
18
+ """
19
+ Private. Raised when a required argument is not passed
20
+ """
12
21
  def __str__(self):
13
22
  return "Required argument not passed"
14
23
 
15
24
 
16
- class IncorrectNumberOfHandlerArgsException(Exception):
17
- def __str__(self):
18
- return "Handler has incorrect number of arguments"
19
-
20
-
21
- class TriggerCannotContainSpacesException(Exception):
25
+ class TriggerContainSpacesException(Exception):
26
+ """
27
+ Private. Raised when there is a space in the trigger being registered
28
+ """
22
29
  def __str__(self):
23
30
  return "Command trigger cannot contain spaces"