1 import collections
as _collections
2 import inspect
as _inspect
5 from . import core
as _core
8 class DispatcherMeta(type):
11 def __new__(metacls
, name
, bases
, ns
):
13 ishook
= lambda member
: isinstance(member
, _core
.Hook
)
15 for basecls
in reversed(bases
):
16 members
= _inspect
.getmembers(basecls
, predicate
=ishook
)
17 for (_
, hook
) in members
:
18 hooks
.update(dict.fromkeys(hook
, hook
))
20 conflicts
= _collections
.defaultdict(list)
21 for (key
, value
) in tuple(ns
.items()):
27 conflicts
[typeid
].append(key
)
30 for (typeid
, keys
) in conflicts
.items():
32 raise ValueError(f
"dispatch conflict: {keys!r}")
34 ns
["__hooks__"] = _types
.MappingProxyType(hooks
)
36 return super().__new
__(metacls
, name
, bases
, ns
)
38 def dispatch(cls
, typeid
=object):
39 return cls
.__hooks
__.get(typeid
)
42 class Dispatcher(metaclass
=DispatcherMeta
):
43 def __call__(self
, instance
):
44 for typeid
in instance
.__class
__.__mro
__:
45 hook
= self
.__class
__.dispatch(typeid
=typeid
)
49 hook
= self
.__class
__.dispatch()
50 return hook(dispatcher
=self
, instance
=instance
)
53 def dispatch_object(self
, instance
):
54 raise NotImplementedError()