From 9a5bff9de2e12126298cc570d4349fe4a77c622f Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Sat, 22 Aug 2026 08:52:09 +0200 Subject: [PATCH] Object: Move into obj.Object The Object is going to get narrowed variants, so move it into a submodule of its own. Keep jw.util.Object as a compatibility shim for now. Signed-off-by: Jan Lindemann --- src/python/jw/util/Object.py | 73 ++------------------------------ src/python/jw/util/obj/Makefile | 4 ++ src/python/jw/util/obj/Object.py | 70 ++++++++++++++++++++++++++++++ src/python/jwutils/Object.py | 2 +- 4 files changed, 79 insertions(+), 70 deletions(-) create mode 100644 src/python/jw/util/obj/Makefile create mode 100644 src/python/jw/util/obj/Object.py diff --git a/src/python/jw/util/Object.py b/src/python/jw/util/Object.py index a9818dc..eec136f 100644 --- a/src/python/jw/util/Object.py +++ b/src/python/jw/util/Object.py @@ -1,70 +1,5 @@ -from __future__ import print_function +from .obj.Object import Object as Object -import abc - -from typing import Any, Iterable, TypeAlias, override - -from . import log - -class Object(abc.ABC): - - LogData: TypeAlias = dict[str, dict[str, Any]] - - @staticmethod - def _class_path(proxy: object) -> str: - owner: type[Any] = getattr(proxy, "__thisclass__") - if not isinstance(owner, type): - raise TypeError("Expected a super() proxy") - return f"{owner.__module__}.{owner.__qualname__}" - - def __init__(self, **kwargs: Any) -> None: - super().__init__(**kwargs) - self.log_level = log.log_level() - - @override - def __repr__(self) -> str: - return self.name - - @override - def __str__(self) -> str: - return self._log_name() - - @abc.abstractmethod - def _name_parts(self) -> Iterable[str]: - yield self.__class__.__name__ - - def _log_data(self, buf: LogData) -> None: - buf[self._class_path(super())] = { - 'name': self.name, - } - - @property - def log_data(self) -> LogData: - ret: Object.LogData = {} - self._log_data(ret) - return ret - - @property - def name(self) -> str: - return ' | '.join(self._name_parts()) - - def _log_name(self) -> str: - return f'{{{self.name}}}' - - @property - def log_name(self) -> str: - return self._log_name() - - def log(self, prio: int, *args: Any) -> None: - if self.log_level == log.log_level(): - log.slog(prio, args) - return - if prio <= self.log_level: - msg = "" - for count, thing in enumerate(args): - msg += ' ' + str(*thing) - if len(msg): - print(msg[1:]) - - def debug(self, *args: Any) -> None: - log.slog(log.DEBUG, args) +__all__ = [ + "Object", # export +] diff --git a/src/python/jw/util/obj/Makefile b/src/python/jw/util/obj/Makefile new file mode 100644 index 0000000..781b0c8 --- /dev/null +++ b/src/python/jw/util/obj/Makefile @@ -0,0 +1,4 @@ +TOPDIR = ../../../../.. + +include $(TOPDIR)/make/proj.mk +include $(JWBDIR)/make/py-mod.mk diff --git a/src/python/jw/util/obj/Object.py b/src/python/jw/util/obj/Object.py new file mode 100644 index 0000000..d7ab852 --- /dev/null +++ b/src/python/jw/util/obj/Object.py @@ -0,0 +1,70 @@ +from __future__ import print_function + +import abc + +from typing import Any, Iterable, TypeAlias, override + +from .. import log + +class Object(abc.ABC): + + LogData: TypeAlias = dict[str, dict[str, Any]] + + @staticmethod + def _class_path(proxy: object) -> str: + owner: type[Any] = getattr(proxy, "__thisclass__") + if not isinstance(owner, type): + raise TypeError("Expected a super() proxy") + return f"{owner.__module__}.{owner.__qualname__}" + + def __init__(self, **kwargs: Any) -> None: + super().__init__(**kwargs) + self.log_level = log.log_level() + + @override + def __repr__(self) -> str: + return self.name + + @override + def __str__(self) -> str: + return self._log_name() + + @abc.abstractmethod + def _name_parts(self) -> Iterable[str]: + yield self.__class__.__name__ + + def _log_data(self, buf: LogData) -> None: + buf[self._class_path(super())] = { + 'name': self.name, + } + + @property + def log_data(self) -> LogData: + ret: Object.LogData = {} + self._log_data(ret) + return ret + + @property + def name(self) -> str: + return ' | '.join(self._name_parts()) + + def _log_name(self) -> str: + return f'{{{self.name}}}' + + @property + def log_name(self) -> str: + return self._log_name() + + def log(self, prio: int, *args: Any) -> None: + if self.log_level == log.log_level(): + log.slog(prio, args) + return + if prio <= self.log_level: + msg = "" + for count, thing in enumerate(args): + msg += ' ' + str(*thing) + if len(msg): + print(msg[1:]) + + def debug(self, *args: Any) -> None: + log.slog(log.DEBUG, args) diff --git a/src/python/jwutils/Object.py b/src/python/jwutils/Object.py index d1867ae..94a55d6 100644 --- a/src/python/jwutils/Object.py +++ b/src/python/jwutils/Object.py @@ -1,5 +1,5 @@ # ruff: noqa: E501 -from jw.util.Object import Object as Object +from jw.util.obj.Object import Object as Object __all__ = [ "Object",