argenta 0.3.1__tar.gz → 0.3.3__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.
Files changed (27) hide show
  1. {argenta-0.3.1 → argenta-0.3.3}/PKG-INFO +1 -1
  2. {argenta-0.3.1 → argenta-0.3.3}/argenta/app/entity.py +32 -9
  3. argenta-0.3.3/argenta/command/entity.py +120 -0
  4. argenta-0.3.3/argenta/command/exceptions.py +49 -0
  5. {argenta-0.3.1 → argenta-0.3.3}/argenta/command/params/flag/entity.py +9 -3
  6. argenta-0.3.3/argenta/command/params/flag/flags_group/entity.py +24 -0
  7. {argenta-0.3.1 → argenta-0.3.3}/argenta/router/entity.py +38 -18
  8. {argenta-0.3.1 → argenta-0.3.3}/argenta/router/exceptions.py +10 -5
  9. {argenta-0.3.1 → argenta-0.3.3}/pyproject.toml +1 -3
  10. argenta-0.3.1/argenta/command/entity.py +0 -60
  11. argenta-0.3.1/argenta/command/exceptions.py +0 -13
  12. argenta-0.3.1/argenta/command/input_comand/entity.py +0 -66
  13. argenta-0.3.1/argenta/command/input_comand/exceptions.py +0 -39
  14. argenta-0.3.1/argenta/command/params/flag/flags_group/__init__.py +0 -0
  15. argenta-0.3.1/argenta/command/params/flag/flags_group/entity.py +0 -22
  16. argenta-0.3.1/argenta/command/params/flag/input_flag/__init__.py +0 -0
  17. argenta-0.3.1/argenta/command/params/flag/input_flag/entity.py +0 -11
  18. {argenta-0.3.1 → argenta-0.3.3}/LICENSE +0 -0
  19. {argenta-0.3.1 → argenta-0.3.3}/README.md +0 -0
  20. {argenta-0.3.1 → argenta-0.3.3}/argenta/__init__.py +0 -0
  21. {argenta-0.3.1 → argenta-0.3.3}/argenta/app/__init__.py +0 -0
  22. {argenta-0.3.1 → argenta-0.3.3}/argenta/app/exceptions.py +0 -0
  23. {argenta-0.3.1 → argenta-0.3.3}/argenta/command/__init__.py +0 -0
  24. {argenta-0.3.1/argenta/command/input_comand → argenta-0.3.3/argenta/command/params}/__init__.py +0 -0
  25. {argenta-0.3.1/argenta/command/params → argenta-0.3.3/argenta/command/params/flag}/__init__.py +0 -0
  26. {argenta-0.3.1/argenta/command/params/flag → argenta-0.3.3/argenta/command/params/flag/flags_group}/__init__.py +0 -0
  27. {argenta-0.3.1 → argenta-0.3.3}/argenta/router/__init__.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: argenta
3
- Version: 0.3.1
3
+ Version: 0.3.3
4
4
  Summary: python library for creating custom shells
5
5
  License: MIT
6
6
  Author: kolo
@@ -3,11 +3,12 @@ from inspect import getfullargspec
3
3
 
4
4
  from ..command.entity import Command
5
5
  from ..router.entity import Router
6
- from ..command.input_comand.entity import InputCommand
7
- from ..command.input_comand.exceptions import (IncorrectInputFlagException,
8
- InvalidInputFlagsHandlerHasBeenAlreadyCreatedException,
9
- IncorrectNumberArgsHandlerException,
10
- UnknownCommandHandlerHasBeenAlreadyCreatedException)
6
+ from ..command.exceptions import (UnprocessedInputFlagException,
7
+ InvalidInputFlagsHandlerHasBeenAlreadyCreatedException,
8
+ IncorrectNumberOfHandlerArgsException,
9
+ UnknownCommandHandlerHasBeenAlreadyCreatedException,
10
+ RepeatedInputFlagsException,
11
+ RepeatedInputFlagsHandlerHasBeenAlreadyCreatedException)
11
12
  from .exceptions import (InvalidRouterInstanceException,
12
13
  InvalidDescriptionMessagePatternException,
13
14
  NoRegisteredRoutersException,
@@ -46,6 +47,7 @@ class App:
46
47
 
47
48
  self._routers: list[Router] = []
48
49
  self._invalid_input_flags_handler: Callable[[str], None] | None = None
50
+ self._repeated_input_flags_handler: Callable[[str], None] | None = None
49
51
  self._unknown_command_handler: Callable[[Command], None] | None = None
50
52
  self._registered_router_entities: list[dict[str, str | list[dict[str, Callable[[], None] | Command]] | Router]] = []
51
53
  self._app_main_router: Router | None = None
@@ -71,8 +73,8 @@ class App:
71
73
  raw_command: str = input()
72
74
 
73
75
  try:
74
- input_command: InputCommand = InputCommand.parse(raw_command=raw_command)
75
- except IncorrectInputFlagException:
76
+ input_command: Command = Command.parse_input_command(raw_command=raw_command)
77
+ except UnprocessedInputFlagException:
76
78
  self.print_func(self.line_separate)
77
79
  if self._invalid_input_flags_handler:
78
80
  self._invalid_input_flags_handler(raw_command)
@@ -82,6 +84,16 @@ class App:
82
84
  if not self.repeat_command_groups:
83
85
  self.print_func(self.prompt)
84
86
  continue
87
+ except RepeatedInputFlagsException:
88
+ 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}"')
93
+ self.print_func(self.line_separate)
94
+ if not self.repeat_command_groups:
95
+ self.print_func(self.prompt)
96
+ continue
85
97
 
86
98
  self._checking_command_for_exit_command(input_command.get_string_entity())
87
99
  self.print_func(self.line_separate)
@@ -125,18 +137,29 @@ class App:
125
137
  else:
126
138
  args = getfullargspec(handler).args
127
139
  if len(args) != 1:
128
- raise IncorrectNumberArgsHandlerException()
140
+ raise IncorrectNumberOfHandlerArgsException()
129
141
  else:
130
142
  self._invalid_input_flags_handler = handler
131
143
 
132
144
 
145
+ def set_repeated_input_flags_handler(self, handler: Callable[[str], None]) -> None:
146
+ if self._repeated_input_flags_handler:
147
+ raise RepeatedInputFlagsHandlerHasBeenAlreadyCreatedException()
148
+ else:
149
+ args = getfullargspec(handler).args
150
+ if len(args) != 1:
151
+ raise IncorrectNumberOfHandlerArgsException()
152
+ else:
153
+ self._repeated_input_flags_handler = handler
154
+
155
+
133
156
  def set_unknown_command_handler(self, handler: Callable[[str], None]) -> None:
134
157
  if self._unknown_command_handler:
135
158
  raise UnknownCommandHandlerHasBeenAlreadyCreatedException()
136
159
  else:
137
160
  args = getfullargspec(handler).args
138
161
  if len(args) != 1:
139
- raise IncorrectNumberArgsHandlerException()
162
+ raise IncorrectNumberOfHandlerArgsException()
140
163
  else:
141
164
  self._unknown_command_handler = handler
142
165
 
@@ -0,0 +1,120 @@
1
+ from .params.flag.entity import Flag
2
+ from .params.flag.flags_group.entity import FlagsGroup
3
+ from .exceptions import (InvalidCommandInstanceException,
4
+ InvalidDescriptionInstanceException,
5
+ InvalidFlagsInstanceException, UnprocessedInputFlagException, RepeatedInputFlagsException)
6
+
7
+ from typing import Generic, TypeVar
8
+
9
+
10
+ T = TypeVar('T')
11
+
12
+
13
+ class Command(Generic[T]):
14
+ def __init__(self, command: str,
15
+ description: str | None = None,
16
+ flags: Flag | FlagsGroup | None = None):
17
+ 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
20
+
21
+ self._input_flags: FlagsGroup | None = None
22
+
23
+
24
+ def get_string_entity(self):
25
+ return self._command
26
+
27
+
28
+ 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
34
+
35
+
36
+ def get_registered_flags(self):
37
+ return self._flags
38
+
39
+
40
+ def validate_commands_params(self):
41
+ if not isinstance(self._command, str):
42
+ raise InvalidCommandInstanceException(self._command)
43
+ if not isinstance(self._description, str):
44
+ raise InvalidDescriptionInstanceException()
45
+ if not any([(isinstance(self._flags, FlagsGroup)), not self._flags]):
46
+ raise InvalidFlagsInstanceException
47
+
48
+
49
+ def validate_input_flag(self, flag: Flag):
50
+ registered_flags: FlagsGroup | None = self.get_registered_flags()
51
+ if registered_flags:
52
+ if isinstance(registered_flags, Flag):
53
+ if registered_flags.get_string_entity() == flag.get_string_entity():
54
+ is_valid = registered_flags.validate_input_flag_value(flag.get_value())
55
+ if is_valid:
56
+ return True
57
+ else:
58
+ for registered_flag in registered_flags:
59
+ if registered_flag.get_string_entity() == flag.get_string_entity():
60
+ is_valid = registered_flag.validate_input_flag_value(flag.get_value())
61
+ if is_valid:
62
+ return True
63
+ return False
64
+
65
+
66
+ def set_input_flags(self, input_flags: FlagsGroup):
67
+ self._input_flags = input_flags
68
+
69
+ def get_input_flags(self) -> FlagsGroup:
70
+ return self._input_flags
71
+
72
+ @staticmethod
73
+ def parse_input_command(raw_command: str) -> 'Command[T]':
74
+ list_of_tokens = raw_command.split()
75
+ command = list_of_tokens[0]
76
+ list_of_tokens.pop(0)
77
+
78
+ flags: FlagsGroup = FlagsGroup()
79
+ current_flag_name = None
80
+ current_flag_value = None
81
+ for _ in list_of_tokens:
82
+ if _.startswith('-'):
83
+ flag_prefix_last_symbol_index = _.rfind('-')
84
+ if current_flag_name or len(_) < 2 or len(_[:flag_prefix_last_symbol_index]) > 3:
85
+ raise UnprocessedInputFlagException()
86
+ else:
87
+ current_flag_name = _
88
+ else:
89
+ if not current_flag_name:
90
+ raise UnprocessedInputFlagException()
91
+ else:
92
+ current_flag_value = _
93
+ if current_flag_name and current_flag_value:
94
+ flag_prefix_last_symbol_index = current_flag_name.rfind('-')
95
+ flag_prefix = current_flag_name[:flag_prefix_last_symbol_index]
96
+ flag_name = current_flag_name[flag_prefix_last_symbol_index:]
97
+
98
+ input_flag = Flag(flag_name=flag_name,
99
+ flag_prefix=flag_prefix)
100
+ input_flag.set_value(current_flag_value)
101
+
102
+ all_flags = [x.get_string_entity() for x in flags.get_flags()]
103
+ if input_flag.get_string_entity() not in all_flags:
104
+ flags.add_flag(input_flag)
105
+ else:
106
+ raise RepeatedInputFlagsException(input_flag)
107
+
108
+ current_flag_name = None
109
+ current_flag_value = None
110
+ if any([current_flag_name, current_flag_value]):
111
+ raise UnprocessedInputFlagException()
112
+ if len(flags.get_flags()) == 0:
113
+ return Command(command=command)
114
+ else:
115
+ input_command = Command(command=command)
116
+ input_command.set_input_flags(flags)
117
+ return input_command
118
+
119
+
120
+
@@ -0,0 +1,49 @@
1
+ from .params.flag.entity import Flag
2
+
3
+
4
+ class InvalidCommandInstanceException(Exception):
5
+ def __str__(self):
6
+ return "Invalid Command Instance"
7
+
8
+
9
+ class InvalidDescriptionInstanceException(Exception):
10
+ def __str__(self):
11
+ return "Invalid Description Instance"
12
+
13
+
14
+ class InvalidFlagsInstanceException(Exception):
15
+ def __str__(self):
16
+ return "Invalid Flags Instance"
17
+
18
+
19
+ class UnprocessedInputFlagException(Exception):
20
+ def __str__(self):
21
+ return "Unprocessed Input Flags"
22
+
23
+
24
+ class RepeatedInputFlagsException(Exception):
25
+ def __init__(self, flag: Flag):
26
+ self.flag = flag
27
+ def __str__(self):
28
+ return ("Repeated Input Flags\n"
29
+ f"Duplicate flag was detected in the input: '{self.flag.get_string_entity()}'")
30
+
31
+
32
+ class InvalidInputFlagsHandlerHasBeenAlreadyCreatedException(Exception):
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"
@@ -1,11 +1,11 @@
1
- from typing import Literal
1
+ from typing import Literal, Pattern
2
2
 
3
3
 
4
4
  class Flag:
5
5
  def __init__(self, flag_name: str,
6
6
  flag_prefix: Literal['-', '--', '---'] = '-',
7
7
  ignore_flag_value_register: bool = False,
8
- possible_flag_values: list[str] = False):
8
+ possible_flag_values: list[str] | Pattern[str] = False):
9
9
  self._flag_name = flag_name
10
10
  self._flag_prefix = flag_prefix
11
11
  self.possible_flag_values = possible_flag_values
@@ -30,7 +30,13 @@ class Flag:
30
30
  self._value = value
31
31
 
32
32
  def validate_input_flag_value(self, input_flag_value: str):
33
- if self.possible_flag_values:
33
+ if isinstance(self.possible_flag_values, Pattern):
34
+ is_valid = bool(self.possible_flag_values.match(input_flag_value))
35
+ if bool(is_valid):
36
+ return True
37
+ else:
38
+ return False
39
+ if isinstance(self.possible_flag_values, list):
34
40
  if self.ignore_flag_value_register:
35
41
  if input_flag_value.lower() in [x.lower() for x in self.possible_flag_values]:
36
42
  return True
@@ -0,0 +1,24 @@
1
+ from argenta.command.params.flag.entity import Flag
2
+
3
+
4
+ class FlagsGroup:
5
+ def __init__(self, flags: list[Flag] = None):
6
+ self._flags: list[Flag] = [] if not flags else flags
7
+
8
+ def get_flags(self):
9
+ return self._flags
10
+
11
+ def add_flag(self, flag: Flag):
12
+ self._flags.append(flag)
13
+
14
+ def add_flags(self, flags: list[Flag]):
15
+ self._flags.extend(flags)
16
+
17
+ def __iter__(self):
18
+ return iter(self._flags)
19
+
20
+ def __next__(self):
21
+ return next(iter(self))
22
+
23
+ def __getitem__(self, item):
24
+ return self._flags[item]
@@ -1,13 +1,12 @@
1
1
  from typing import Callable, Any
2
2
  from inspect import getfullargspec
3
3
  from ..command.entity import Command
4
- from ..command.input_comand.entity import InputCommand
5
- from ..command.input_comand.exceptions import InvalidInputFlagException
6
4
  from ..command.params.flag.flags_group.entity import FlagsGroup
7
5
  from ..router.exceptions import (RepeatedCommandException, RepeatedFlagNameException,
8
- CurrentCommandDoesNotProcessFlagsException,
9
6
  TooManyTransferredArgsException,
10
- RequiredArgumentNotPassedException)
7
+ RequiredArgumentNotPassedException,
8
+ NotValidInputFlagHandlerHasBeenAlreadyCreatedException,
9
+ IncorrectNumberOfHandlerArgsException)
11
10
 
12
11
 
13
12
  class Router:
@@ -21,8 +20,7 @@ class Router:
21
20
  self._command_entities: list[dict[str, Callable[[], None] | Command]] = []
22
21
  self._ignore_command_register: bool = False
23
22
 
24
- self._unknown_command_handler: Callable[[str], None] | None = None
25
- self._not_valid_flag_handler: Callable[[str], None] | None = None
23
+ self._not_valid_flag_handler: Callable[[Command], None] | None = None
26
24
 
27
25
 
28
26
  def command(self, command: Command) -> Callable[[Any], Any]:
@@ -39,23 +37,46 @@ class Router:
39
37
 
40
38
  return command_decorator
41
39
 
40
+ def not_valid_input_flag(self, func):
41
+ if self._not_valid_flag_handler:
42
+ raise NotValidInputFlagHandlerHasBeenAlreadyCreatedException()
43
+ 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)
42
51
 
43
- def input_command_handler(self, input_command: InputCommand):
52
+ return wrapper
53
+
54
+
55
+ def input_command_handler(self, input_command: Command):
44
56
  input_command_name: str = input_command.get_string_entity()
57
+ input_command_flags: FlagsGroup = input_command.get_input_flags()
45
58
  for command_entity in self._command_entities:
46
59
  if input_command_name.lower() == command_entity['command'].get_string_entity().lower():
47
- if command_entity['command'].get_flags():
48
- if input_command.get_input_flags():
49
- for flag in input_command.get_input_flags():
60
+ if command_entity['command'].get_registered_flags():
61
+ if input_command_flags:
62
+ for flag in input_command_flags:
50
63
  is_valid = command_entity['command'].validate_input_flag(flag)
51
64
  if not is_valid:
52
- raise InvalidInputFlagException(flag)
53
- return command_entity['handler_func'](input_command.get_input_flags())
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()}'")
69
+ return
70
+ return command_entity['handler_func'](input_command_flags)
54
71
  else:
55
72
  return command_entity['handler_func'](FlagsGroup(None))
56
73
  else:
57
- if input_command.get_input_flags():
58
- raise CurrentCommandDoesNotProcessFlagsException()
74
+ 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()}'")
79
+ return
59
80
  else:
60
81
  return command_entity['handler_func']()
61
82
 
@@ -68,7 +89,7 @@ class Router:
68
89
  if command_name.lower() in [x.lower() for x in self.get_all_commands()]:
69
90
  raise RepeatedCommandException()
70
91
 
71
- flags: FlagsGroup = command.get_flags()
92
+ flags: FlagsGroup = command.get_registered_flags()
72
93
  if flags:
73
94
  flags_name: list = [x.get_string_entity().lower() for x in flags]
74
95
  if len(set(flags_name)) < len(flags_name):
@@ -77,7 +98,7 @@ class Router:
77
98
 
78
99
  @staticmethod
79
100
  def _validate_func_args(command: Command, func: Callable):
80
- registered_args = command.get_flags()
101
+ registered_args = command.get_registered_flags()
81
102
  transferred_args = getfullargspec(func).args
82
103
  if registered_args and transferred_args:
83
104
  if len(transferred_args) != 1:
@@ -111,7 +132,6 @@ class Router:
111
132
  'ignore_command_register': self._ignore_command_register,
112
133
  'attributes': {
113
134
  'command_entities': self._command_entities,
114
- 'unknown_command_func': self._unknown_command_handler
115
135
  }
116
136
 
117
137
  }
@@ -127,6 +147,6 @@ class Router:
127
147
  def get_all_flags(self) -> list[FlagsGroup]:
128
148
  all_flags: list[FlagsGroup] = []
129
149
  for command_entity in self._command_entities:
130
- all_flags.append(command_entity['command'].get_flags())
150
+ all_flags.append(command_entity['command'].get_registered_flags())
131
151
 
132
152
  return all_flags
@@ -13,11 +13,6 @@ class RepeatedFlagNameException(Exception):
13
13
  return "Repeated flag name in register command"
14
14
 
15
15
 
16
- class CurrentCommandDoesNotProcessFlagsException(Exception):
17
- def __str__(self):
18
- return "Current command does not process flags"
19
-
20
-
21
16
  class TooManyTransferredArgsException(Exception):
22
17
  def __str__(self):
23
18
  return "Too many transferred arguments"
@@ -26,3 +21,13 @@ class TooManyTransferredArgsException(Exception):
26
21
  class RequiredArgumentNotPassedException(Exception):
27
22
  def __str__(self):
28
23
  return "Required argument not passed"
24
+
25
+
26
+ class NotValidInputFlagHandlerHasBeenAlreadyCreatedException(Exception):
27
+ def __str__(self):
28
+ return "Invalid Input Flag Handler has already been created"
29
+
30
+
31
+ class IncorrectNumberOfHandlerArgsException(Exception):
32
+ def __str__(self):
33
+ return "Incorrect Input Flags Handler has incorrect number of arguments"
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "argenta"
3
- version = "0.3.1"
3
+ version = "0.3.3"
4
4
  description = "python library for creating custom shells"
5
5
  authors = [
6
6
  {name = "kolo",email = "kolo.is.main@gmail.com"}
@@ -33,6 +33,4 @@ numpy = "^2.2.2"
33
33
  word2number = "^1.1"
34
34
  numexpr = "^2.10.2"
35
35
  requests = "^2.32.3"
36
- tqdm = "^4.67.1"
37
- setuptools = "^75.8.0"
38
36
 
@@ -1,60 +0,0 @@
1
- from .params.flag.entity import Flag
2
- from .params.flag.flags_group.entity import FlagsGroup
3
- from .exceptions import (InvalidCommandInstanceException,
4
- InvalidDescriptionInstanceException,
5
- InvalidFlagsInstanceException)
6
- from .params.flag.input_flag.entity import InputFlag
7
-
8
-
9
- class Command:
10
- def __init__(self, command: str,
11
- description: str | None = None,
12
- flags: Flag | FlagsGroup | None = None):
13
- self._command = command
14
- self._description = description
15
- self._flags: FlagsGroup | None = flags if isinstance(flags, FlagsGroup) else FlagsGroup([flags]) if isinstance(flags, Flag) else flags
16
-
17
- self._input_flags: InputFlag | FlagsGroup | None = None
18
-
19
- def get_string_entity(self):
20
- return self._command
21
-
22
- def get_description(self):
23
- if not self._description:
24
- description = f'description for "{self._command}" command'
25
- return description
26
- else:
27
- return self._description
28
-
29
- def get_flags(self):
30
- return self._flags
31
-
32
- def set_command(self, command: str):
33
- self._command = command
34
-
35
- def validate_commands_params(self):
36
- if not isinstance(self._command, str):
37
- raise InvalidCommandInstanceException(self._command)
38
- if not isinstance(self._description, str):
39
- raise InvalidDescriptionInstanceException()
40
- if not any([(isinstance(self._flags, Flag), isinstance(self._flags, FlagsGroup)), not self._flags]):
41
- raise InvalidFlagsInstanceException
42
-
43
- def validate_input_flag(self, flag: InputFlag):
44
- registered_flags: FlagsGroup | Flag | None = self._flags
45
- if registered_flags:
46
- if isinstance(registered_flags, Flag):
47
- if registered_flags.get_string_entity() == flag.get_string_entity():
48
- is_valid = registered_flags.validate_input_flag_value(flag.get_value())
49
- if is_valid:
50
- return True
51
- else:
52
- for registered_flag in registered_flags:
53
- if registered_flag.get_string_entity() == flag.get_string_entity():
54
- is_valid = registered_flag.validate_input_flag_value(flag.get_value())
55
- if is_valid:
56
- return True
57
- return False
58
-
59
-
60
-
@@ -1,13 +0,0 @@
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 InvalidFlagsInstanceException(Exception):
12
- def __str__(self):
13
- return "Invalid Flags Instance"
@@ -1,66 +0,0 @@
1
- from ..input_comand.exceptions import IncorrectInputFlagException, RepeatedInputFlagsException
2
- from ..entity import Command
3
- from ..params.flag.flags_group.entity import FlagsGroup
4
- from ..params.flag.input_flag.entity import InputFlag
5
-
6
- from typing import Generic, TypeVar
7
-
8
-
9
- T = TypeVar('T')
10
-
11
-
12
- class InputCommand(Command, Generic[T]):
13
- def set_input_flags(self, input_flags: FlagsGroup):
14
- self._input_flags = input_flags
15
-
16
- def get_input_flags(self) -> FlagsGroup:
17
- return self._input_flags
18
-
19
- @staticmethod
20
- def parse(raw_command: str) -> 'InputCommand[T]':
21
- list_of_tokens = raw_command.split()
22
- command = list_of_tokens[0]
23
- list_of_tokens.pop(0)
24
-
25
- flags: FlagsGroup = FlagsGroup()
26
- current_flag_name = None
27
- current_flag_value = None
28
- for _ in list_of_tokens:
29
- if _.startswith('-'):
30
- flag_prefix_last_symbol_index = _.rfind('-')
31
- if current_flag_name or len(_) < 2 or len(_[:flag_prefix_last_symbol_index]) > 3:
32
- raise IncorrectInputFlagException()
33
- else:
34
- current_flag_name = _
35
- else:
36
- if not current_flag_name:
37
- raise IncorrectInputFlagException()
38
- else:
39
- current_flag_value = _
40
- if current_flag_name and current_flag_value:
41
- flag_prefix_last_symbol_index = current_flag_name.rfind('-')
42
- flag_prefix = current_flag_name[:flag_prefix_last_symbol_index]
43
- flag_name = current_flag_name[flag_prefix_last_symbol_index:]
44
-
45
- input_flag = InputFlag(flag_name=flag_name,
46
- flag_prefix=flag_prefix)
47
- input_flag.set_value(current_flag_value)
48
-
49
- all_flags = [x.get_string_entity() for x in flags.get_flags()]
50
- if input_flag.get_string_entity() not in all_flags:
51
- flags.add_flag(input_flag)
52
- else:
53
- raise RepeatedInputFlagsException(input_flag)
54
-
55
- current_flag_name = None
56
- current_flag_value = None
57
- if any([current_flag_name, current_flag_value]):
58
- raise IncorrectInputFlagException()
59
- if len(flags.get_flags()) == 0:
60
- return InputCommand(command=command)
61
- else:
62
- input_command = InputCommand(command=command)
63
- input_command.set_input_flags(flags)
64
- return input_command
65
-
66
-
@@ -1,39 +0,0 @@
1
- from ..params.flag.input_flag.entity import InputFlag
2
-
3
-
4
- class InvalidInputFlagException(Exception):
5
- def __init__(self, flag: InputFlag):
6
- self.flag = flag
7
- def __str__(self):
8
- return ("Invalid Input Flags\n"
9
- f"Unknown or invalid input flag: '{self.flag.get_string_entity()} {self.flag.get_value()}'")
10
-
11
- class IncorrectInputFlagException(Exception):
12
- def __str__(self):
13
- return "Incorrect Input Flags"
14
-
15
-
16
- class RepeatedInputFlagsException(Exception):
17
- def __init__(self, flag: InputFlag):
18
- self.flag = flag
19
- def __str__(self):
20
- return ("Repeated Input Flags\n"
21
- f"Duplicate flag was detected in the input: '{self.flag.get_string_entity()}'")
22
-
23
-
24
- class InvalidInputFlagsHandlerHasBeenAlreadyCreatedException(Exception):
25
- def __str__(self):
26
- return "Invalid Input Flags Handler has already been created"
27
-
28
-
29
- class UnknownCommandHandlerHasBeenAlreadyCreatedException(Exception):
30
- def __str__(self):
31
- return "Unknown Command Handler has already been created"
32
-
33
-
34
- class IncorrectNumberArgsHandlerException(Exception):
35
- def __str__(self):
36
- return "Incorrect Input Flags Handler has incorrect number of arguments"
37
-
38
-
39
-
@@ -1,22 +0,0 @@
1
- from argenta.command.params.flag.entity import Flag
2
- from argenta.command.params.flag.input_flag.entity import InputFlag
3
-
4
-
5
- class FlagsGroup:
6
- def __init__(self, flags: list[Flag | InputFlag] = None):
7
- self._flags: list[Flag | InputFlag] = [] if not flags else flags
8
-
9
- def get_flags(self):
10
- return self._flags
11
-
12
- def add_flag(self, flag: Flag | InputFlag):
13
- self._flags.append(flag)
14
-
15
- def add_flags(self, flags: list[Flag | InputFlag]):
16
- self._flags.extend(flags)
17
-
18
- def __iter__(self):
19
- return iter(self._flags)
20
-
21
- def __next__(self):
22
- return next(iter(self))
@@ -1,11 +0,0 @@
1
- from ...flag.entity import Flag
2
-
3
-
4
- class InputFlag(Flag):
5
- def set_value(self, value: str):
6
- self._value = value
7
-
8
- def get_value(self) -> str:
9
- return self._value
10
-
11
-
File without changes
File without changes
File without changes
File without changes