Skip to content

Commit

Permalink
For got about listed nodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
InnocentBug committed Mar 11, 2024
1 parent 145e498 commit 0015b99
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/cript/nodes/node_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,34 @@ def __init__(self, root):
def _depth_first(self, node) -> None:
"""Helper function that does the traversal in depth first order and stores result in stack"""

def handle_child_node(child_node):
"""Helper function that adds a child to the stack.
This function can be called for both listed children and regular children attributes
"""
self._stack.append(child_node)
self._uuid_visited.add(str(child_node.uuid))
# Recursive call to add all children
self._depth_first(child_node)

for attr_name in fields(node._json_attrs):
attr = getattr(node._json_attrs.attr_name)
attr = getattr(node._json_attrs, str(attr_name))
try: # Only attribute that have uuid, can be UUID children
uuid = str(attr.uuid)
except AttributeError:
pass # Ignore non-uuid nodes
# Handle list attributes
if isinstance(attr, list):
for list_attr in attr:
try:
list_uuid = str(attr.uuid) # type: ignore
except AttributeError:
pass # We do not handle non-uuid nodes
else:
if list_uuid not in self._uuid_visited:
handle_child_node(list_attr)
else:
if uuid not in self._uuid_visited: # break cycles
self._stack.append(attr)
self._uuid_visited.add(uuid)
# Recursive call to add all children
self._add_children(attr)
handle_child_node(attr)

def __next__(self):
if self._iter_position >= len(self._stack):
Expand Down

0 comments on commit 0015b99

Please sign in to comment.