Skip to content

Commit c085859

Browse files
authored
Merge pull request #6 from desultory/dev
make imports more backwards comptible
2 parents 340aaef + edef09f commit c085859

File tree

9 files changed

+52
-54
lines changed

9 files changed

+52
-54
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "zenlib"
7-
version = "3.0.0"
7+
version = "3.0.1"
88
authors = [
99
{ name="Desultory", email="[email protected]" },
1010
]

src/zenlib/logging/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from .colorlognameformatter import ColorLognameFormatter
2+
from .classlogger import ClassLogger
23
from .loggify import loggify
4+
from .loggermixin import LoggerMixIn
35
from .log_call import log_call
4-
from .classlogger import ClassLogger
56

6-
__all__ = ['ColorLognameFormatter', 'loggify', 'log_call', 'ClassLogger']
7+
__all__ = ["ColorLognameFormatter", "loggify", "LoggerMixIn", "log_call", "ClassLogger"]

src/zenlib/logging/classlogger.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
11
__author__ = "desultory"
2-
__version__ = "2.2.0"
2+
__version__ = "3.0.0"
33

4-
from .utils import add_handler_if_not_exists, log_init, handle_additional_logging
4+
from .loggermixin import LoggerMixIn
55

6-
from logging import Logger, getLogger
76

8-
9-
class ClassLogger:
7+
class ClassLogger(LoggerMixIn):
108
def __init__(self, *args, **kwargs):
11-
# Get the parent logger from the root if one was not passed
12-
parent_logger = kwargs.pop('logger') if isinstance(kwargs.get('logger'), Logger) else getLogger()
13-
# Get a child logger from the parent logger, set self.logger
14-
self.logger = parent_logger.getChild(self.__class__.__name__)
15-
# Bump the log level if _log_bump is passed
16-
self.logger.setLevel(self.logger.parent.level + kwargs.pop('_log_bump', 0))
17-
18-
# Add a colored stream handler if one does not exist
19-
add_handler_if_not_exists(self.logger)
20-
21-
# Log class init if _log_init is passed
22-
log_init(self, args, kwargs)
23-
24-
# Add logging to _log_setattr if set
25-
handle_additional_logging(self, kwargs)
9+
self.init_logger(args, kwargs)
2610

2711
if super().__class__.__class__ is not type:
2812
super().__init__(*args, **kwargs)

src/zenlib/logging/colorlognameformatter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
__author__ = "desultory"
2-
__version__ = "3.0.0"
2+
__version__ = "3.1.0"
33

44
from logging import Formatter
55

6-
from zenlib.util import colorize
7-
86

97
class ColorLognameFormatter(Formatter):
108
"""A logging formatter which colors the levelname of the log message.
119
Uses the zenlib.util.colorize function to color the levelname.
1210
Normal levelnames are padded to the length of the longest levelname."""
1311

1412
level_colors = {
15-
"DEBUG": {"color": "white"},
13+
"DEBUG": {"color": "white", "bright": True},
1614
"INFO": {"color": "blue"},
1715
"WARNING": {"color": "yellow"},
1816
"ERROR": {"color": "red", "bold": True},
@@ -21,11 +19,13 @@ class ColorLognameFormatter(Formatter):
2119

2220
def __init__(self, fmt="%(levelname)s | %(message)s", *args, **kwargs):
2321
super().__init__(fmt, *args, **kwargs)
24-
self.level_str_width = max(len(name) for name in self.level_colors) - 1
22+
self.level_str_width = max(len(name) for name in self.level_colors)
2523

2624
def format(self, record):
2725
# When calling format, replace the levelname with a colored version
2826
# Note: the string size is greatly increased because of the color codes
27+
from zenlib.util import colorize
28+
2929
old_levelname = record.levelname
3030
color_info = self.level_colors.get(record.levelname, {"color": "white"})
3131
record.levelname = colorize(record.levelname.ljust(self.level_str_width), **color_info)

src/zenlib/logging/loggermixin.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
__author__ = "desultory"
2+
__version__ = "1.2.0"
3+
4+
from logging import Logger, getLogger
5+
6+
from .utils import add_handler_if_not_exists, handle_additional_logging, log_init
7+
8+
9+
class LoggerMixIn:
10+
def init_logger(self, args, kwargs):
11+
# Get the parent logger from the root if one was not passed
12+
parent_logger = kwargs.pop("logger") if isinstance(kwargs.get("logger"), Logger) else getLogger()
13+
# Get a child logger from the parent logger, set self.logger
14+
self.logger = parent_logger.getChild(self.__class__.__name__)
15+
# Bump the log level if _log_bump is passed
16+
self.logger.setLevel(self.logger.parent.level + kwargs.pop("_log_bump", 0))
17+
18+
# Add a colored stream handler if one does not exist
19+
add_handler_if_not_exists(self.logger)
20+
21+
# Log class init if _log_init is passed
22+
log_init(self, args, kwargs)
23+
24+
# Add logging to _log_setattr if set
25+
handle_additional_logging(self, kwargs)

src/zenlib/logging/loggify.py

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,15 @@
11
__author__ = "desultory"
2-
__version__ = "2.4.2"
3-
4-
from logging import Logger, getLogger
2+
__version__ = "3.0.0"
53

64
from zenlib.util import merge_class
75

8-
from .utils import add_handler_if_not_exists, log_init, handle_additional_logging
6+
from .loggermixin import LoggerMixIn
97

108

119
def loggify(cls):
12-
class ClassLogger(cls):
10+
class ClassLogger(cls, LoggerMixIn):
1311
def __init__(self, *args, **kwargs):
14-
# Get the parent logger from the root if one was not passed
15-
parent_logger = kwargs.pop("logger") if isinstance(kwargs.get("logger"), Logger) else getLogger()
16-
# Get a child logger from the parent logger, set self.logger
17-
self.logger = parent_logger.getChild(cls.__name__)
18-
# Bump the log level if _log_bump is passed
19-
self.logger.setLevel(self.logger.parent.level + kwargs.pop("_log_bump", 0))
20-
21-
# Add a colored stream handler if one does not exist
22-
add_handler_if_not_exists(self.logger)
23-
24-
# Log class init if _log_init is passed
25-
log_init(self, args, kwargs)
26-
27-
# Add logging to _log_setattr if set
28-
handle_additional_logging(self, kwargs)
29-
12+
self.init_logger(args, kwargs)
3013
super().__init__(*args, **kwargs)
3114

3215
merge_class(cls, ClassLogger, ignored_attributes=["__setattr__"])

src/zenlib/types/validated_dataclass.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from dataclasses import dataclass
22
from typing import get_type_hints
33

4-
from zenlib.util import merge_class
5-
from zenlib.logging import loggify
6-
74

85
def validatedDataclass(cls):
6+
from zenlib.logging import loggify
7+
from zenlib.util import merge_class
8+
99
cls = loggify(dataclass(cls))
1010

1111
class ValidatedDataclass(cls):
@@ -28,5 +28,5 @@ def _validate_attribute(self, attribute, value):
2828
raise TypeError(f"[{attribute}] Type mismatch: '{expected_type}' != {type(value)}")
2929
return value
3030

31-
merge_class(cls, ValidatedDataclass, ignored_attributes = ["__setattr__"])
31+
merge_class(cls, ValidatedDataclass, ignored_attributes=["__setattr__"])
3232
return ValidatedDataclass

src/zenlib/util/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from .dict_check import contains, unset
21
from .colorize import colorize
2+
from .dict_check import contains, unset
33
from .handle_plural import handle_plural
44
from .main_funcs import get_args_n_logger, get_kwargs, get_kwargs_from_args, init_argparser, init_logger, process_args
55
from .merge_class import merge_class
66
from .pretty_print import pretty_print
77
from .replace_file_line import replace_file_line
8+
from ..types import NoDupFlatList
89

910
__all__ = [
1011
"handle_plural",

tests/test_nodupflatlist.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55

66
class TestNoDupFlatList(TestCase):
7+
def test_util_import(self):
8+
from zenlib.util import NoDupFlatList as NDFL
9+
self.assertEqual(NoDupFlatList, NDFL)
10+
711
def test_dedup1(self):
812
test_list = NoDupFlatList(no_warn=True)
913
test_list += 1

0 commit comments

Comments
 (0)