Load graph.json. Returns normalized (nodes, edges, hyperedges, metadata).
(path: str | Path)
| 253 | |
| 254 | |
| 255 | def load_graph(path: str | Path) -> tuple: |
| 256 | """Load graph.json. Returns normalized (nodes, edges, hyperedges, metadata).""" |
| 257 | if path: |
| 258 | from graphify.security import check_graph_file_size_cap |
| 259 | try: |
| 260 | check_graph_file_size_cap(Path(path)) |
| 261 | except ValueError as exc: |
| 262 | raise SystemExit(f"ERROR: {exc}") from exc |
| 263 | data = read_json(path) |
| 264 | if not isinstance(data, dict): |
| 265 | raise SystemExit(f"ERROR: graph file must contain a JSON object: {path}") |
| 266 | |
| 267 | graph_block = data.get("graph") if isinstance(data.get("graph"), dict) else {} |
| 268 | meta_block = data.get("metadata") if isinstance(data.get("metadata"), dict) else {} |
| 269 | |
| 270 | node_link = _node_link_payload(data) |
| 271 | if node_link: |
| 272 | raw_nodes, raw_edges = node_link |
| 273 | else: |
| 274 | raw_nodes = first_list(data.get("nodes"), data.get("vertices"), graph_block.get("nodes"), graph_block.get("vertices")) |
| 275 | raw_edges = first_list(data.get("links"), data.get("edges"), graph_block.get("links"), graph_block.get("edges")) |
| 276 | hyperedges = first_list(data.get("hyperedges"), graph_block.get("hyperedges"), data.get("groups"), graph_block.get("groups")) |
| 277 | |
| 278 | nodes = [normalize_node(n, i) for i, n in enumerate(raw_nodes) if isinstance(n, dict)] |
| 279 | edges = [] |
| 280 | for i, raw_edge in enumerate(raw_edges): |
| 281 | if not isinstance(raw_edge, dict): |
| 282 | continue |
| 283 | edge = normalize_edge(raw_edge, i) |
| 284 | if edge: |
| 285 | edges.append(edge) |
| 286 | |
| 287 | meta = dict(graph_block) |
| 288 | meta.update(meta_block) |
| 289 | for key in ("built_at_commit", "commit", "project_name", "repo", "repository", "language_breakdown"): |
| 290 | if data.get(key) and not meta.get(key): |
| 291 | meta[key] = data.get(key) |
| 292 | if meta.get("commit") and not meta.get("built_at_commit"): |
| 293 | meta["built_at_commit"] = meta["commit"] |
| 294 | |
| 295 | return nodes, edges, hyperedges, meta |
| 296 | |
| 297 | |
| 298 | def load_labels(path: str | Path | None) -> dict: |