From 52b277a8182963ee5edbf2680ddb3b71e243e21d Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Sat, 23 Nov 2024 02:59:12 -0400 Subject: [PATCH] feat: get rid of internal python paths (#432) --- a_sync/primitives/_debug.pxd | 1 + a_sync/primitives/_debug.pyx | 5 ++++- a_sync/primitives/_loggable.pyx | 25 +++++++++++++++++++------ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/a_sync/primitives/_debug.pxd b/a_sync/primitives/_debug.pxd index cf77d705..c947002d 100644 --- a/a_sync/primitives/_debug.pxd +++ b/a_sync/primitives/_debug.pxd @@ -8,3 +8,4 @@ cdef class _LoopBoundMixin(_LoggerMixin): cdef class _DebugDaemonMixin(_LoopBoundMixin): cdef object _daemon cdef object _c_ensure_debug_daemon(self, tuple[object] args, dict[str, object] kwargs) + cdef object _c_start_debug_daemon(self, tuple[object] args, dict[str, object] kwargs) diff --git a/a_sync/primitives/_debug.pyx b/a_sync/primitives/_debug.pyx index b94bfd98..a74b41f9 100644 --- a/a_sync/primitives/_debug.pyx +++ b/a_sync/primitives/_debug.pyx @@ -124,6 +124,9 @@ cdef class _DebugDaemonMixin(_LoopBoundMixin): See Also: :meth:`_ensure_debug_daemon` for ensuring the daemon is running. """ + return self._c_start_debug_daemon(args, kwargs) + + cdef object _c_start_debug_daemon(self, tuple[object] args, dict[str, object] kwargs): cdef object loop = self._c_get_loop() if self.check_debug_logs_enabled() and loop.is_running(): return ccreate_task_simple(self._debug_daemon(*args, **kwargs)) @@ -159,7 +162,7 @@ cdef class _DebugDaemonMixin(_LoopBoundMixin): cdef object daemon = self._daemon if daemon is None: if self.check_debug_logs_enabled(): - self._daemon = self._start_debug_daemon(*args, **kwargs) + self._daemon = self._c_start_debug_daemon(args, kwargs) self._daemon.add_done_callback(self._stop_debug_daemon) else: self._daemon = self._c_get_loop().create_future() diff --git a/a_sync/primitives/_loggable.pyx b/a_sync/primitives/_loggable.pyx index 5f650efa..414e92de 100644 --- a/a_sync/primitives/_loggable.pyx +++ b/a_sync/primitives/_loggable.pyx @@ -50,15 +50,17 @@ cdef class _LoggerMixin: return self.get_logger() cdef object get_logger(self): - cdef str logger_id cdef object logger, cls + cdef str cls_key, name logger = self._logger - if not logger: + if logger is None: cls = type(self) - logger_id = "{}.{}".format(cls.__module__, cls.__qualname__) - if hasattr(self, "_name") and self._name: - logger_id += ".{}".format(self._name) - logger = getLogger(logger_id) + name = getattr(self, "_name", "") + if name: + logger = getLogger(f"{cls.__module__}.{cls.__qualname__}.{name}") + else: + logger = _get_logger_for_cls(f"{cls.__module__}.{cls.__qualname__}") + self._logger = logger return logger @property @@ -81,3 +83,14 @@ cdef class _LoggerMixin: cdef bint check_debug_logs_enabled(self): return self.get_logger().isEnabledFor(DEBUG) + + +cdef dict[str, object] _class_loggers = {} + +cdef object _get_logger_for_cls(str cls_key): + cdef object logger = _class_loggers.get(cls_key) + if logger is None: + logger = getLogger(cls_key) + _class_loggers[cls_key] = logger + return logger +