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

Class Graph

scripts/theme_gate.py:139–179  ·  view source on GitHub ↗

Read-only view over graph.json, reimplemented here on purpose.

Source from the content-addressed store, hash-verified

137# ---------------------------------------------------------------- graph index
138
139class Graph:
140 """Read-only view over graph.json, reimplemented here on purpose."""
141
142 def __init__(self, path: Path, root: Path) -> None:
143 data = json.loads(path.read_text(encoding="utf-8"))
144 self.root = root.resolve().as_posix().rstrip("/") + "/"
145 self.nodes: dict[str, dict] = {n["id"]: n for n in data.get("nodes", []) if "id" in n}
146 edges = data.get("links", data.get("edges", []))
147 self.edges_at: dict[tuple[str, int], list[dict]] = {}
148 self.out: dict[str, list[dict]] = {}
149 for e in edges:
150 src_file = self.rel(e.get("source_file"))
151 for ln in self._lines(e, src_file):
152 self.edges_at.setdefault((src_file, ln), []).append(e)
153 self.out.setdefault(e.get("source"), []).append(e)
154 self.nodes_at: dict[tuple[str, int], list[dict]] = {}
155 for n in self.nodes.values():
156 m = re.fullmatch(r"L(\d+)", str(n.get("source_location") or ""))
157 if m:
158 self.nodes_at.setdefault((self.rel(n.get("source_file")), int(m.group(1))), []).append(n)
159
160 def rel(self, f: object) -> str:
161 s = str(f or "").replace("\\", "/")
162 return s[len(self.root):] if s.startswith(self.root) else s
163
164 def _lines(self, e: dict, src_file: str) -> set[int]:
165 lines: set[int] = set()
166 for ev in (e, *[a for a in (e.get("also_seen") or []) if isinstance(a, dict)]):
167 if ev is not e and ev.get("source_file") not in (None, "") and self.rel(ev["source_file"]) != src_file:
168 continue
169 m = re.fullmatch(r"L(\d+)", str(ev.get("source_location") or ""))
170 if m:
171 lines.add(int(m.group(1)))
172 return lines
173
174 def targets(self, file: str, line: int) -> list[dict]:
175 """Destination nodes of every edge seen at ``file:line``."""
176 return [self.nodes[e["target"]] for e in self.edges_at.get((file, line), []) if e.get("target") in self.nodes]
177
178 def target_edges(self, file: str, line: int) -> list[tuple[dict, dict]]:
179 return [(e, self.nodes[e["target"]]) for e in self.edges_at.get((file, line), []) if e.get("target") in self.nodes]
180
181
182def _is_unresolved(n: dict) -> bool:

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected