all: deprecate core module
[mdis.git] / src / mdis / dispatcher.py
index f21a292006df30ea1991267b12ceb542215f3c1e..6f6b9cd1be5d36bf797837039524a30ace1494ad 100644 (file)
@@ -2,7 +2,34 @@ import collections as _collections
 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):
@@ -10,7 +37,7 @@ 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)
@@ -49,6 +76,6 @@ class Dispatcher(metaclass=DispatcherMeta):
             hook = self.__class__.dispatch()
         return hook(dispatcher=self, instance=instance)
 
-    @_core.hook(object)
+    @Hook(object)
     def dispatch_object(self, instance):
         raise NotImplementedError()