initial implementation
[mdis.git] / src / mdis / core.py
1 class TypeidHook(object):
2 def __init__(self, *typeids, wrapper=lambda call: call):
3 for typeid in typeids:
4 if not isinstance(typeid, type):
5 raise ValueError(typeid)
6 self.__typeids = typeids
7 self.__wrapper = wrapper
8 return super().__init__()
9
10 def __iter__(self):
11 yield from self.__typeids
12
13 def __repr__(self):
14 return f"{self.__class__.__name__}({self.__typeids})"
15
16 def __call__(self, call):
17 if not callable(call):
18 raise ValueError(call)
19 return CallHook(typeids=self, call=call, wrapper=self.__wrapper)
20
21
22 class CallHook(object):
23 def __init__(self, typeids, call, wrapper=lambda call: call):
24 if not isinstance(typeids, TypeidHook):
25 raise ValueError(typeids)
26 if not callable(call):
27 raise ValueError(call)
28 self.__typeids = typeids
29 self.__call = wrapper(call)
30 return super().__init__()
31
32 def __repr__(self):
33 return f"{self.__class__.__name__}(call={self.call!r}, typeids={self.typeids!r})"
34
35 @property
36 def typeids(self):
37 return self.__typeids
38
39 @property
40 def call(self):
41 return self.__call
42
43 def __call__(self, visitor, instance):
44 return self.__call(visitor, instance)
45
46
47 def hook(*typeids):
48 return TypeidHook(*typeids)