Read current graphify graph.json via NetworkX's node-link parser.
(data: dict)
| 220 | |
| 221 | |
| 222 | def _node_link_payload(data: dict) -> tuple[list, list] | None: |
| 223 | """Read current graphify graph.json via NetworkX's node-link parser.""" |
| 224 | if not isinstance(data.get("nodes"), list): |
| 225 | return None |
| 226 | if not isinstance(data.get("links"), list) and not isinstance(data.get("edges"), list): |
| 227 | return None |
| 228 | |
| 229 | try: |
| 230 | # Shared loader normalizes the raw writer's "edges" key to "links" |
| 231 | # before parsing; without it an edges-keyed payload raised |
| 232 | # KeyError: 'links' and this function silently returned None even |
| 233 | # though the shape check above accepts "edges" (#2212). |
| 234 | from graphify.paths import load_node_link_graph |
| 235 | |
| 236 | # Force directed/multigraph so the stored caller->callee direction and |
| 237 | # parallel edges survive the round-trip; mirrors affected.py, |
| 238 | # serve.py and cli.py (#1174) and the directed-view fix for |
| 239 | # path/shortest_path (#2487, #2309). graph.json is written with |
| 240 | # "directed": false for backward compatibility, so without this |
| 241 | # networkx returns an undirected Graph and edge orientation becomes |
| 242 | # arbitrary -- which silently swaps the Caller and Callee columns of |
| 243 | # the call table and drops parallel edges. The _src/_tgt override |
| 244 | # below still wins on legacy marker files. |
| 245 | graph = load_node_link_graph({**data, "directed": True, "multigraph": True}) |
| 246 | except Exception: |
| 247 | return None |
| 248 | |
| 249 | nodes = [] |
| 250 | for node_id, attrs in graph.nodes(data=True): |
| 251 | node = dict(attrs) |
| 252 | node["id"] = node_id |
| 253 | nodes.append(node) |
| 254 | |
| 255 | edges = [] |
| 256 | for index, (source, target, attrs) in enumerate(graph.edges(data=True), 1): |
| 257 | edge = dict(attrs) |
| 258 | edge["source"] = edge.get("_src", edge.get("source", source)) |
| 259 | edge["target"] = edge.get("_tgt", edge.get("target", target)) |
| 260 | edge.setdefault("id", f"edge_{index}") |
| 261 | edges.append(edge) |
| 262 | return nodes, edges |
| 263 | |
| 264 | |
| 265 | def load_graph(path: str | Path) -> tuple: |
no test coverage detected