cli-ih 0.6.0.2.1__py3-none-any.whl → 0.6.1.2__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.
- cli_ih/Input_Handler.py +39 -20
- {cli_ih-0.6.0.2.1.dist-info → cli_ih-0.6.1.2.dist-info}/METADATA +1 -1
- cli_ih-0.6.1.2.dist-info/RECORD +6 -0
- cli_ih-0.6.0.2.1.dist-info/RECORD +0 -6
- {cli_ih-0.6.0.2.1.dist-info → cli_ih-0.6.1.2.dist-info}/WHEEL +0 -0
- {cli_ih-0.6.0.2.1.dist-info → cli_ih-0.6.1.2.dist-info}/top_level.txt +0 -0
cli_ih/Input_Handler.py
CHANGED
|
@@ -52,17 +52,20 @@ class InputHandler:
|
|
|
52
52
|
else:
|
|
53
53
|
print(f"[EXEPTION]: {msg}: {e}")
|
|
54
54
|
|
|
55
|
-
def __register_cmd(self, name: str, func: Callable, description: str = ""):
|
|
55
|
+
def __register_cmd(self, name: str, func: Callable, description: str = "", legacy=False):
|
|
56
|
+
name = name.lower()
|
|
56
57
|
if not description:
|
|
57
58
|
description = "A command"
|
|
58
59
|
if ' ' in name:
|
|
59
60
|
raise SyntaxError("Command name must not have spaces")
|
|
60
|
-
self.commands
|
|
61
|
+
if name in self.commands:
|
|
62
|
+
raise SyntaxError(f"Command '{name}' is already registered. If theese commands have a different case and they need to stay the same, downgrade the package version to 0.5.x")
|
|
63
|
+
self.commands[name] = {"cmd": func, "description": description, "legacy": legacy}
|
|
61
64
|
|
|
62
65
|
def register_command(self, name: str, func: Callable, description: str = ""):
|
|
63
66
|
"""Registers a command with its associated function."""
|
|
64
67
|
warnings.warn("Registering commands with `register_command` is deprecated, and will be removed in the next big update.", DeprecationWarning, 2)
|
|
65
|
-
self.__register_cmd(name, func, description)
|
|
68
|
+
self.__register_cmd(name, func, description, legacy=True)
|
|
66
69
|
|
|
67
70
|
def command(self, *, name: str = "", description: str = ""):
|
|
68
71
|
"""Registers a command with its associated function as a decorator."""
|
|
@@ -84,17 +87,36 @@ class InputHandler:
|
|
|
84
87
|
command = commands.get(name)
|
|
85
88
|
if command:
|
|
86
89
|
func = command.get("cmd")
|
|
90
|
+
is_legacy = command.get("legacy", False)
|
|
87
91
|
if callable(func):
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
92
|
+
if is_legacy:
|
|
93
|
+
try:
|
|
94
|
+
sig = inspect.signature(func)
|
|
95
|
+
sig.bind(args)
|
|
96
|
+
except TypeError as e:
|
|
97
|
+
self.__warning(f"Argument error for legacy command '{name}': {e}")
|
|
98
|
+
return
|
|
99
|
+
|
|
100
|
+
try:
|
|
101
|
+
warnings.warn("This way of running commands id Deprecated. And should be changed to the new decorator way.", DeprecationWarning, 2)
|
|
102
|
+
func(args)
|
|
103
|
+
except HandlerClosed as e:
|
|
104
|
+
raise e
|
|
105
|
+
except Exception as e:
|
|
106
|
+
self.__exeption(f"An error occurred in legacy command '{name}'", e)
|
|
107
|
+
else:
|
|
108
|
+
try:
|
|
109
|
+
sig = inspect.signature(func)
|
|
110
|
+
sig.bind(*args)
|
|
111
|
+
except TypeError as e:
|
|
112
|
+
self.__warning(f"Argument error for command '{name}': {e}")
|
|
113
|
+
return
|
|
114
|
+
try:
|
|
115
|
+
func(*args)
|
|
116
|
+
except HandlerClosed as e:
|
|
117
|
+
raise e
|
|
118
|
+
except Exception as e:
|
|
119
|
+
self.__exeption(f"An error occurred in command '{name}'", e)
|
|
98
120
|
else:
|
|
99
121
|
raise ValueError(f"The command '{name}' is not callable.")
|
|
100
122
|
else:
|
|
@@ -134,14 +156,14 @@ class InputHandler:
|
|
|
134
156
|
|
|
135
157
|
def register_default_commands(self):
|
|
136
158
|
@self.command(name="help", description="Displays all the available commands")
|
|
137
|
-
def help(
|
|
159
|
+
def help():
|
|
138
160
|
str_out = "Available commands:\n"
|
|
139
161
|
for command, data in self.commands.items():
|
|
140
162
|
str_out += f" {command}: {data['description']}\n"
|
|
141
163
|
print(str_out)
|
|
142
164
|
|
|
143
165
|
@self.command(name="debug", description="If a logger is present changes the logging level to DEBUG.")
|
|
144
|
-
def debug_mode(
|
|
166
|
+
def debug_mode():
|
|
145
167
|
logger = self.global_logger
|
|
146
168
|
if not logger:
|
|
147
169
|
return self.__warning("No logger defined for this InputHandler instance.")
|
|
@@ -161,8 +183,5 @@ class InputHandler:
|
|
|
161
183
|
self.__info(message)
|
|
162
184
|
|
|
163
185
|
@self.command(name="exit", description="Exits the Input Handler irreversibly.")
|
|
164
|
-
def exit_thread(
|
|
165
|
-
raise HandlerClosed("Handler was closed with exit command.")
|
|
166
|
-
# self.register_command("help", help, "Displays all the available commands")
|
|
167
|
-
# self.register_command("debug", debug_mode, "Changes the logging level to DEBUG.")
|
|
168
|
-
# self.register_command("exit", exit_thread, "Exits the Input Handler irreversibly.")
|
|
186
|
+
def exit_thread():
|
|
187
|
+
raise HandlerClosed("Handler was closed with exit command.")
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
cli_ih/Input_Handler.py,sha256=Az90f37leURlrol1YWJfdtnfnljZ1i7uorgkQ2LJqrI,7856
|
|
2
|
+
cli_ih/__init__.py,sha256=X1CBDfeflUjcXik3rHCo-YLrIDe6EsH2ShnK57DDRYc,210
|
|
3
|
+
cli_ih-0.6.1.2.dist-info/METADATA,sha256=BOmWrixDKwQT6tR4NZFcsYaQ2ZH-BQYq7Tt2ziys6Xc,2239
|
|
4
|
+
cli_ih-0.6.1.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
5
|
+
cli_ih-0.6.1.2.dist-info/top_level.txt,sha256=Ve1CRLNXhPyPSkpN0xLu26roh30LQCpNzkF61BZYfk0,7
|
|
6
|
+
cli_ih-0.6.1.2.dist-info/RECORD,,
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
cli_ih/Input_Handler.py,sha256=INlPvqlHHzN2tEDj4WUtztd2VkZZjT9FloGNSdQKc1Y,6876
|
|
2
|
-
cli_ih/__init__.py,sha256=X1CBDfeflUjcXik3rHCo-YLrIDe6EsH2ShnK57DDRYc,210
|
|
3
|
-
cli_ih-0.6.0.2.1.dist-info/METADATA,sha256=sztmZuw7GXAIvi-VSPXlO8SbEch9fdok1LKScvyRvik,2241
|
|
4
|
-
cli_ih-0.6.0.2.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
5
|
-
cli_ih-0.6.0.2.1.dist-info/top_level.txt,sha256=Ve1CRLNXhPyPSkpN0xLu26roh30LQCpNzkF61BZYfk0,7
|
|
6
|
-
cli_ih-0.6.0.2.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|