def __call__(self, call):
class ConcreteHook(Hook):
- def __call__(self, dispatcher, instance, *args, **kwargs):
- return call(self=dispatcher, instance=instance,
- *args, **kwargs)
+ def __call__(self, dispatcher, node, *args, **kwargs):
+ return call(dispatcher, node, *args, **kwargs)
return ConcreteHook(*tuple(self))
class Dispatcher(metaclass=DispatcherMeta):
- def __call__(self, instance, *args, **kwargs):
- for typeid in instance.__class__.__mro__:
+ def __call__(self, node, *args, **kwargs):
+ for typeid in node.__class__.__mro__:
hook = self.__class__.dispatch(typeid=typeid)
if hook is not None:
break
if hook is None:
hook = self.__class__.dispatch()
- return hook(dispatcher=self, instance=instance, *args, **kwargs)
+ return hook(self, node, *args, **kwargs)
@Hook(object)
- def dispatch_object(self, instance, *args, **kwargs):
+ def dispatch_object(self, node, *args, **kwargs):
raise NotImplementedError()
class Visitor(dispatcher.Dispatcher, metaclass=VisitorMeta):
@dispatcher.Hook(object)
- def dispatch_object(self, instance):
- return instance
+ def dispatch_object(self, node):
+ return node
class ContextVisitor(Visitor):
@dispatcher.Hook(object)
@contextlib.contextmanager
- def dispatch_object(self, instance):
- yield super().__call__(instance=instance)
+ def dispatch_object(self, node):
+ yield super().__call__(node)
class Walker(dispatcher.Dispatcher, metaclass=WalkerMeta):
@dispatcher.Hook(tuple, list)
- def dispatch_ordered_sequence(self, instance):
- for (index, item) in enumerate(instance):
- yield (item, instance, index, IndexPath)
+ def dispatch_ordered_sequence(self, node):
+ for (index, item) in enumerate(node):
+ yield (item, node, index, IndexPath)
yield from self(item)
@dispatcher.Hook(set, frozenset)
- def dispatch_unordered_sequence(self, instance):
- for item in instance:
- yield (item, instance, item, HashPath)
+ def dispatch_unordered_sequence(self, node):
+ for item in node:
+ yield (item, node, item, HashPath)
yield from self(item)
@dispatcher.Hook(dataclasses.is_dataclass)
- def dispatch_dataclass(self, instance):
- for field in dataclasses.fields(instance):
+ def dispatch_dataclass(self, node):
+ for field in dataclasses.fields(node):
key = field.name
- value = getattr(instance, key)
- yield (value, instance, key, AttributePath)
+ value = getattr(node, key)
+ yield (value, node, key, AttributePath)
yield from self(value)
@dispatcher.Hook(dict)
- def dispatch_mapping(self, instance):
- for (key, value) in instance.items():
- yield (key, instance, key, HashPath)
+ def dispatch_mapping(self, node):
+ for (key, value) in node.items():
+ yield (key, node, key, HashPath)
yield from self(key)
- yield (value, instance, key, IndexPath)
+ yield (value, node, key, IndexPath)
yield from self(value)
@dispatcher.Hook(object)
- def dispatch_object(self, instance, path=()):
+ def dispatch_object(self, node, path=()):
yield from ()