Skip to content

Commit

Permalink
feat: cythonize _debug and _loggable mixins (#392)
Browse files Browse the repository at this point in the history
* feat: cythonize _debug and _loggable mixins

* chore: `black .`

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
BobTheBuidler and github-actions[bot] authored Nov 18, 2024
1 parent 5ce4e02 commit 41dd0ea
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
12 changes: 12 additions & 0 deletions a_sync/primitives/_debug.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import abc
from a_sync.primitives._loggable import _LoggerMixin

class _DebugDaemonMixin(_LoggerMixin, metaclass=abc.ABCMeta):
"""
A mixin class that provides a framework for debugging capabilities using a daemon task.
This mixin sets up the structure for managing a debug daemon task. Subclasses are responsible for implementing the specific behavior of the daemon, including any logging functionality.
See Also:
:class:`_LoggerMixin` for logging capabilities.
"""
8 changes: 5 additions & 3 deletions a_sync/primitives/_debug.py → a_sync/primitives/_debug.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import asyncio
from typing import Optional

import a_sync.asyncio
from a_sync.primitives._loggable import _LoggerMixin


Expand Down Expand Up @@ -72,9 +73,10 @@ def _start_debug_daemon(self, *args, **kwargs) -> "asyncio.Future[None]":
See Also:
:meth:`_ensure_debug_daemon` for ensuring the daemon is running.
"""
if self.debug_logs_enabled and asyncio.get_event_loop().is_running():
return asyncio.create_task(self._debug_daemon(*args, **kwargs))
return asyncio.get_event_loop().create_future()
loop = asyncio.get_event_loop()
if self.debug_logs_enabled and loop.is_running():
return a_sync.asyncio.create_task(self._debug_daemon(*args, **kwargs))
return loop.create_future()

def _ensure_debug_daemon(self, *args, **kwargs) -> "asyncio.Future[None]":
"""
Expand Down
62 changes: 62 additions & 0 deletions a_sync/primitives/_loggable.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from functools import cached_property as cached_property
from logging import Logger

class _LoggerMixin:
"""
A mixin class that adds logging capabilities to other classes.
This mixin provides a cached property for accessing a logger instance and a property to check if debug logging is enabled.
See Also:
- :func:`logging.getLogger`
- :class:`logging.Logger`
"""

@cached_property
def logger(self) -> Logger:
"""
Provides a logger instance specific to the class using this mixin.
The logger ID is constructed from the module and class name, and optionally includes an instance name if available.
Examples:
>>> class MyClass(_LoggerMixin):
... _name = "example"
...
>>> instance = MyClass()
>>> logger = instance.logger
>>> logger.name
\'module_name.MyClass.example\'
>>> class AnotherClass(_LoggerMixin):
... pass
...
>>> another_instance = AnotherClass()
>>> another_logger = another_instance.logger
>>> another_logger.name
\'module_name.AnotherClass\'
Note:
Replace `module_name` with the actual module name where the class is defined.
See Also:
- :func:`logging.getLogger`
- :class:`logging.Logger`
"""

@property
def debug_logs_enabled(self) -> bool:
"""
Checks if debug logging is enabled for the logger.
Examples:
>>> class MyClass(_LoggerMixin):
... pass
...
>>> instance = MyClass()
>>> instance.debug_logs_enabled
False
See Also:
- :attr:`logging.Logger.isEnabledFor`
"""
File renamed without changes.

0 comments on commit 41dd0ea

Please sign in to comment.