From 2f48d742660b8588d16bbf6c95a2977587a21ab2 Mon Sep 17 00:00:00 2001 From: Brett Date: Fri, 20 Dec 2024 16:05:32 -0500 Subject: [PATCH] reorganize "traversable" detection 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 --- asdf/_node_info.py | 11 ++++++++--- asdf/search.py | 10 +++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/asdf/_node_info.py b/asdf/_node_info.py index 870b68746..ba6b9f328 100644 --- a/asdf/_node_info.py +++ b/asdf/_node_info.py @@ -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"]) @@ -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, @@ -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: diff --git a/asdf/search.py b/asdf/search.py index d04972d8e..7f6626a69 100644 --- a/asdf/search.py +++ b/asdf/search.py @@ -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 @@ -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) @@ -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])