cli-ih 0.5.3.2__tar.gz → 0.5.3.4__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.
- {cli_ih-0.5.3.2 → cli_ih-0.5.3.4}/PKG-INFO +1 -1
- {cli_ih-0.5.3.2 → cli_ih-0.5.3.4}/cli_ih/Input_Handler.py +38 -34
- cli_ih-0.5.3.4/cli_ih/__init__.py +1 -0
- {cli_ih-0.5.3.2 → cli_ih-0.5.3.4}/cli_ih.egg-info/PKG-INFO +1 -1
- {cli_ih-0.5.3.2 → cli_ih-0.5.3.4}/pyproject.toml +1 -1
- {cli_ih-0.5.3.2 → cli_ih-0.5.3.4}/setup.py +1 -1
- cli_ih-0.5.3.2/cli_ih/__init__.py +0 -1
- {cli_ih-0.5.3.2 → cli_ih-0.5.3.4}/README.md +0 -0
- {cli_ih-0.5.3.2 → cli_ih-0.5.3.4}/cli_ih.egg-info/SOURCES.txt +0 -0
- {cli_ih-0.5.3.2 → cli_ih-0.5.3.4}/cli_ih.egg-info/dependency_links.txt +0 -0
- {cli_ih-0.5.3.2 → cli_ih-0.5.3.4}/cli_ih.egg-info/top_level.txt +0 -0
- {cli_ih-0.5.3.2 → cli_ih-0.5.3.4}/setup.cfg +0 -0
@@ -4,38 +4,43 @@ import logging
|
|
4
4
|
class HandlerClosed(Exception): ...
|
5
5
|
class MissingParameter(Exception): ...
|
6
6
|
|
7
|
-
class CustomFormatter(logging.Formatter):
|
8
|
-
"""Custom formatter to add colors to log levels."""
|
9
|
-
|
10
|
-
LEVEL_COLORS = {
|
11
|
-
logging.DEBUG: "\033[34m",
|
12
|
-
logging.INFO: "\033[0m",
|
13
|
-
logging.WARNING: "\033[33m",
|
14
|
-
logging.ERROR: "\033[31m",
|
15
|
-
logging.CRITICAL: "\033[37;41m"
|
16
|
-
}
|
17
|
-
|
18
|
-
def format(self, record):
|
19
|
-
log_color = self.LEVEL_COLORS.get(record.levelno, "\033[0m")
|
20
|
-
log_message = super().format(record)
|
21
|
-
return f"{log_color}{log_message}\033[0m"
|
22
|
-
|
23
|
-
def setup_logging():
|
24
|
-
log_format = '[%(asctime)s | %(levelname)s]: %(message)s'
|
25
|
-
handler = logging.StreamHandler()
|
26
|
-
handler.setFormatter(CustomFormatter(log_format))
|
27
|
-
logging.basicConfig(level=logging.INFO, handlers=[handler], datefmt='%B %d %H:%M:%S')
|
28
|
-
|
29
7
|
class InputHandler:
|
30
|
-
|
31
|
-
def __init__(self, thread_mode = True, cursor = ""):
|
8
|
+
def __init__(self, thread_mode = True, cursor = "", logger: logging.Logger | None = None):
|
32
9
|
self.commands = {}
|
33
10
|
self.is_running = False
|
34
11
|
self.thread_mode = thread_mode
|
35
12
|
self.cursor = f"{cursor.strip()} "
|
36
13
|
self.thread = None
|
14
|
+
self.logger = logger
|
37
15
|
self.register_default_commands()
|
38
16
|
|
17
|
+
def get_logger(self):
|
18
|
+
return self.logger
|
19
|
+
|
20
|
+
def __debug(self, msg: str):
|
21
|
+
if self.logger:
|
22
|
+
self.__debug(msg)
|
23
|
+
else:
|
24
|
+
print(f"[DEBUG]: {msg}")
|
25
|
+
|
26
|
+
def __info(self, msg: str):
|
27
|
+
if self.logger:
|
28
|
+
self.__info(msg)
|
29
|
+
else:
|
30
|
+
print(f"[INFO]: {msg}")
|
31
|
+
|
32
|
+
def __warning(self, msg: str):
|
33
|
+
if self.logger:
|
34
|
+
self.__warning(msg)
|
35
|
+
else:
|
36
|
+
print(f"[WARNING]: {msg}")
|
37
|
+
|
38
|
+
def __error(self, msg: str):
|
39
|
+
if self.logger:
|
40
|
+
self.__error(msg)
|
41
|
+
else:
|
42
|
+
print(f"[ERROR]: {msg}")
|
43
|
+
|
39
44
|
def register_command(self, name: str, func: Callable, description: str = ""):
|
40
45
|
"""Registers a command with its associated function."""
|
41
46
|
if not description:
|
@@ -64,7 +69,7 @@ class InputHandler:
|
|
64
69
|
else:
|
65
70
|
raise ValueError(f"The command '{name}' is not callable.")
|
66
71
|
else:
|
67
|
-
|
72
|
+
self.__warning(f"Command '{name}' not found.")
|
68
73
|
|
69
74
|
|
70
75
|
def _thread():
|
@@ -81,15 +86,15 @@ class InputHandler:
|
|
81
86
|
if command_name in self.commands:
|
82
87
|
run_command(self.commands, command_name, args)
|
83
88
|
else:
|
84
|
-
|
89
|
+
self.__warning(f"Unknown command: '{command_name}'")
|
85
90
|
except EOFError:
|
86
|
-
|
91
|
+
self.__error("Input ended unexpectedly.")
|
87
92
|
break
|
88
93
|
except KeyboardInterrupt:
|
89
|
-
|
94
|
+
self.__error("Input interrupted.")
|
90
95
|
break
|
91
96
|
except HandlerClosed:
|
92
|
-
|
97
|
+
self.__info("Input Handler exited.")
|
93
98
|
break
|
94
99
|
self.is_running = False
|
95
100
|
if self.thread_mode:
|
@@ -106,17 +111,16 @@ class InputHandler:
|
|
106
111
|
print(str_out)
|
107
112
|
|
108
113
|
def debug_mode(args):
|
109
|
-
logger =
|
114
|
+
logger = self.logger
|
110
115
|
if logger.getEffectiveLevel() == logging.DEBUG:
|
111
116
|
logger.setLevel(logging.INFO)
|
112
|
-
|
117
|
+
self.__info("Debug mode is now off")
|
113
118
|
else:
|
114
119
|
logger.setLevel(logging.DEBUG)
|
115
|
-
|
120
|
+
self.__debug("Debug mode is now on")
|
116
121
|
|
117
122
|
def exit_thread(args):
|
118
123
|
raise HandlerClosed
|
119
124
|
self.register_command("help", lambda args: help(self.commands), "Displays all the available commands")
|
120
125
|
self.register_command("debug", debug_mode, "Changes the logging level to DEBUG.")
|
121
|
-
self.register_command("exit", exit_thread, "Exits the Input Handler irreversibly.")
|
122
|
-
setup_logging()
|
126
|
+
self.register_command("exit", exit_thread, "Exits the Input Handler irreversibly.")
|
@@ -0,0 +1 @@
|
|
1
|
+
from .Input_Handler import InputHandler
|
@@ -1 +0,0 @@
|
|
1
|
-
from Input_Handler import InputHandler
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|