Skip to content

Commit

Permalink
reorganize "traversable" detection
Browse files Browse the repository at this point in the history
Leave search usage as a hasattr(__asdf_traverse__) check
since search doesn't have access to an extension manager

Update info usage to allow use of an extension manager
  • Loading branch information
braingram committed Dec 20, 2024
1 parent ecaff10 commit 2f48d74
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
11 changes: 8 additions & 3 deletions asdf/_node_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ def _get_extension_manager(refresh_extension_manager):
return af.extension_manager


def _traversable(node, extension_manager):
# TODO use extension_manager
return hasattr(node, "__asdf_traverse__")


_SchemaInfo = namedtuple("SchemaInfo", ["info", "value"])


Expand Down Expand Up @@ -268,7 +273,7 @@ def from_root_node(
next_nodes = []

for parent, identifier, node in current_nodes:
if (isinstance(node, (dict, tuple)) or cls.traversable(node)) and id(node) in seen:
if (isinstance(node, (dict, tuple)) or _traversable(node, extension_manager)) and id(node) in seen:
info = NodeSchemaInfo(
key,
parent,
Expand All @@ -289,14 +294,14 @@ def from_root_node(
root_info = info

if parent is not None:
if parent.schema is not None and not cls.traversable(node):
if parent.schema is not None and not _traversable(node, extension_manager):
info.set_schema_for_property(parent, identifier)

parent.children.append(info)

seen.add(id(node))

if cls.traversable(node):
if _traversable(node, extension_manager):
t_node = node.__asdf_traverse__()
if hasattr(node, "_tag") and isinstance(node._tag, str):
try:
Expand Down
10 changes: 5 additions & 5 deletions asdf/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import typing

from ._display import DEFAULT_MAX_COLS, DEFAULT_MAX_ROWS, DEFAULT_SHOW_VALUES, format_faint, format_italic, render_tree
from ._node_info import NodeSchemaInfo, collect_schema_info
from ._node_info import collect_schema_info
from .treeutil import get_children, is_container
from .util import NotSet

Expand Down Expand Up @@ -353,8 +353,8 @@ def schema_info(self, key="description", preserve_list=True, refresh_extension_m
)

def __getitem__(self, key):
if isinstance(self._node, (dict, list, tuple)) or NodeSchemaInfo.traversable(self._node):
child = self._node.__asdf_traverse__()[key] if NodeSchemaInfo.traversable(self._node) else self._node[key]
if isinstance(self._node, (dict, list, tuple)) or hasattr(self._node, "__asdf_traverse__"):
child = self._node.__asdf_traverse__()[key] if hasattr(self._node, "__asdf_traverse__") else self._node[key]
else:
msg = "This node cannot be indexed"
raise TypeError(msg)
Expand All @@ -381,9 +381,9 @@ def _walk_tree_breadth_first(root_identifiers, root_node, callback):
next_nodes = []

for identifiers, parent, node in current_nodes:
if (isinstance(node, (dict, list, tuple)) or NodeSchemaInfo.traversable(node)) and id(node) in seen:
if (isinstance(node, (dict, list, tuple)) or hasattr(node, "__asdf_traverse__")) and id(node) in seen:
continue
tnode = node.__asdf_traverse__() if NodeSchemaInfo.traversable(node) else node
tnode = node.__asdf_traverse__() if hasattr(node, "__asdf_traverse__") else node
children = get_children(tnode)
callback(identifiers, parent, node, [c for _, c in children])
next_nodes.extend([([*identifiers, i], node, c) for i, c in children])
Expand Down

0 comments on commit 2f48d74

Please sign in to comment.