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:
Jan Lindemann 2026-03-15 09:30:58 +01:00
commit b2847809c1

View file

@ -34,12 +34,17 @@ class Types(abc.ABC, Iterable[T], Generic[T]): # export
class LoadTypes(Types): # 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_name_filter = type_name_filter
self.__type_filter = type_filter self.__type_filter = type_filter
self.__mod_names = mod_names self.__mod_names = mod_names
self.__classes: list[type[Any]]|None = None 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): def _stringify(self):
return [ return [
"type_name_filter: " + str(self.__type_name_filter), "type_name_filter: " + str(self.__type_name_filter),
@ -59,21 +64,21 @@ class LoadTypes(Types): # export
importlib.import_module(mod_name) importlib.import_module(mod_name)
for member_name, c in inspect.getmembers(sys.modules[mod_name], inspect.isclass): 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): 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 continue
if inspect.isabstract(c): 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 continue
if self.__type_filter: if self.__type_filter:
for tp in self.__type_filter: for tp in self.__type_filter:
if issubclass(c, tp): if issubclass(c, tp):
break 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: 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() breakpoint()
continue 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) ret.append(c)
self.__classes = ret self.__classes = ret
return self.__classes return self.__classes