|
8c1f142…
|
lmata
|
1 |
""" |
|
8c1f142…
|
lmata
|
2 |
FrameworkEnricher base class. |
|
8c1f142…
|
lmata
|
3 |
|
|
8c1f142…
|
lmata
|
4 |
Post-processing step that runs after AST ingestion. Examines existing graph |
|
8c1f142…
|
lmata
|
5 |
nodes and promotes generic Function/Class nodes to semantic framework types |
|
8c1f142…
|
lmata
|
6 |
by adding labels/properties. |
|
8c1f142…
|
lmata
|
7 |
""" |
|
8c1f142…
|
lmata
|
8 |
|
|
8c1f142…
|
lmata
|
9 |
from abc import ABC, abstractmethod |
|
8c1f142…
|
lmata
|
10 |
|
|
8c1f142…
|
lmata
|
11 |
from navegador.graph.store import GraphStore |
|
8c1f142…
|
lmata
|
12 |
|
|
8c1f142…
|
lmata
|
13 |
|
|
8c1f142…
|
lmata
|
14 |
class EnrichmentResult: |
|
8c1f142…
|
lmata
|
15 |
"""Result of an enrichment pass.""" |
|
8c1f142…
|
lmata
|
16 |
|
|
8c1f142…
|
lmata
|
17 |
def __init__(self): |
|
8c1f142…
|
lmata
|
18 |
self.promoted: int = 0 # nodes that got framework-specific labels |
|
8c1f142…
|
lmata
|
19 |
self.edges_added: int = 0 # new semantic edges added |
|
8c1f142…
|
lmata
|
20 |
self.patterns_found: dict[str, int] = {} # pattern_name -> count |
|
8c1f142…
|
lmata
|
21 |
|
|
8c1f142…
|
lmata
|
22 |
|
|
8c1f142…
|
lmata
|
23 |
class FrameworkEnricher(ABC): |
|
8c1f142…
|
lmata
|
24 |
"""Base class for framework-specific graph enrichment.""" |
|
8c1f142…
|
lmata
|
25 |
|
|
8c1f142…
|
lmata
|
26 |
def __init__(self, store: GraphStore): |
|
8c1f142…
|
lmata
|
27 |
self.store = store |
|
8c1f142…
|
lmata
|
28 |
|
|
8c1f142…
|
lmata
|
29 |
@property |
|
8c1f142…
|
lmata
|
30 |
@abstractmethod |
|
8c1f142…
|
lmata
|
31 |
def framework_name(self) -> str: |
|
8c1f142…
|
lmata
|
32 |
"""Name of the framework (e.g. 'django', 'fastapi').""" |
|
8c1f142…
|
lmata
|
33 |
|
|
8c1f142…
|
lmata
|
34 |
@property |
|
8c1f142…
|
lmata
|
35 |
@abstractmethod |
|
8c1f142…
|
lmata
|
36 |
def detection_patterns(self) -> list[str]: |
|
2c266d2…
|
lmata
|
37 |
"""Import module names that indicate this framework is in use. |
|
2c266d2…
|
lmata
|
38 |
|
|
2c266d2…
|
lmata
|
39 |
Detection queries Import nodes for exact module matches, so use |
|
2c266d2…
|
lmata
|
40 |
the actual package name (e.g. 'django', 'fastapi', 'express'). |
|
2c266d2…
|
lmata
|
41 |
""" |
|
2c266d2…
|
lmata
|
42 |
|
|
2c266d2…
|
lmata
|
43 |
@property |
|
2c266d2…
|
lmata
|
44 |
def detection_files(self) -> list[str]: |
|
2c266d2…
|
lmata
|
45 |
"""Filenames whose presence confirms the framework. |
|
8c1f142…
|
lmata
|
46 |
|
|
2c266d2…
|
lmata
|
47 |
Override this to add file-based detection (e.g. 'manage.py' for |
|
2c266d2…
|
lmata
|
48 |
Django, 'Gemfile' for Rails). File names are matched exactly |
|
2c266d2…
|
lmata
|
49 |
against File node names — not as substrings. |
|
8c1f142…
|
lmata
|
50 |
""" |
|
2c266d2…
|
lmata
|
51 |
return [] |
|
8c1f142…
|
lmata
|
52 |
|
|
8c1f142…
|
lmata
|
53 |
@abstractmethod |
|
8c1f142…
|
lmata
|
54 |
def enrich(self) -> EnrichmentResult: |
|
8c1f142…
|
lmata
|
55 |
"""Run enrichment on the current graph.""" |
|
8c1f142…
|
lmata
|
56 |
|
|
8c1f142…
|
lmata
|
57 |
def detect(self) -> bool: |
|
2c266d2…
|
lmata
|
58 |
"""Check if the framework is present by looking for real imports and marker files.""" |
|
2c266d2…
|
lmata
|
59 |
# Check Import nodes for actual framework imports |
|
8c1f142…
|
lmata
|
60 |
for pattern in self.detection_patterns: |
|
8c1f142…
|
lmata
|
61 |
result = self.store.query( |
|
b45288f…
|
lmata
|
62 |
"MATCH (n:Import) WHERE n.name = $name OR n.module = $name RETURN count(n) AS c", |
|
2c266d2…
|
lmata
|
63 |
{"name": pattern}, |
|
2c266d2…
|
lmata
|
64 |
) |
|
2c266d2…
|
lmata
|
65 |
rows = result.result_set or [] |
|
2c266d2…
|
lmata
|
66 |
if rows and rows[0][0] > 0: |
|
2c266d2…
|
lmata
|
67 |
return True |
|
2c266d2…
|
lmata
|
68 |
|
|
2c266d2…
|
lmata
|
69 |
# Check for marker files by exact filename match |
|
2c266d2…
|
lmata
|
70 |
for filename in self.detection_files: |
|
2c266d2…
|
lmata
|
71 |
result = self.store.query( |
|
b45288f…
|
lmata
|
72 |
"MATCH (f:File) WHERE f.name = $name RETURN count(f) AS c", |
|
2c266d2…
|
lmata
|
73 |
{"name": filename}, |
|
8c1f142…
|
lmata
|
74 |
) |
|
8c1f142…
|
lmata
|
75 |
rows = result.result_set or [] |
|
8c1f142…
|
lmata
|
76 |
if rows and rows[0][0] > 0: |
|
8c1f142…
|
lmata
|
77 |
return True |
|
2c266d2…
|
lmata
|
78 |
|
|
8c1f142…
|
lmata
|
79 |
return False |
|
8c1f142…
|
lmata
|
80 |
|
|
8c1f142…
|
lmata
|
81 |
def _promote_node( |
|
8c1f142…
|
lmata
|
82 |
self, name: str, file_path: str, semantic_type: str, props: dict = None |
|
8c1f142…
|
lmata
|
83 |
) -> None: |
|
8c1f142…
|
lmata
|
84 |
"""Add a semantic_type property to an existing node.""" |
|
8c1f142…
|
lmata
|
85 |
extra = "" |
|
8c1f142…
|
lmata
|
86 |
if props: |
|
8c1f142…
|
lmata
|
87 |
extra = ", " + ", ".join(f"n.{k} = ${k}" for k in props) |
|
8c1f142…
|
lmata
|
88 |
self.store.query( |
|
8c1f142…
|
lmata
|
89 |
f"MATCH (n) WHERE n.name = $name AND n.file_path = $file_path " |
|
8c1f142…
|
lmata
|
90 |
f"SET n.semantic_type = $semantic_type{extra}", |
|
8c1f142…
|
lmata
|
91 |
{"name": name, "file_path": file_path, "semantic_type": semantic_type, **(props or {})}, |
|
8c1f142…
|
lmata
|
92 |
) |
|
8c1f142…
|
lmata
|
93 |
|
|
8c1f142…
|
lmata
|
94 |
def _add_semantic_edge( |
|
8c1f142…
|
lmata
|
95 |
self, from_name: str, edge_type: str, to_name: str, props: dict = None |
|
8c1f142…
|
lmata
|
96 |
) -> None: |
|
8c1f142…
|
lmata
|
97 |
"""Create a semantic edge between two nodes by name.""" |
|
8c1f142…
|
lmata
|
98 |
extra = "" |
|
8c1f142…
|
lmata
|
99 |
if props: |
|
8c1f142…
|
lmata
|
100 |
extra = " SET " + ", ".join(f"r.{k} = ${k}" for k in props) |
|
8c1f142…
|
lmata
|
101 |
self.store.query( |
|
8c1f142…
|
lmata
|
102 |
f"MATCH (a), (b) WHERE a.name = $from_name AND b.name = $to_name " |
|
8c1f142…
|
lmata
|
103 |
f"MERGE (a)-[r:{edge_type}]->(b){extra}", |
|
8c1f142…
|
lmata
|
104 |
{"from_name": from_name, "to_name": to_name, **(props or {})}, |
|
8c1f142…
|
lmata
|
105 |
) |