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}]"
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
)
54 @dispatcher.Hook(set, frozenset)
55 def dispatch_unordered_sequence(self
, node
):
57 yield (item
, node
, item
, HashPath
)
60 @dispatcher.Hook(dataclasses
.is_dataclass
)
61 def dispatch_dataclass(self
, node
):
62 for field
in dataclasses
.fields(node
):
64 value
= getattr(node
, key
)
65 yield (value
, node
, key
, AttributePath
)
66 yield from self(value
)
68 @dispatcher.Hook(dict)
69 def dispatch_mapping(self
, node
):
70 for (key
, value
) in node
.items():
71 yield (key
, node
, key
, HashPath
)
73 yield (value
, node
, key
, IndexPath
)
74 yield from self(value
)
76 @dispatcher.Hook(object)
77 def dispatch_object(self
, node
, path
=()):