mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-24 09:13:37 +02:00
lib.Types: Make debug logging optional
lib.Types class detection is too chatty. Make that a ctor option. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
e56befb1c3
commit
b2847809c1
1 changed files with 11 additions and 6 deletions
|
|
@ -34,12 +34,17 @@ class Types(abc.ABC, Iterable[T], Generic[T]): # export
|
|||
|
||||
class LoadTypes(Types): # export
|
||||
|
||||
def __init__(self, mod_names: list[str], type_name_filter: str=None, type_filter: list[T]=[]):
|
||||
def __init__(self, mod_names: list[str], type_name_filter: str=None, type_filter: list[T]=[], debug_level=OFF):
|
||||
self.__debug_level = debug_level
|
||||
self.__type_name_filter = type_name_filter
|
||||
self.__type_filter = type_filter
|
||||
self.__mod_names = mod_names
|
||||
self.__classes: list[type[Any]]|None = None
|
||||
|
||||
def _debug(self, *args, **kwargs) -> None:
|
||||
if self.__debug_level != OFF:
|
||||
log(self.__debug_level, *args, **kwargs)
|
||||
|
||||
def _stringify(self):
|
||||
return [
|
||||
"type_name_filter: " + str(self.__type_name_filter),
|
||||
|
|
@ -59,21 +64,21 @@ class LoadTypes(Types): # export
|
|||
importlib.import_module(mod_name)
|
||||
for member_name, c in inspect.getmembers(sys.modules[mod_name], inspect.isclass):
|
||||
if rx is not None and not re.match(rx, member_name):
|
||||
log(DEBUG, 'o "{}.{}" has wrong name'.format(mod_name, member_name))
|
||||
self._debug('o "{}.{}" has wrong name'.format(mod_name, member_name))
|
||||
continue
|
||||
if inspect.isabstract(c):
|
||||
log(DEBUG, 'o "{}.{}" is abstract'.format(mod_name, member_name))
|
||||
self._debug('o "{}.{}" is abstract'.format(mod_name, member_name))
|
||||
continue
|
||||
if self.__type_filter:
|
||||
for tp in self.__type_filter:
|
||||
if issubclass(c, tp):
|
||||
break
|
||||
log(DEBUG, 'o "{}.{}" is not of type {}'.format(mod_name, member_name, tp))
|
||||
self._debug('o "{}.{}" is not of type {}'.format(mod_name, member_name, tp))
|
||||
else:
|
||||
log(DEBUG, 'o "{}.{}" doesn\'t match type filter'.format(mod_name, member_name))
|
||||
self._debug('o "{}.{}" doesn\'t match type filter'.format(mod_name, member_name))
|
||||
breakpoint()
|
||||
continue
|
||||
log(DEBUG, 'o "{}.{}" is fine, adding'.format(mod_name, member_name))
|
||||
self._debug('o "{}.{}" is fine, adding'.format(mod_name, member_name))
|
||||
ret.append(c)
|
||||
self.__classes = ret
|
||||
return self.__classes
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue