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