Skip to content

Commit f754c13

Browse files
author
dmy.berezovskyi
committed
fix ruff
1 parent e4542d4 commit f754c13

1 file changed

Lines changed: 33 additions & 18 deletions

File tree

utils/logger.py

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,24 @@ def __init__(self, log_lvl: LogLevel = LogLevel.INFO) -> None:
3131
def _create_log_file(self) -> str:
3232
current_time = time.strftime("%Y-%m-%d")
3333
log_directory = os.path.abspath(
34-
os.path.join(os.path.dirname(__file__), "../tests/logs"))
34+
os.path.join(os.path.dirname(__file__), "../tests/logs")
35+
)
3536

3637
try:
37-
os.makedirs(log_directory, exist_ok=True) # Create directory if it doesn't exist
38+
os.makedirs(
39+
log_directory, exist_ok=True
40+
) # Create directory if it doesn't exist
3841
except Exception as e:
39-
raise RuntimeError(f"Failed to create log directory '{log_directory}': {e}")
42+
raise RuntimeError(
43+
f"Failed to create log directory '{log_directory}': {e}"
44+
)
4045

4146
return os.path.join(log_directory, f"log_{current_time}.log")
4247

4348
def _initialize_logging(self, log_lvl: LogLevel) -> None:
44-
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
49+
formatter = logging.Formatter(
50+
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
51+
)
4552
fh = logging.FileHandler(self.log_file, mode="w")
4653
fh.setFormatter(formatter)
4754
fh.setLevel(log_lvl.value)
@@ -50,7 +57,9 @@ def _initialize_logging(self, log_lvl: LogLevel) -> None:
5057
def get_instance(self) -> logging.Logger:
5158
return self._log
5259

53-
def annotate(self, message: str, level: Literal["info", "warn", "debug", "error"]) -> None:
60+
def annotate(
61+
self, message: str, level: Literal["info", "warn", "debug", "error"]
62+
) -> None:
5463
"""Log a message at the specified level."""
5564
if level == "info":
5665
self._log.info(message)
@@ -66,7 +75,7 @@ def annotate(self, message: str, level: Literal["info", "warn", "debug", "error"
6675

6776
def log(
6877
data: Optional[str] = None,
69-
level: Literal["info", "warn", "debug", "error"] = "info"
78+
level: Literal["info", "warn", "debug", "error"] = "info",
7079
) -> Callable:
7180
"""Decorator to log the current method's execution.
7281
@@ -83,32 +92,38 @@ def wrapper(self, *args, **kwargs) -> Any:
8392
# Raise an exception if both the docstring and data are None
8493
if method_docs is None and data is None:
8594
raise ValueError(
86-
f"No documentation available for method :: {func.__name__} and no custom log data provided."
95+
f"No documentation available for :: {func.__name__}"
8796
)
8897

8998
# Construct the parameter string for logging
90-
params_str = ', '.join(repr(arg) for arg in args)
91-
kwargs_str = ', '.join(f"{k}={v!r}" for k, v in kwargs.items())
92-
all_params_str = ', '.join(filter(None, [params_str, kwargs_str]))
93-
94-
# Log message with method documentation or custom data
95-
logs = (method_docs + f" Method :: {func.__name__}()" + f" with parameters: {all_params_str}"
96-
if method_docs else
97-
data + f" Method :: {func.__name__}()" + f" with parameters: {all_params_str}")
99+
params_str = ", ".join(repr(arg) for arg in args)
100+
kwargs_str = ", ".join(f"{k}={v!r}" for k, v in kwargs.items())
101+
all_params_str = ", ".join(filter(None, [params_str, kwargs_str]))
102+
103+
logs = (
104+
method_docs
105+
+ f" Method :: {func.__name__}()"
106+
+ f" with parameters: {all_params_str}"
107+
if method_docs
108+
else data
109+
+ f" Method :: {func.__name__}()"
110+
+ f" with parameters: {all_params_str}"
111+
)
98112

99113
logger_instance.annotate(logs, level)
100114

101115
# Call the original method, passing *args and **kwargs
102-
return func(self, *args, **kwargs) # <--- Fix: properly passing args and kwargs
116+
return func(
117+
self, *args, **kwargs
118+
)
103119

104120
return wrapper
105121

106122
return decorator
107123

108124

109-
110125
def format_method_doc_str(doc_str: Optional[str]) -> Optional[str]:
111126
"""Add a dot to the docs string if it doesn't exist."""
112-
if doc_str and not doc_str.endswith('.'):
127+
if doc_str and not doc_str.endswith("."):
113128
return doc_str + "."
114129
return doc_str

0 commit comments

Comments
 (0)