9 from . import dispatcher
12 class WalkerMeta(dispatcher
.DispatcherMeta
):
17 def __init__(self
, path
):
19 return super().__init
__()
22 return self
.__path
.__str
__()
25 return f
"{self.__class__.__name__}({str(self)})"
32 class IndexPath(GenericPath
):
34 return f
"[{self.path!r}]"
37 class AttributePath(GenericPath
):
39 return f
".{self.path}]"
42 class HashPath(GenericPath
):
44 return f
"{{{self.path}}}"
47 class Walker(dispatcher
.Dispatcher
, metaclass
=WalkerMeta
):
48 @dispatcher.Hook(tuple, list)
49 def dispatch_ordered_sequence(self
, node
, *_
, **__
):
50 for (index
, item
) in enumerate(node
):
51 yield (item
, node
, index
, IndexPath
)
53 @dispatcher.Hook(set, frozenset)
54 def dispatch_unordered_sequence(self
, node
, *_
, **__
):
56 yield (item
, node
, item
, HashPath
)
58 @dispatcher.Hook(dataclasses
.is_dataclass
)
59 def dispatch_dataclass(self
, node
, *_
, **__
):
60 for field
in dataclasses
.fields(node
):
62 value
= getattr(node
, key
)
63 yield (value
, node
, key
, AttributePath
)
65 @dispatcher.Hook(dict)
66 def dispatch_mapping(self
, node
, *_
, **__
):
67 for (key
, value
) in node
.items():
68 yield (key
, node
, key
, HashPath
)
69 yield (value
, node
, key
, IndexPath
)
71 @dispatcher.Hook(object)
72 def dispatch_object(self
, node
, *_
, **__
):