import inspect as _inspect
import types as _types
-from . import core as _core
+
+class Hook(object):
+ def __init__(self, *typeids):
+ for typeid in typeids:
+ if not isinstance(typeid, type):
+ raise ValueError(typeid)
+ self.__typeids = typeids
+ return super().__init__()
+
+ def __iter__(self):
+ yield from self.__typeids
+
+ def __repr__(self):
+ names = []
+ for typeid in self.__typeids:
+ name = typeid.__qualname__
+ module = typeid.__module__
+ if module not in ("builtins",):
+ name = f"{module}.{name}"
+ names.append(name)
+ return f"<{', '.join(names)}>"
+
+ def __call__(self, call):
+ class ConcreteHook(Hook):
+ def __call__(self, dispatcher, instance):
+ return call(self=dispatcher, instance=instance)
+
+ return ConcreteHook(*tuple(self))
class DispatcherMeta(type):
def __new__(metacls, name, bases, ns):
hooks = {}
- ishook = lambda member: isinstance(member, _core.Hook)
+ ishook = lambda member: isinstance(member, Hook)
for basecls in reversed(bases):
members = _inspect.getmembers(basecls, predicate=ishook)
hook = self.__class__.dispatch()
return hook(dispatcher=self, instance=instance)
- @_core.hook(object)
+ @Hook(object)
def dispatch_object(self, instance):
raise NotImplementedError()