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 <jan@janware.com>
This commit is contained in:
parent
76fa902dab
commit
9a5bff9de2
4 changed files with 79 additions and 70 deletions
|
|
@ -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
|
||||
]
|
||||
|
|
|
|||
4
src/python/jw/util/obj/Makefile
Normal file
4
src/python/jw/util/obj/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
TOPDIR = ../../../../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/py-mod.mk
|
||||
70
src/python/jw/util/obj/Object.py
Normal file
70
src/python/jw/util/obj/Object.py
Normal file
|
|
@ -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)
|
||||
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Reference in a new issue