cli-ih 0.5.3.3__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cli_ih
3
- Version: 0.5.3.3
3
+ Version: 0.5.3.4
4
4
  Summary: A background command handler for python's command line interface.
5
5
  Author-email: Hotment <michatchuplay@gmail.com>
6
6
  Requires-Python: >=3.8
@@ -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
- #logging.basicConfig(level=logging.INFO, format='[%(asctime)s | %(levelname)s]: %(message)s', datefmt='%B %d %H:%M:%S')
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
- logging.warning(f"Command '{name}' not found.")
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
- logging.warning(f"Unknown command: '{command_name}'")
89
+ self.__warning(f"Unknown command: '{command_name}'")
85
90
  except EOFError:
86
- logging.error("Input ended unexpectedly.")
91
+ self.__error("Input ended unexpectedly.")
87
92
  break
88
93
  except KeyboardInterrupt:
89
- logging.error("Input interrupted.")
94
+ self.__error("Input interrupted.")
90
95
  break
91
96
  except HandlerClosed:
92
- logging.info("Input Handler exited.")
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 = logging.getLogger()
114
+ logger = self.logger
110
115
  if logger.getEffectiveLevel() == logging.DEBUG:
111
116
  logger.setLevel(logging.INFO)
112
- logging.info("Debug mode is now off")
117
+ self.__info("Debug mode is now off")
113
118
  else:
114
119
  logger.setLevel(logging.DEBUG)
115
- logging.debug("Debug mode is now on")
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,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cli_ih
3
- Version: 0.5.3.3
3
+ Version: 0.5.3.4
4
4
  Summary: A background command handler for python's command line interface.
5
5
  Author-email: Hotment <michatchuplay@gmail.com>
6
6
  Requires-Python: >=3.8
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "cli_ih"
3
- version = "0.5.3.3"
3
+ version = "0.5.3.4"
4
4
  description = "A background command handler for python's command line interface."
5
5
  readme = "README.md"
6
6
  requires-python = ">= 3.8"
@@ -5,7 +5,7 @@ with open("README.md", "r") as f:
5
5
 
6
6
  setup(
7
7
  name="cli_ih",
8
- version = "0.5.3.3",
8
+ version = "0.5.3.4",
9
9
  packages=find_packages(),
10
10
  install_requires = [
11
11
  'logging>=0.4.9.6'
@@ -1,2 +0,0 @@
1
- from .Input_Handler import InputHandler
2
- from .Input_Handler import logging
File without changes
File without changes