MCPcopy Create free account
hub / github.com/Graphify-Labs/graphify / _prefer_file_node

Function _prefer_file_node

graphify/affected.py:120–175  ·  view source on GitHub ↗

Return the file-level node when a source_file query matches many nodes.

(
    graph: nx.Graph,
    node_ids: list[str],
    query: str,
)

Source from the content-addressed store, hash-verified

118
119
120def _prefer_file_node(
121 graph: nx.Graph,
122 node_ids: list[str],
123 query: str,
124) -> str | None:
125 """Return the file-level node when a source_file query matches many nodes."""
126 query_basename = _normalize_label(Path(query).name)
127 exact_file_nodes = [
128 node_id
129 for node_id in node_ids
130 if str(graph.nodes[node_id].get("source_location", "")) == "L1"
131 and _normalize_label(str(graph.nodes[node_id].get("label", ""))) == query_basename
132 ]
133 if len(exact_file_nodes) == 1:
134 return exact_file_nodes[0]
135
136 # #2032 relabels colliding basenames to their shortest unique path suffix
137 # ("alpha/hooks.py"), so neither pass above nor the one below matches a file
138 # node whose basename is shared. The label is still a path form of the
139 # node's OWN source_file, which is what _is_file_node_label encodes (the
140 # predicate build/analyze already treat as "this node names its file").
141 # Checked before the L1 fallback because a module docstring (a `rationale`
142 # node located on the docstring's line) or a one-line top-level definition
143 # can also sit on L1, leaving that fallback with more than one candidate
144 # (#4698).
145 from graphify.build import _is_file_node_label
146
147 path_file_nodes = [
148 node_id
149 for node_id in node_ids
150 if str(graph.nodes[node_id].get("source_location", "")) == "L1"
151 and _is_file_node_label(
152 str(graph.nodes[node_id].get("label", "")),
153 str(graph.nodes[node_id].get("source_file", "")),
154 )
155 ]
156 if len(path_file_nodes) == 1:
157 return path_file_nodes[0]
158
159 l1_nodes = [
160 node_id
161 for node_id in node_ids
162 if str(graph.nodes[node_id].get("source_location", "")) == "L1"
163 ]
164 if len(l1_nodes) == 1:
165 return l1_nodes[0]
166
167 basename_nodes = [
168 node_id
169 for node_id in node_ids
170 if _normalize_label(str(graph.nodes[node_id].get("label", ""))) == query_basename
171 ]
172 if len(basename_nodes) == 1:
173 return basename_nodes[0]
174
175 return None
176
177

Callers 1

resolve_seedFunction · 0.85

Calls 3

_is_file_node_labelFunction · 0.90
_normalize_labelFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected