bear-utils 0.7.20__py3-none-any.whl → 0.7.22__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.
- bear_utils/__init__.py +24 -1
- bear_utils/ai/__init__.py +5 -5
- bear_utils/ai/ai_helpers/__init__.py +24 -18
- bear_utils/ai/ai_helpers/_parsers.py +27 -21
- bear_utils/ai/ai_helpers/_types.py +2 -7
- bear_utils/cache/__init__.py +35 -23
- bear_utils/cli/__init__.py +13 -0
- bear_utils/cli/commands.py +14 -8
- bear_utils/cli/prompt_helpers.py +40 -34
- bear_utils/cli/shell/__init__.py +1 -0
- bear_utils/cli/shell/_base_command.py +17 -18
- bear_utils/cli/shell/_base_shell.py +37 -34
- bear_utils/config/__init__.py +4 -2
- bear_utils/config/config_manager.py +193 -56
- bear_utils/config/dir_manager.py +8 -3
- bear_utils/config/settings_manager.py +94 -171
- bear_utils/constants/__init__.py +2 -1
- bear_utils/constants/_exceptions.py +6 -1
- bear_utils/constants/date_related.py +2 -0
- bear_utils/constants/logger_protocol.py +28 -0
- bear_utils/constants/time_related.py +2 -0
- bear_utils/database/__init__.py +2 -0
- bear_utils/database/_db_manager.py +10 -11
- bear_utils/events/__init__.py +3 -1
- bear_utils/events/events_class.py +11 -11
- bear_utils/events/events_module.py +17 -8
- bear_utils/extras/__init__.py +8 -6
- bear_utils/extras/_async_helpers.py +2 -3
- bear_utils/extras/_tools.py +54 -52
- bear_utils/extras/platform_utils.py +5 -1
- bear_utils/extras/responses/__init__.py +1 -0
- bear_utils/extras/responses/function_response.py +301 -0
- bear_utils/extras/wrappers/__init__.py +1 -0
- bear_utils/extras/wrappers/add_methods.py +17 -15
- bear_utils/files/__init__.py +3 -1
- bear_utils/files/file_handlers/__init__.py +2 -0
- bear_utils/files/file_handlers/_base_file_handler.py +23 -3
- bear_utils/files/file_handlers/file_handler_factory.py +38 -38
- bear_utils/files/file_handlers/json_file_handler.py +49 -22
- bear_utils/files/file_handlers/log_file_handler.py +19 -12
- bear_utils/files/file_handlers/toml_file_handler.py +13 -5
- bear_utils/files/file_handlers/txt_file_handler.py +56 -14
- bear_utils/files/file_handlers/yaml_file_handler.py +19 -13
- bear_utils/files/ignore_parser.py +52 -57
- bear_utils/graphics/__init__.py +3 -1
- bear_utils/graphics/bear_gradient.py +17 -12
- bear_utils/graphics/image_helpers.py +11 -5
- bear_utils/gui/__init__.py +7 -2
- bear_utils/gui/gui_tools/__init__.py +9 -4
- bear_utils/gui/gui_tools/_settings.py +0 -1
- bear_utils/gui/gui_tools/qt_app.py +16 -11
- bear_utils/gui/gui_tools/qt_color_picker.py +24 -13
- bear_utils/gui/gui_tools/qt_file_handler.py +30 -38
- bear_utils/gui/gui_tools/qt_input_dialog.py +11 -14
- bear_utils/logging/__init__.py +6 -4
- bear_utils/logging/logger_manager/__init__.py +1 -0
- bear_utils/logging/logger_manager/_common.py +0 -1
- bear_utils/logging/logger_manager/_console_junk.py +15 -11
- bear_utils/logging/logger_manager/_styles.py +1 -2
- bear_utils/logging/logger_manager/loggers/__init__.py +1 -0
- bear_utils/logging/logger_manager/loggers/_base_logger.py +33 -33
- bear_utils/logging/logger_manager/loggers/_base_logger.pyi +6 -5
- bear_utils/logging/logger_manager/loggers/_buffer_logger.py +2 -3
- bear_utils/logging/logger_manager/loggers/_console_logger.py +54 -26
- bear_utils/logging/logger_manager/loggers/_console_logger.pyi +7 -21
- bear_utils/logging/logger_manager/loggers/_file_logger.py +20 -13
- bear_utils/logging/logger_manager/loggers/_level_sin.py +15 -15
- bear_utils/logging/logger_manager/loggers/_logger.py +4 -6
- bear_utils/logging/logger_manager/loggers/_sub_logger.py +16 -23
- bear_utils/logging/logger_manager/loggers/_sub_logger.pyi +4 -19
- bear_utils/logging/loggers.py +9 -13
- bear_utils/monitoring/__init__.py +7 -4
- bear_utils/monitoring/_common.py +28 -0
- bear_utils/monitoring/host_monitor.py +44 -48
- bear_utils/time/__init__.py +13 -6
- {bear_utils-0.7.20.dist-info → bear_utils-0.7.22.dist-info}/METADATA +50 -7
- bear_utils-0.7.22.dist-info/RECORD +83 -0
- bear_utils-0.7.20.dist-info/RECORD +0 -79
- {bear_utils-0.7.20.dist-info → bear_utils-0.7.22.dist-info}/WHEEL +0 -0
@@ -1,3 +1,5 @@
|
|
1
|
+
"""A module for file handling dialogs using PyQt6."""
|
2
|
+
|
1
3
|
from PyQt6.QtWidgets import QFileDialog
|
2
4
|
|
3
5
|
from .qt_app import QTApplication
|
@@ -6,9 +8,8 @@ from .qt_app import QTApplication
|
|
6
8
|
class QTFileHandler(QTApplication):
|
7
9
|
"""A singleton class to handle file selection dialogs using PyQt6."""
|
8
10
|
|
9
|
-
def select_file(self, caption="Select File", directory="",
|
10
|
-
"""
|
11
|
-
Shows a file selection dialog and returns the selected file path.
|
11
|
+
def select_file(self, caption: str = "Select File", directory: str = "", fil: str = "All Files (*)"):
|
12
|
+
"""Shows a file selection dialog and returns the selected file path.
|
12
13
|
|
13
14
|
Args:
|
14
15
|
caption (str): The dialog window title
|
@@ -18,76 +19,68 @@ class QTFileHandler(QTApplication):
|
|
18
19
|
Returns:
|
19
20
|
str: Selected file path or empty string if canceled
|
20
21
|
"""
|
21
|
-
file_path, _ = QFileDialog.getOpenFileName(None, caption, directory,
|
22
|
+
file_path, _ = QFileDialog.getOpenFileName(None, caption, directory, fil)
|
22
23
|
return file_path
|
23
24
|
|
24
|
-
def select_files(self, caption="Select Files", directory="",
|
25
|
-
"""
|
26
|
-
Shows a file selection dialog that allows multiple selections.
|
25
|
+
def select_files(self, caption: str = "Select Files", directory: str = "", fil: str = "All Files (*)"):
|
26
|
+
"""Shows a file selection dialog that allows multiple selections.
|
27
27
|
|
28
28
|
Returns:
|
29
29
|
list: List of selected file paths
|
30
30
|
"""
|
31
|
-
file_paths, _ = QFileDialog.getOpenFileNames(None, caption, directory,
|
31
|
+
file_paths, _ = QFileDialog.getOpenFileNames(None, caption, directory, fil)
|
32
32
|
return file_paths
|
33
33
|
|
34
|
-
def select_directory(self, caption="Select Directory", directory=""):
|
35
|
-
"""
|
36
|
-
Shows a directory selection dialog.
|
34
|
+
def select_directory(self, caption: str = "Select Directory", directory: str = ""):
|
35
|
+
"""Shows a directory selection dialog.
|
37
36
|
|
38
37
|
Returns:
|
39
38
|
str: Selected directory path or empty string if canceled
|
40
39
|
"""
|
41
|
-
|
42
|
-
return dir_path
|
40
|
+
return QFileDialog.getExistingDirectory(None, caption, directory)
|
43
41
|
|
44
|
-
def save_file_dialog(self, caption="Save File", directory="",
|
45
|
-
"""
|
46
|
-
Shows a save file dialog.
|
42
|
+
def save_file_dialog(self, caption: str = "Save File", directory: str = "", fil: str = "All Files (*)"):
|
43
|
+
"""Shows a save file dialog.
|
47
44
|
|
48
45
|
Returns:
|
49
46
|
str: Selected save file path or empty string if canceled
|
50
47
|
"""
|
51
|
-
|
52
|
-
file_path, _ = QFileDialog.getSaveFileName(None, caption, directory, filter)
|
48
|
+
file_path, _ = QFileDialog.getSaveFileName(None, caption, directory, fil)
|
53
49
|
return file_path
|
54
50
|
|
55
51
|
|
56
|
-
def select_file(caption="Select File", directory="",
|
57
|
-
"""
|
58
|
-
Select a file using the QTApplication singleton instance.
|
52
|
+
def select_file(caption: str = "Select File", directory: str = "", fil: str = "All Files (*)"):
|
53
|
+
"""Select a file using the QTApplication singleton instance.
|
59
54
|
|
60
55
|
Args:
|
61
56
|
caption (str): The dialog window title
|
62
57
|
directory (str): The starting directory
|
63
|
-
|
58
|
+
fil (str): File filter pattern (e.g., "Images (*.png *.jpg);;Text files (*.txt)")
|
64
59
|
|
65
60
|
Returns:
|
66
61
|
str: Selected file path or empty string if canceled
|
67
62
|
"""
|
68
63
|
qt_app = QTFileHandler()
|
69
|
-
return qt_app.select_file(caption, directory,
|
64
|
+
return qt_app.select_file(caption, directory, fil)
|
70
65
|
|
71
66
|
|
72
|
-
def select_files(caption="Select Files", directory="",
|
73
|
-
"""
|
74
|
-
Select multiple files using the QTApplication singleton instance.
|
67
|
+
def select_files(caption: str = "Select Files", directory: str = "", fil: str = "All Files (*)"):
|
68
|
+
"""Select multiple files using the QTApplication singleton instance.
|
75
69
|
|
76
70
|
Args:
|
77
71
|
caption (str): The dialog window title
|
78
72
|
directory (str): The starting directory
|
79
|
-
|
73
|
+
fil (str): File filter pattern (e.g., "Images (*.png *.jpg);;Text files (*.txt)")
|
80
74
|
|
81
75
|
Returns:
|
82
76
|
list: List of selected file paths
|
83
77
|
"""
|
84
78
|
qt_app = QTFileHandler()
|
85
|
-
return qt_app.select_files(caption, directory,
|
79
|
+
return qt_app.select_files(caption, directory, fil)
|
86
80
|
|
87
81
|
|
88
|
-
def select_directory(caption="Select Directory", directory=""):
|
89
|
-
"""
|
90
|
-
Select a directory using the QTApplication singleton instance.
|
82
|
+
def select_directory(caption: str = "Select Directory", directory: str = ""):
|
83
|
+
"""Select a directory using the QTApplication singleton instance.
|
91
84
|
|
92
85
|
Args:
|
93
86
|
caption (str): The dialog window title
|
@@ -100,28 +93,27 @@ def select_directory(caption="Select Directory", directory=""):
|
|
100
93
|
return qt_app.select_directory(caption, directory)
|
101
94
|
|
102
95
|
|
103
|
-
def save_file_dialog(caption="Save File", directory="",
|
104
|
-
"""
|
105
|
-
Show a save file dialog using the QTApplication singleton instance.
|
96
|
+
def save_file_dialog(caption: str = "Save File", directory: str = "", fil: str = "All Files (*)"):
|
97
|
+
"""Show a save file dialog using the QTApplication singleton instance.
|
106
98
|
|
107
99
|
Args:
|
108
100
|
caption (str): The dialog window title
|
109
101
|
directory (str): The starting directory
|
110
|
-
|
102
|
+
fil (str): File filter pattern (e.g., "Images (*.png *.jpg);;Text files (*.txt)")
|
111
103
|
|
112
104
|
Returns:
|
113
105
|
str: Selected save file path or empty string if canceled
|
114
106
|
"""
|
115
107
|
qt_app = QTFileHandler()
|
116
|
-
return qt_app.save_file_dialog(caption, directory,
|
108
|
+
return qt_app.save_file_dialog(caption, directory, fil)
|
117
109
|
|
118
110
|
|
119
111
|
__all__ = [
|
120
112
|
"QTFileHandler",
|
113
|
+
"save_file_dialog",
|
114
|
+
"select_directory",
|
121
115
|
"select_file",
|
122
116
|
"select_files",
|
123
|
-
"select_directory",
|
124
|
-
"save_file_dialog",
|
125
117
|
]
|
126
118
|
|
127
119
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
"""A module for input dialogs using PyQt6."""
|
2
|
+
|
1
3
|
from PyQt6.QtCore import Qt
|
2
4
|
from PyQt6.QtWidgets import QInputDialog, QLineEdit
|
3
5
|
|
@@ -5,6 +7,8 @@ from .qt_app import QTApplication
|
|
5
7
|
|
6
8
|
|
7
9
|
class InputDialog(QTApplication):
|
10
|
+
"""A class for displaying various input dialogs using PyQt6."""
|
11
|
+
|
8
12
|
def get_text(
|
9
13
|
self,
|
10
14
|
title: str = "Input",
|
@@ -12,8 +16,7 @@ class InputDialog(QTApplication):
|
|
12
16
|
default: str = "",
|
13
17
|
echo_mode: QLineEdit.EchoMode = QLineEdit.EchoMode.Normal,
|
14
18
|
) -> str | None:
|
15
|
-
"""
|
16
|
-
Shows a text input dialog.
|
19
|
+
"""Shows a text input dialog.
|
17
20
|
|
18
21
|
Args:
|
19
22
|
title: Dialog window title
|
@@ -45,8 +48,7 @@ class InputDialog(QTApplication):
|
|
45
48
|
label: str = "Enter password:",
|
46
49
|
default: str = "",
|
47
50
|
) -> str | None:
|
48
|
-
"""
|
49
|
-
Shows a password input dialog with masked text.
|
51
|
+
"""Shows a password input dialog with masked text.
|
50
52
|
|
51
53
|
Args:
|
52
54
|
title: Dialog window title
|
@@ -72,8 +74,7 @@ class InputDialog(QTApplication):
|
|
72
74
|
max_value: int = 2147483647,
|
73
75
|
step: int = 1,
|
74
76
|
) -> int | None:
|
75
|
-
"""
|
76
|
-
Shows an integer input dialog with spinner.
|
77
|
+
"""Shows an integer input dialog with spinner.
|
77
78
|
|
78
79
|
Args:
|
79
80
|
title: Dialog window title
|
@@ -111,8 +112,7 @@ class InputDialog(QTApplication):
|
|
111
112
|
max_value: float = 2147483647.0,
|
112
113
|
decimals: int = 2,
|
113
114
|
) -> float | None:
|
114
|
-
"""
|
115
|
-
Shows a floating-point input dialog with spinner.
|
115
|
+
"""Shows a floating-point input dialog with spinner.
|
116
116
|
|
117
117
|
Args:
|
118
118
|
title: Dialog window title
|
@@ -149,8 +149,7 @@ class InputDialog(QTApplication):
|
|
149
149
|
current: int = 0,
|
150
150
|
editable: bool = False,
|
151
151
|
) -> str | None:
|
152
|
-
"""
|
153
|
-
Shows a dropdown selection dialog.
|
152
|
+
"""Shows a dropdown selection dialog.
|
154
153
|
|
155
154
|
Args:
|
156
155
|
title: Dialog window title
|
@@ -181,8 +180,7 @@ class InputDialog(QTApplication):
|
|
181
180
|
return None
|
182
181
|
|
183
182
|
def get_multiline_text(self, title: str = "Input", label: str = "Enter text:", default: str = "") -> str | None:
|
184
|
-
"""
|
185
|
-
Shows a multi-line text input dialog.
|
183
|
+
"""Shows a multi-line text input dialog.
|
186
184
|
|
187
185
|
Args:
|
188
186
|
title: Dialog window title
|
@@ -229,8 +227,7 @@ def get_api_key(
|
|
229
227
|
default: str = "",
|
230
228
|
service_name: str = "",
|
231
229
|
) -> str | None:
|
232
|
-
"""
|
233
|
-
Shows a secure input dialog optimized for entering API keys.
|
230
|
+
"""Shows a secure input dialog optimized for entering API keys.
|
234
231
|
|
235
232
|
Args:
|
236
233
|
title: Dialog window title
|
bear_utils/logging/__init__.py
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
"""Logging utilities for Bear Utils."""
|
2
|
+
|
1
3
|
from .logger_manager._common import VERBOSE_CONSOLE_FORMAT
|
2
4
|
from .logger_manager._styles import VERBOSE
|
3
5
|
from .loggers import (
|
@@ -12,14 +14,14 @@ from .loggers import (
|
|
12
14
|
)
|
13
15
|
|
14
16
|
__all__ = [
|
17
|
+
"VERBOSE",
|
18
|
+
"VERBOSE_CONSOLE_FORMAT",
|
15
19
|
"BaseLogger",
|
16
|
-
"ConsoleLogger",
|
17
20
|
"BufferLogger",
|
18
|
-
"
|
21
|
+
"ConsoleLogger",
|
19
22
|
"FileLogger",
|
23
|
+
"SubConsoleLogger",
|
20
24
|
"get_console",
|
21
25
|
"get_logger",
|
22
26
|
"get_sub_logger",
|
23
|
-
"VERBOSE",
|
24
|
-
"VERBOSE_CONSOLE_FORMAT",
|
25
27
|
]
|
@@ -0,0 +1 @@
|
|
1
|
+
"""Logging Manager for Bear Utils."""
|
@@ -1,14 +1,16 @@
|
|
1
|
-
import threading
|
2
1
|
from collections.abc import Callable
|
3
2
|
from io import StringIO
|
4
3
|
from logging import Formatter, Handler, LogRecord
|
5
4
|
from logging.handlers import BufferingHandler
|
5
|
+
import threading
|
6
|
+
from typing import Any
|
6
7
|
|
7
8
|
from prompt_toolkit import print_formatted_text
|
8
9
|
from prompt_toolkit.output.defaults import create_output
|
9
10
|
from rich.text import Text
|
10
11
|
|
11
|
-
from
|
12
|
+
from bear_utils.constants.date_related import DATE_TIME_FORMAT
|
13
|
+
|
12
14
|
from ._common import SIMPLE_FORMAT, VERBOSE_CONSOLE_FORMAT, ExecValues
|
13
15
|
from ._styles import LoggerExtraInfo
|
14
16
|
|
@@ -34,14 +36,13 @@ def extract_exec_info(record: LogRecord) -> dict[str, ExecValues] | None:
|
|
34
36
|
|
35
37
|
|
36
38
|
class ConsoleHandler(Handler):
|
37
|
-
def __init__(self, print_func, buffer_output):
|
39
|
+
def __init__(self, print_func: Callable, buffer_output: Callable):
|
38
40
|
super().__init__()
|
39
41
|
self.print_func: Callable = print_func
|
40
42
|
self.buffer_func: Callable = buffer_output
|
41
43
|
|
42
|
-
def emit(self, record: LogRecord, return_str: bool = False):
|
43
|
-
"""
|
44
|
-
Emit a log record either to console or return as string.
|
44
|
+
def emit(self, record: LogRecord, return_str: bool = False) -> Any:
|
45
|
+
"""Emit a log record either to console or return as string.
|
45
46
|
|
46
47
|
Args:
|
47
48
|
record: The LogRecord to emit
|
@@ -53,13 +54,13 @@ class ConsoleHandler(Handler):
|
|
53
54
|
formatted_msg: str = self.format(record)
|
54
55
|
extra: LoggerExtraInfo = get_extra(record)
|
55
56
|
exec_values: dict[str, ExecValues] | None = extract_exec_info(record)
|
56
|
-
exc_info: bool =
|
57
|
+
exc_info: bool = bool(exec_values)
|
57
58
|
style_name = extra.get("style_name", "")
|
58
59
|
|
59
60
|
print_kwargs = {
|
60
61
|
"msg": formatted_msg,
|
61
62
|
"style": style_name,
|
62
|
-
"exc_info": exc_info
|
63
|
+
"exc_info": bool(exc_info),
|
63
64
|
"exec_values": exec_values,
|
64
65
|
"return_str": return_str,
|
65
66
|
}
|
@@ -97,11 +98,14 @@ class ConsoleBuffering(BufferingHandler):
|
|
97
98
|
def __init__(
|
98
99
|
self,
|
99
100
|
capacity: int = 9999,
|
100
|
-
console_handler
|
101
|
+
console_handler: ConsoleHandler | None = None,
|
101
102
|
return_auto: bool = False,
|
102
103
|
):
|
103
104
|
super().__init__(capacity=capacity)
|
104
|
-
|
105
|
+
# should come with a formatter before getting here
|
106
|
+
self.console_handler: ConsoleHandler = console_handler or ConsoleHandler(
|
107
|
+
print_func=lambda **kwargs: None, buffer_output=lambda **kwargs: ""
|
108
|
+
)
|
105
109
|
self._lock = threading.RLock()
|
106
110
|
self.flush_auto = return_auto
|
107
111
|
|
@@ -118,7 +122,7 @@ class ConsoleBuffering(BufferingHandler):
|
|
118
122
|
self.buffer.clear()
|
119
123
|
return Text.from_ansi(output)
|
120
124
|
|
121
|
-
def trigger_flush(self):
|
125
|
+
def trigger_flush(self) -> None:
|
122
126
|
"""Immediately flush all buffered records to console."""
|
123
127
|
self.flush()
|
124
128
|
|
@@ -71,8 +71,7 @@ LOGGER_METHODS: dict[str, LoggerExtraInfo] = {
|
|
71
71
|
|
72
72
|
|
73
73
|
def get_method(name: str) -> LoggerExtraInfo:
|
74
|
-
"""
|
75
|
-
Get the name info from the logger methods.
|
74
|
+
"""Get the name info from the logger methods.
|
76
75
|
|
77
76
|
Args:
|
78
77
|
name (str): The name of the logger method.
|
@@ -0,0 +1 @@
|
|
1
|
+
"""A module with all of the loggers used in the logger manager."""
|
@@ -1,7 +1,7 @@
|
|
1
|
-
import sys
|
2
1
|
from functools import partial
|
3
2
|
from io import StringIO
|
4
|
-
|
3
|
+
import sys
|
4
|
+
from typing import Any, Self
|
5
5
|
|
6
6
|
from prompt_toolkit.formatted_text import ANSI, FormattedText, to_formatted_text
|
7
7
|
from prompt_toolkit.shortcuts import print_formatted_text
|
@@ -11,8 +11,9 @@ from rich.theme import Theme
|
|
11
11
|
from rich.traceback import Traceback
|
12
12
|
from singleton_base import SingletonBase
|
13
13
|
|
14
|
-
from
|
15
|
-
from
|
14
|
+
from bear_utils.logging.logger_manager._common import ExecValues, StackLevelTracker
|
15
|
+
from bear_utils.logging.logger_manager._styles import DEFAULT_THEME, LOGGER_METHODS, LoggerExtraInfo
|
16
|
+
|
16
17
|
from ._level_sin import INFO, add_level_name, check_level, lvl_exists
|
17
18
|
from ._sub_logger import SubConsoleLogger
|
18
19
|
|
@@ -20,14 +21,14 @@ from ._sub_logger import SubConsoleLogger
|
|
20
21
|
class BaseLogger(SingletonBase):
|
21
22
|
def __init__(
|
22
23
|
self,
|
23
|
-
output_handler=None,
|
24
|
+
output_handler: Any = None,
|
24
25
|
theme: Theme | None = None,
|
25
26
|
style_disabled: bool = False,
|
26
27
|
logger_mode: bool = False,
|
27
28
|
**kwargs,
|
28
29
|
) -> None:
|
29
|
-
"""
|
30
|
-
|
30
|
+
"""Initialize the BaseLogger with an optional style_disabled flag.
|
31
|
+
|
31
32
|
This flag can be used to disable styled output in the logger.
|
32
33
|
"""
|
33
34
|
self.output_handler = output_handler or self._default_output
|
@@ -37,7 +38,7 @@ class BaseLogger(SingletonBase):
|
|
37
38
|
self.theme: Theme = DEFAULT_THEME if theme is None else theme
|
38
39
|
self.style_disabled: bool = style_disabled
|
39
40
|
self.console: Console = self.get_console(self.theme, style_disabled)
|
40
|
-
self.console_buffer: StringIO = self.console.file # type: ignore
|
41
|
+
self.console_buffer: StringIO = self.console.file # type: ignore[assignment]
|
41
42
|
self.backup_console = Console(theme=self.theme, highlight=True, force_terminal=True)
|
42
43
|
self._generate_style_methods()
|
43
44
|
|
@@ -60,21 +61,21 @@ class BaseLogger(SingletonBase):
|
|
60
61
|
)
|
61
62
|
return console
|
62
63
|
|
63
|
-
def _default_output(self, msg: object,
|
64
|
+
def _default_output(self, msg: object, _: LoggerExtraInfo, *args, **kwargs) -> None:
|
64
65
|
"""Default output handler that prints to console."""
|
65
66
|
if not self.logger_mode:
|
66
67
|
self.print(msg, *args, **kwargs)
|
67
68
|
|
68
69
|
def __enter__(self) -> Self:
|
69
|
-
"""
|
70
|
-
|
70
|
+
"""Enter the context manager, returning the ConsoleLogger instance.
|
71
|
+
|
71
72
|
This allows for using the logger in a with statement.
|
72
73
|
"""
|
73
74
|
return self
|
74
75
|
|
75
|
-
def __exit__(self, exc_type, exc_value, traceback) -> None:
|
76
|
-
"""
|
77
|
-
|
76
|
+
def __exit__(self, exc_type: object, exc_value: object, traceback: object) -> None:
|
77
|
+
"""Exit the context manager, cleaning up resources.
|
78
|
+
|
78
79
|
This is called when the with statement is exited.
|
79
80
|
"""
|
80
81
|
self.exit()
|
@@ -83,8 +84,8 @@ class BaseLogger(SingletonBase):
|
|
83
84
|
return "No buffering handler available."
|
84
85
|
|
85
86
|
def set_base_level(self, level: int | str) -> None:
|
86
|
-
"""
|
87
|
-
|
87
|
+
"""Set the logging level for the console.
|
88
|
+
|
88
89
|
This method allows changing the logging level dynamically.
|
89
90
|
|
90
91
|
This isn't actually a logging logger so we are having to add this while avoiding messing with
|
@@ -96,8 +97,8 @@ class BaseLogger(SingletonBase):
|
|
96
97
|
self._level = check_level(level)
|
97
98
|
|
98
99
|
def filter_by_level(self, level: str | int) -> bool:
|
99
|
-
"""
|
100
|
-
|
100
|
+
"""Filter method to determine if a message should be logged based on its level.
|
101
|
+
|
101
102
|
This method checks if the provided level is greater than or equal to the logger's level.
|
102
103
|
|
103
104
|
Args:
|
@@ -135,13 +136,13 @@ class BaseLogger(SingletonBase):
|
|
135
136
|
self,
|
136
137
|
msg: object,
|
137
138
|
end: str = "\n",
|
138
|
-
exc_info=None,
|
139
|
+
exc_info: object | None = None,
|
139
140
|
extra: dict | None = None,
|
140
|
-
*
|
141
|
+
*_,
|
141
142
|
**kwargs,
|
142
143
|
) -> None | str:
|
143
|
-
"""
|
144
|
-
|
144
|
+
"""Print a message to the console with the specified formatting.
|
145
|
+
|
145
146
|
This method allows for printing messages with additional context and formatting.
|
146
147
|
"""
|
147
148
|
if exc_info is not None:
|
@@ -165,28 +166,27 @@ class BaseLogger(SingletonBase):
|
|
165
166
|
indent: int = 2,
|
166
167
|
sort: bool = False,
|
167
168
|
**kwargs,
|
168
|
-
):
|
169
|
+
) -> None:
|
169
170
|
"""Just a pass-through to the console.print_json method."""
|
170
171
|
self.console.print_json(json=json, data=data, indent=indent, sort_keys=sort, **kwargs)
|
171
172
|
|
172
|
-
def raw_print(self, msg: object, end: str = "\n", *
|
173
|
-
"""
|
174
|
-
Use the underlying console directly and bypass all the extra formatting and handling.
|
173
|
+
def raw_print(self, msg: object, end: str = "\n", *_, **kwargs) -> None:
|
174
|
+
"""Use the underlying console directly and bypass all the extra formatting and handling.
|
175
175
|
|
176
176
|
Args:
|
177
177
|
msg (object): The message to print.
|
178
|
-
end (str, optional): The string appended after the message. Defaults to
|
178
|
+
end (str, optional): The string appended after the message. Defaults to new line.
|
179
179
|
"""
|
180
180
|
self.backup_console.print(msg, end=end, style=kwargs.pop("style", "white"), **kwargs)
|
181
181
|
|
182
182
|
def _print(self, msg: object, end: str, json: bool = False, *args, **kwargs) -> None:
|
183
|
-
"""
|
184
|
-
|
183
|
+
"""Print a message to the console with the specified formatting.
|
184
|
+
|
185
185
|
This method allows for printing messages with additional context and formatting.
|
186
186
|
"""
|
187
187
|
try:
|
188
188
|
if json:
|
189
|
-
self.print_json(
|
189
|
+
self.print_json(msg, *args, **kwargs) # type: ignore[arg-type]
|
190
190
|
else:
|
191
191
|
self.console.print(msg, end="", style=kwargs.pop("style", "white"))
|
192
192
|
formatted_text: FormattedText = to_formatted_text(ANSI(self.console_buffer.getvalue()))
|
@@ -199,7 +199,7 @@ class BaseLogger(SingletonBase):
|
|
199
199
|
)
|
200
200
|
self._reset_buffer()
|
201
201
|
|
202
|
-
def _extract_exception_values(self, exc_info) -> ExecValues | None:
|
202
|
+
def _extract_exception_values(self, exc_info: Any) -> ExecValues | None:
|
203
203
|
"""Extract exception values in a clean, reusable way."""
|
204
204
|
if isinstance(exc_info, BaseException):
|
205
205
|
exc_tuple = (type(exc_info), exc_info, exc_info.__traceback__)
|
@@ -231,8 +231,8 @@ class BaseLogger(SingletonBase):
|
|
231
231
|
self.console_buffer.seek(0)
|
232
232
|
|
233
233
|
def exit(self) -> None:
|
234
|
-
"""
|
235
|
-
|
234
|
+
"""Exit the console logger.
|
235
|
+
|
236
236
|
This method is called when the program exits to clean up resources.
|
237
237
|
"""
|
238
238
|
self.console_buffer.close()
|
@@ -7,7 +7,8 @@ from rich.theme import Theme
|
|
7
7
|
from rich.traceback import Traceback
|
8
8
|
from singleton_base import SingletonBase
|
9
9
|
|
10
|
-
from
|
10
|
+
from bear_utils.logging.logger_manager._common import ExecValues, StackLevelTracker
|
11
|
+
|
11
12
|
from ._sub_logger import SubConsoleLogger
|
12
13
|
|
13
14
|
class BaseLogger(SingletonBase):
|
@@ -21,22 +22,22 @@ class BaseLogger(SingletonBase):
|
|
21
22
|
logger_mode: bool
|
22
23
|
sub_logger: dict[str, SubConsoleLogger]
|
23
24
|
# fmt: off
|
24
|
-
def __init__(self, output_handler = None, theme: Theme | None = ..., style_disabled: bool = ..., logger_mode: bool = ..., **kwargs) -> None: ...
|
25
|
+
def __init__(self, output_handler: Any = None, theme: Theme | None = ..., style_disabled: bool = ..., logger_mode: bool = ..., **kwargs) -> None: ...
|
25
26
|
@staticmethod
|
26
27
|
def get_console(theme: Theme, style_disabled: bool) -> Console: ...
|
27
28
|
def __enter__(self) -> Self: ...
|
28
|
-
def __exit__(self, exc_type:
|
29
|
+
def __exit__(self, exc_type: object, exc_value: object, traceback: object) -> None: ...
|
29
30
|
def _generate_style_methods(self, **kwargs) -> None: ...
|
30
31
|
def get_sub_logger(self, namespace: str, **kwargs: Any) -> SubConsoleLogger: ...
|
31
32
|
def replacement_method(self, msg: object, *args: Any, **kwargs: Any) -> None: ...
|
32
|
-
def print(self, msg: object, end: str="\n", exc_info=None, extra: dict | None = None, *args, **kwargs) -> None | str: ...
|
33
|
+
def print(self, msg: object, end: str="\n", exc_info:str|None=None, extra: dict | None = None, *args, **kwargs) -> None | str: ...
|
33
34
|
def print_json(self, data: Any | None = ..., indent: int = ..., sort: bool = ...) -> None: ...
|
34
35
|
def set_base_level(self, level: int) -> None: ...
|
35
36
|
def filter_by_level(self, level: int) -> bool: ...
|
36
37
|
def trigger_buffer_flush(self) -> str | Text: ...
|
37
38
|
def _print(self, msg: object, end: str, json: bool = ..., *args: Any, **kwargs: Any) -> None: ...
|
38
39
|
def _get_exception(self, manual: bool = ..., exec_values: ExecValues | None = ...) -> Traceback: ...
|
39
|
-
def _extract_exception_values(self, exc_info) -> ExecValues | None: ...
|
40
|
+
def _extract_exception_values(self, exc_info: str | None) -> ExecValues | None: ...
|
40
41
|
def _reset_buffer(self) -> None: ...
|
41
42
|
def exit(self) -> None: ...
|
42
43
|
def debug(self, msg: object, *args: Any, **kwargs: Any) -> None: ...
|
@@ -8,8 +8,7 @@ from ._sub_logger import SubConsoleLogger
|
|
8
8
|
|
9
9
|
|
10
10
|
class BufferLogger(ConsoleLogger):
|
11
|
-
"""
|
12
|
-
A buffer-based logger that writes styled log messages to a buffer.
|
11
|
+
"""A buffer-based logger that writes styled log messages to a buffer.
|
13
12
|
|
14
13
|
Combines Python's logging framework with Rich console styling, but outputs
|
15
14
|
to a buffer instead of console or file. Supports buffering of log messages,
|
@@ -34,7 +33,7 @@ class BufferLogger(ConsoleLogger):
|
|
34
33
|
theme: Theme | None = None,
|
35
34
|
name: str = "BufferLogger",
|
36
35
|
level: int = DEBUG,
|
37
|
-
*
|
36
|
+
*_,
|
38
37
|
**kwargs,
|
39
38
|
) -> None:
|
40
39
|
ConsoleLogger.__init__(
|