argenta 0.3.3__tar.gz → 0.3.5__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: argenta
3
- Version: 0.3.3
3
+ Version: 0.3.5
4
4
  Summary: python library for creating custom shells
5
5
  License: MIT
6
6
  Author: kolo
@@ -1,19 +1,18 @@
1
1
  from typing import Callable
2
2
  from inspect import getfullargspec
3
+ import re
3
4
 
4
5
  from ..command.entity import Command
5
6
  from ..router.entity import Router
6
7
  from ..command.exceptions import (UnprocessedInputFlagException,
7
- InvalidInputFlagsHandlerHasBeenAlreadyCreatedException,
8
- IncorrectNumberOfHandlerArgsException,
9
- UnknownCommandHandlerHasBeenAlreadyCreatedException,
10
8
  RepeatedInputFlagsException,
11
- RepeatedInputFlagsHandlerHasBeenAlreadyCreatedException)
9
+ EmptyInputCommandException)
12
10
  from .exceptions import (InvalidRouterInstanceException,
13
11
  InvalidDescriptionMessagePatternException,
14
12
  NoRegisteredRoutersException,
15
13
  NoRegisteredHandlersException,
16
- RepeatedCommandInDifferentRoutersException)
14
+ RepeatedCommandInDifferentRoutersException,
15
+ IncorrectNumberOfHandlerArgsException)
17
16
 
18
17
 
19
18
  class App:
@@ -46,12 +45,12 @@ class App:
46
45
  self.repeat_command_groups = repeat_command_groups
47
46
 
48
47
  self._routers: list[Router] = []
49
- self._invalid_input_flags_handler: Callable[[str], None] | None = None
50
- self._repeated_input_flags_handler: Callable[[str], None] | None = None
51
- self._unknown_command_handler: Callable[[Command], None] | None = None
52
- self._registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | Command]] | Router]] = []
53
- self._app_main_router: Router | None = None
54
48
  self._description_message_pattern: str = '[{command}] *=*=* {description}'
49
+ self._registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | Command]] | Router]] = []
50
+ self._invalid_input_flags_handler: Callable[[str], None] = lambda raw_command: print_func(f'Incorrect flag syntax: "{raw_command}"')
51
+ self._repeated_input_flags_handler: Callable[[str], None] = lambda raw_command: print_func(f'Repeated input flags: "{raw_command}"')
52
+ self._empty_input_command_handler: Callable[[], None] = lambda: print_func(f'Empty input command')
53
+ self._unknown_command_handler: Callable[[Command], None] = lambda command: print_func(f"Unknown command: {command.get_string_entity()}")
55
54
 
56
55
 
57
56
  def start_polling(self) -> None:
@@ -76,31 +75,39 @@ class App:
76
75
  input_command: Command = Command.parse_input_command(raw_command=raw_command)
77
76
  except UnprocessedInputFlagException:
78
77
  self.print_func(self.line_separate)
79
- if self._invalid_input_flags_handler:
80
- self._invalid_input_flags_handler(raw_command)
81
- else:
82
- self.print_func(f'Incorrect flag syntax: "{raw_command}"')
78
+ self._invalid_input_flags_handler(raw_command)
83
79
  self.print_func(self.line_separate)
80
+
84
81
  if not self.repeat_command_groups:
85
82
  self.print_func(self.prompt)
86
83
  continue
84
+
87
85
  except RepeatedInputFlagsException:
88
86
  self.print_func(self.line_separate)
89
- if self._repeated_input_flags_handler:
90
- self._repeated_input_flags_handler(raw_command)
91
- else:
92
- self.print_func(f'Repeated input flags: "{raw_command}"')
87
+ self._repeated_input_flags_handler(raw_command)
93
88
  self.print_func(self.line_separate)
89
+
94
90
  if not self.repeat_command_groups:
95
91
  self.print_func(self.prompt)
96
92
  continue
97
93
 
98
- self._checking_command_for_exit_command(input_command.get_string_entity())
99
- self.print_func(self.line_separate)
94
+ except EmptyInputCommandException:
95
+ self.print_func(self.line_separate)
96
+ self._empty_input_command_handler()
97
+ self.print_func(self.line_separate)
100
98
 
99
+ if not self.repeat_command_groups:
100
+ self.print_func(self.prompt)
101
+ continue
102
+
103
+ self._check_command_for_exit_command(input_command.get_string_entity())
104
+
105
+ self.print_func(self.line_separate)
101
106
  is_unknown_command: bool = self._check_is_command_unknown(input_command)
102
107
 
103
108
  if is_unknown_command:
109
+ self.print_func(self.line_separate)
110
+ self.print_func(self.command_group_description_separate)
104
111
  if not self.repeat_command_groups:
105
112
  self.print_func(self.prompt)
106
113
  continue
@@ -123,53 +130,37 @@ class App:
123
130
 
124
131
 
125
132
  def set_description_message_pattern(self, pattern: str) -> None:
126
- try:
127
- pattern.format(command='command',
128
- description='description')
129
- except KeyError:
133
+ first_check = re.match(r'.*{command}.*', pattern)
134
+ second_check = re.match(r'.*{description}.*', pattern)
135
+
136
+ if bool(first_check) and bool(second_check):
137
+ self._description_message_pattern: str = pattern
138
+ else:
130
139
  raise InvalidDescriptionMessagePatternException(pattern)
131
- self._description_message_pattern: str = pattern
132
140
 
133
141
 
134
142
  def set_invalid_input_flags_handler(self, handler: Callable[[str], None]) -> None:
135
- if self._invalid_input_flags_handler:
136
- raise InvalidInputFlagsHandlerHasBeenAlreadyCreatedException()
143
+ args = getfullargspec(handler).args
144
+ if len(args) != 1:
145
+ raise IncorrectNumberOfHandlerArgsException()
137
146
  else:
138
- args = getfullargspec(handler).args
139
- if len(args) != 1:
140
- raise IncorrectNumberOfHandlerArgsException()
141
- else:
142
- self._invalid_input_flags_handler = handler
147
+ self._invalid_input_flags_handler = handler
143
148
 
144
149
 
145
150
  def set_repeated_input_flags_handler(self, handler: Callable[[str], None]) -> None:
146
- if self._repeated_input_flags_handler:
147
- raise RepeatedInputFlagsHandlerHasBeenAlreadyCreatedException()
151
+ args = getfullargspec(handler).args
152
+ if len(args) != 1:
153
+ raise IncorrectNumberOfHandlerArgsException()
148
154
  else:
149
- args = getfullargspec(handler).args
150
- if len(args) != 1:
151
- raise IncorrectNumberOfHandlerArgsException()
152
- else:
153
- self._repeated_input_flags_handler = handler
155
+ self._repeated_input_flags_handler = handler
154
156
 
155
157
 
156
158
  def set_unknown_command_handler(self, handler: Callable[[str], None]) -> None:
157
- if self._unknown_command_handler:
158
- raise UnknownCommandHandlerHasBeenAlreadyCreatedException()
159
+ args = getfullargspec(handler).args
160
+ if len(args) != 1:
161
+ raise IncorrectNumberOfHandlerArgsException()
159
162
  else:
160
- args = getfullargspec(handler).args
161
- if len(args) != 1:
162
- raise IncorrectNumberOfHandlerArgsException()
163
- else:
164
- self._unknown_command_handler = handler
165
-
166
-
167
- def get_all_app_commands(self) -> list[str]:
168
- all_commands: list[str] = []
169
- for router in self._routers:
170
- all_commands.extend(router.get_all_commands())
171
-
172
- return all_commands
163
+ self._unknown_command_handler = handler
173
164
 
174
165
 
175
166
  def include_router(self, router: Router) -> None:
@@ -213,7 +204,7 @@ class App:
213
204
  raise RepeatedCommandInDifferentRoutersException()
214
205
 
215
206
 
216
- def _checking_command_for_exit_command(self, command: str):
207
+ def _check_command_for_exit_command(self, command: str):
217
208
  if command.lower() == self.exit_command.lower():
218
209
  if self.ignore_exit_command_register:
219
210
  self.print_func(self.farewell_message)
@@ -234,14 +225,7 @@ class App:
234
225
  else:
235
226
  if command_entity['command'].get_string_entity() == command.get_string_entity():
236
227
  return False
237
-
238
- if self._unknown_command_handler:
239
- self._unknown_command_handler(command)
240
- else:
241
- print(f"Unknown command: {command.get_string_entity()}")
242
-
243
- self.print_func(self.line_separate)
244
- self.print_func(self.command_group_description_separate)
228
+ self._unknown_command_handler(command)
245
229
  return True
246
230
 
247
231
 
@@ -28,3 +28,8 @@ class NoRegisteredHandlersException(Exception):
28
28
  class RepeatedCommandInDifferentRoutersException(Exception):
29
29
  def __str__(self):
30
30
  return "Commands in different handlers cannot be repeated"
31
+
32
+
33
+ class IncorrectNumberOfHandlerArgsException(Exception):
34
+ def __str__(self):
35
+ return "Incorrect Input Flags Handler has incorrect number of arguments"
@@ -0,0 +1 @@
1
+ from .entity import Command
@@ -2,7 +2,10 @@ from .params.flag.entity import Flag
2
2
  from .params.flag.flags_group.entity import FlagsGroup
3
3
  from .exceptions import (InvalidCommandInstanceException,
4
4
  InvalidDescriptionInstanceException,
5
- InvalidFlagsInstanceException, UnprocessedInputFlagException, RepeatedInputFlagsException)
5
+ InvalidFlagsInstanceException,
6
+ UnprocessedInputFlagException,
7
+ RepeatedInputFlagsException,
8
+ EmptyInputCommandException)
6
9
 
7
10
  from typing import Generic, TypeVar
8
11
 
@@ -12,11 +15,11 @@ T = TypeVar('T')
12
15
 
13
16
  class Command(Generic[T]):
14
17
  def __init__(self, command: str,
15
- description: str | None = None,
18
+ description: str = None,
16
19
  flags: Flag | FlagsGroup | None = None):
17
20
  self._command = command
18
- self._description = description
19
- self._flags: FlagsGroup | None = flags if isinstance(flags, FlagsGroup) else FlagsGroup([flags]) if isinstance(flags, Flag) else flags
21
+ self._description = f'description for "{self._command}" command' if not description else description
22
+ self._registered_flags: FlagsGroup | None = flags if isinstance(flags, FlagsGroup) else FlagsGroup([flags]) if isinstance(flags, Flag) else flags
20
23
 
21
24
  self._input_flags: FlagsGroup | None = None
22
25
 
@@ -26,15 +29,11 @@ class Command(Generic[T]):
26
29
 
27
30
 
28
31
  def get_description(self):
29
- if not self._description:
30
- description = f'description for "{self._command}" command'
31
- return description
32
- else:
33
- return self._description
32
+ return self._description
34
33
 
35
34
 
36
35
  def get_registered_flags(self):
37
- return self._flags
36
+ return self._registered_flags
38
37
 
39
38
 
40
39
  def validate_commands_params(self):
@@ -42,7 +41,7 @@ class Command(Generic[T]):
42
41
  raise InvalidCommandInstanceException(self._command)
43
42
  if not isinstance(self._description, str):
44
43
  raise InvalidDescriptionInstanceException()
45
- if not any([(isinstance(self._flags, FlagsGroup)), not self._flags]):
44
+ if not any([(isinstance(self._registered_flags, FlagsGroup)), not self._registered_flags]):
46
45
  raise InvalidFlagsInstanceException
47
46
 
48
47
 
@@ -71,6 +70,8 @@ class Command(Generic[T]):
71
70
 
72
71
  @staticmethod
73
72
  def parse_input_command(raw_command: str) -> 'Command[T]':
73
+ if not raw_command:
74
+ raise EmptyInputCommandException()
74
75
  list_of_tokens = raw_command.split()
75
76
  command = list_of_tokens[0]
76
77
  list_of_tokens.pop(0)
@@ -29,21 +29,6 @@ class RepeatedInputFlagsException(Exception):
29
29
  f"Duplicate flag was detected in the input: '{self.flag.get_string_entity()}'")
30
30
 
31
31
 
32
- class InvalidInputFlagsHandlerHasBeenAlreadyCreatedException(Exception):
32
+ class EmptyInputCommandException(Exception):
33
33
  def __str__(self):
34
- return "Invalid Input Flags Handler has already been created"
35
-
36
-
37
- class RepeatedInputFlagsHandlerHasBeenAlreadyCreatedException(Exception):
38
- def __str__(self):
39
- return "Repeated Input Flags Handler has already been created"
40
-
41
-
42
- class UnknownCommandHandlerHasBeenAlreadyCreatedException(Exception):
43
- def __str__(self):
44
- return "Unknown Command Handler has already been created"
45
-
46
-
47
- class IncorrectNumberOfHandlerArgsException(Exception):
48
- def __str__(self):
49
- return "Incorrect Input Flags Handler has incorrect number of arguments"
34
+ return "Input Command is empty"
@@ -0,0 +1,2 @@
1
+ from .entity import Flag
2
+ from .flags_group.entity import FlagsGroup
@@ -36,6 +36,7 @@ class Flag:
36
36
  return True
37
37
  else:
38
38
  return False
39
+
39
40
  if isinstance(self.possible_flag_values, list):
40
41
  if self.ignore_flag_value_register:
41
42
  if input_flag_value.lower() in [x.lower() for x in self.possible_flag_values]:
@@ -47,5 +48,4 @@ class Flag:
47
48
  return True
48
49
  else:
49
50
  return False
50
- else:
51
- return True
51
+ return True
@@ -1,11 +1,12 @@
1
1
  from typing import Callable, Any
2
2
  from inspect import getfullargspec
3
+
3
4
  from ..command.entity import Command
5
+ from ..command.params.flag.entity import Flag
4
6
  from ..command.params.flag.flags_group.entity import FlagsGroup
5
7
  from ..router.exceptions import (RepeatedCommandException, RepeatedFlagNameException,
6
8
  TooManyTransferredArgsException,
7
9
  RequiredArgumentNotPassedException,
8
- NotValidInputFlagHandlerHasBeenAlreadyCreatedException,
9
10
  IncorrectNumberOfHandlerArgsException)
10
11
 
11
12
 
@@ -20,7 +21,7 @@ class Router:
20
21
  self._command_entities: list[dict[str, Callable[[], None] | Command]] = []
21
22
  self._ignore_command_register: bool = False
22
23
 
23
- self._not_valid_flag_handler: Callable[[Command], None] | None = None
24
+ self._not_valid_flag_handler: Callable[[Flag], None] = lambda flag: print(f"Undefined or incorrect input flag: '{flag.get_string_entity()} {flag.get_value()}'")
24
25
 
25
26
 
26
27
  def command(self, command: Command) -> Callable[[Any], Any]:
@@ -37,19 +38,12 @@ class Router:
37
38
 
38
39
  return command_decorator
39
40
 
40
- def not_valid_input_flag(self, func):
41
- if self._not_valid_flag_handler:
42
- raise NotValidInputFlagHandlerHasBeenAlreadyCreatedException()
41
+ def set_invalid_input_flag_handler(self, func):
42
+ processed_args = getfullargspec(func).args
43
+ if len(processed_args) != 1:
44
+ raise IncorrectNumberOfHandlerArgsException()
43
45
  else:
44
- processed_args = getfullargspec(func).args
45
- if len(processed_args) != 1:
46
- raise IncorrectNumberOfHandlerArgsException()
47
- else:
48
- self._not_valid_flag_handler = func
49
- def wrapper(*args, **kwargs):
50
- return func(*args, **kwargs)
51
-
52
- return wrapper
46
+ self._not_valid_flag_handler = func
53
47
 
54
48
 
55
49
  def input_command_handler(self, input_command: Command):
@@ -62,20 +56,14 @@ class Router:
62
56
  for flag in input_command_flags:
63
57
  is_valid = command_entity['command'].validate_input_flag(flag)
64
58
  if not is_valid:
65
- if self._not_valid_flag_handler:
66
- self._not_valid_flag_handler(input_command)
67
- else:
68
- print(f"Undefined or incorrect input flag: '{flag.get_string_entity()} {flag.get_value()}'")
59
+ self._not_valid_flag_handler(flag)
69
60
  return
70
61
  return command_entity['handler_func'](input_command_flags)
71
62
  else:
72
63
  return command_entity['handler_func'](FlagsGroup(None))
73
64
  else:
74
65
  if input_command_flags:
75
- if self._not_valid_flag_handler:
76
- self._not_valid_flag_handler(input_command)
77
- else:
78
- print(f"Undefined or incorrect input flag: '{input_command_flags[0].get_string_entity()} {input_command_flags[0].get_value()}'")
66
+ self._not_valid_flag_handler(input_command_flags[0])
79
67
  return
80
68
  else:
81
69
  return command_entity['handler_func']()
@@ -125,28 +113,9 @@ class Router:
125
113
  return self.title
126
114
 
127
115
 
128
- def get_router_info(self) -> dict:
129
- return {
130
- 'title': self.title,
131
- 'name': self.name,
132
- 'ignore_command_register': self._ignore_command_register,
133
- 'attributes': {
134
- 'command_entities': self._command_entities,
135
- }
136
-
137
- }
138
-
139
-
140
116
  def get_all_commands(self) -> list[str]:
141
117
  all_commands: list[str] = []
142
118
  for command_entity in self._command_entities:
143
119
  all_commands.append(command_entity['command'].get_string_entity())
144
120
 
145
121
  return all_commands
146
-
147
- def get_all_flags(self) -> list[FlagsGroup]:
148
- all_flags: list[FlagsGroup] = []
149
- for command_entity in self._command_entities:
150
- all_flags.append(command_entity['command'].get_registered_flags())
151
-
152
- return all_flags
@@ -1,9 +1,9 @@
1
1
  [project]
2
2
  name = "argenta"
3
- version = "0.3.3"
3
+ version = "0.3.5"
4
4
  description = "python library for creating custom shells"
5
5
  authors = [
6
- {name = "kolo",email = "kolo.is.main@gmail.com"}
6
+ {name = "kolo", email = "kolo.is.main@gmail.com"}
7
7
  ]
8
8
  license = {text = "MIT"}
9
9
  readme = "README.md"
File without changes
File without changes
File without changes
File without changes
File without changes