Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "zenlib"
version = "2.4.1"
version = "3.0.0"
authors = [
{ name="Desultory", email="[email protected]" },
]
Expand Down
30 changes: 0 additions & 30 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,33 +142,3 @@ If `key` is a dict, the structure will be used to walk the `validate_dict`.
* `message` Set the vailidation failure message.

Additional arguments exist to set a value to compare found keys against

## Threading

> This is not being maintained, please don't use it, it's just here because I used to use it.
### ZenThread

An extension of the builtin thread that sets results to self.return_value and exceptions to self.exception.

Supports re-starting and looping with the self.loop event.

### @threaded

Runs the wrapped function in a thread when called.

Adds the thread to `_threads` within the object.

If an exception is raised, it will be added to `self._threads` in the form `(thread, exception_queue)`.

### @thread_wrapped('threadname')

Meant to be used with `@add_thread`, the argument is the threadname that function is associated with.

### @add_thread('threadname', 'target_function', 'description')

`@add_thread` decorates a class, and adds `create_{threadname}_thread`, `start_` and `stop_` functions which are used to handle thread management of a `thread_wrapped` function.

Once added, a thread will be added to `self.threads[threadname]`.


4 changes: 3 additions & 1 deletion src/zenlib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from .logging import ColorLognameFormatter, loggify
from .util import NoDupFlatList, check_dict, handle_plural, pretty_print, replace_file_line, update_init, walk_dict
from .types import NoDupFlatList, validatedDataclass
from .util import check_dict, handle_plural, pretty_print, replace_file_line, update_init, walk_dict

__all__ = [
"ColorLognameFormatter",
"loggify",
"handle_plural",
"NoDupFlatList",
"validatedDataclass",
"pretty_print",
"replace_file_line",
"update_init",
Expand Down
14 changes: 4 additions & 10 deletions src/zenlib/logging/classlogger.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__author__ = "desultory"
__version__ = "2.1.0"
__version__ = "2.2.0"

from .utils import add_handler_if_not_exists, log_init, log_setattr
from .utils import add_handler_if_not_exists, log_init, handle_additional_logging

from logging import Logger, getLogger

Expand All @@ -21,14 +21,8 @@ def __init__(self, *args, **kwargs):
# Log class init if _log_init is passed
log_init(self, args, kwargs)

# add setattr logging
setattr(self, "__setattr__", log_setattr)
# Add logging to _log_setattr if set
handle_additional_logging(self, kwargs)

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

def __setitem__(self, name, value):
""" Add logging to dict setitem. """
if hasattr(super(), '__setitem__'):
super().__setitem__(name, value)
self.logger.log(5, "Setitem '%s' to: %s" % (name, value))
21 changes: 10 additions & 11 deletions src/zenlib/logging/loggify.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
__author__ = "desultory"
__version__ = "2.4.0"

from .utils import add_handler_if_not_exists, log_init, log_setattr
__version__ = "2.4.2"

from logging import Logger, getLogger

from zenlib.util import merge_class

from .utils import add_handler_if_not_exists, log_init, handle_additional_logging


def loggify(cls):
class ClassLogger(cls):
def __init__(self, *args, **kwargs):
# Get the parent logger from the root if one was not passed
parent_logger = kwargs.pop('logger') if isinstance(kwargs.get('logger'), Logger) else getLogger()
parent_logger = kwargs.pop("logger") if isinstance(kwargs.get("logger"), Logger) else getLogger()
# Get a child logger from the parent logger, set self.logger
self.logger = parent_logger.getChild(cls.__name__)
# Bump the log level if _log_bump is passed
self.logger.setLevel(self.logger.parent.level + kwargs.pop('_log_bump', 0))
self.logger.setLevel(self.logger.parent.level + kwargs.pop("_log_bump", 0))

# Add a colored stream handler if one does not exist
add_handler_if_not_exists(self.logger)

# Log class init if _log_init is passed
log_init(self, args, kwargs)

# Add setattr logging
setattr(self, "__setattr__", log_setattr)
# Add logging to _log_setattr if set
handle_additional_logging(self, kwargs)

super().__init__(*args, **kwargs)

ClassLogger.__name__ = cls.__name__
ClassLogger.__module__ = cls.__module__
ClassLogger.__doc__ = cls.__doc__
ClassLogger.__qualname__ = cls.__qualname__
merge_class(cls, ClassLogger, ignored_attributes=["__setattr__"])
return ClassLogger
7 changes: 6 additions & 1 deletion src/zenlib/logging/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def log_init(self, args, kwargs):
class_name = self.__class__.__name__
logger = self.logger
if not kwargs.pop("_log_init", False):
return logger.debug("Init logging disabled for class: %s", class_name)
return logger.log(5, "Init logging disabled for class: %s", class_name)

logger.info("Initializing class: %s", class_name)

Expand All @@ -49,6 +49,11 @@ def log_init(self, args, kwargs):
if class_version := getattr(self, '__version__', None):
logger.info("[%s] Class version: %s" % (class_name, class_version))

def handle_additional_logging(self, kwargs):
""" Sets __setattr__ to log_setattr if _log_setattr is in the kwargs and set to True """
if kwargs.pop("_log_setattr", False):
setattr(self, "__setattr__", log_setattr)

def log_setattr(self, name, value):
""" Logs when an attribute is set """
super().__setattr__(name, value)
Expand Down
8 changes: 0 additions & 8 deletions src/zenlib/threading/__init__.py

This file was deleted.

85 changes: 0 additions & 85 deletions src/zenlib/threading/add_thread.py

This file was deleted.

14 changes: 0 additions & 14 deletions src/zenlib/threading/loop_thread.py

This file was deleted.

32 changes: 0 additions & 32 deletions src/zenlib/threading/threaded.py

This file was deleted.

44 changes: 0 additions & 44 deletions src/zenlib/threading/zenthread.py

This file was deleted.

4 changes: 4 additions & 0 deletions src/zenlib/types/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .nodupflatlist import NoDupFlatList
from .validated_dataclass import validatedDataclass

__all__ = ["NoDupFlatList", "validatedDataclass"]
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
__author__ = "desultory"
__version__ = "1.0.0"
__version__ = "1.1.1"


from zenlib.logging import loggify
from zenlib.logging import ClassLogger
from zenlib.util import handle_plural

from .handle_plural import handle_plural


@loggify
class NoDupFlatList(list):
class NoDupFlatList(ClassLogger, list):
"""List that automatically filters duplicate elements when appended and concatenated."""

def __init__(self, no_warn=False, log_bump=0, *args, **kwargs):
def __init__(self, no_warn=False, *args, **kwargs):
super().__init__(*args, **kwargs)
self.no_warn = no_warn
self.logger.setLevel(self.logger.parent.level + log_bump)

@handle_plural
def append(self, item):
Expand Down
Loading
Loading