|
8554cc4…
|
lmata
|
1 |
""" |
|
8554cc4…
|
lmata
|
2 |
FastAPI framework enricher. |
|
8554cc4…
|
lmata
|
3 |
|
|
8554cc4…
|
lmata
|
4 |
Promotes generic Function/Class nodes to FastAPI semantic types: |
|
8554cc4…
|
lmata
|
5 |
- Route — functions decorated with @app.<method> or @router.<method> |
|
8554cc4…
|
lmata
|
6 |
- Dependency — functions with Depends( in their signature |
|
8554cc4…
|
lmata
|
7 |
- PydanticModel — classes that inherit from BaseModel |
|
8554cc4…
|
lmata
|
8 |
- BackgroundTask — functions decorated with @app.on_event or using BackgroundTasks |
|
8554cc4…
|
lmata
|
9 |
""" |
|
8554cc4…
|
lmata
|
10 |
|
|
8554cc4…
|
lmata
|
11 |
from navegador.enrichment.base import EnrichmentResult, FrameworkEnricher |
|
8554cc4…
|
lmata
|
12 |
from navegador.graph.store import GraphStore |
|
8554cc4…
|
lmata
|
13 |
|
|
8554cc4…
|
lmata
|
14 |
# HTTP verbs recognised as route decorators |
|
8554cc4…
|
lmata
|
15 |
_HTTP_METHODS = ("get", "post", "put", "delete", "patch", "head", "options") |
|
8554cc4…
|
lmata
|
16 |
|
|
8554cc4…
|
lmata
|
17 |
|
|
8554cc4…
|
lmata
|
18 |
class FastAPIEnricher(FrameworkEnricher): |
|
8554cc4…
|
lmata
|
19 |
"""Enriches a navegador graph with FastAPI-specific semantic types.""" |
|
8554cc4…
|
lmata
|
20 |
|
|
8554cc4…
|
lmata
|
21 |
def __init__(self, store: GraphStore) -> None: |
|
8554cc4…
|
lmata
|
22 |
super().__init__(store) |
|
8554cc4…
|
lmata
|
23 |
|
|
8554cc4…
|
lmata
|
24 |
# ── Identity ────────────────────────────────────────────────────────────── |
|
8554cc4…
|
lmata
|
25 |
|
|
8554cc4…
|
lmata
|
26 |
@property |
|
8554cc4…
|
lmata
|
27 |
def framework_name(self) -> str: |
|
8554cc4…
|
lmata
|
28 |
return "fastapi" |
|
8554cc4…
|
lmata
|
29 |
|
|
8554cc4…
|
lmata
|
30 |
@property |
|
8554cc4…
|
lmata
|
31 |
def detection_patterns(self) -> list[str]: |
|
2c266d2…
|
lmata
|
32 |
return ["fastapi"] |
|
8554cc4…
|
lmata
|
33 |
|
|
8554cc4…
|
lmata
|
34 |
# ── Enrichment ──────────────────────────────────────────────────────────── |
|
8554cc4…
|
lmata
|
35 |
|
|
8554cc4…
|
lmata
|
36 |
def enrich(self) -> EnrichmentResult: |
|
8554cc4…
|
lmata
|
37 |
result = EnrichmentResult() |
|
8554cc4…
|
lmata
|
38 |
|
|
8554cc4…
|
lmata
|
39 |
routes = self._enrich_routes() |
|
8554cc4…
|
lmata
|
40 |
result.promoted += routes |
|
8554cc4…
|
lmata
|
41 |
result.patterns_found["routes"] = routes |
|
8554cc4…
|
lmata
|
42 |
|
|
8554cc4…
|
lmata
|
43 |
dependencies = self._enrich_dependencies() |
|
8554cc4…
|
lmata
|
44 |
result.promoted += dependencies |
|
8554cc4…
|
lmata
|
45 |
result.patterns_found["dependencies"] = dependencies |
|
8554cc4…
|
lmata
|
46 |
|
|
8554cc4…
|
lmata
|
47 |
pydantic_models = self._enrich_pydantic_models() |
|
8554cc4…
|
lmata
|
48 |
result.promoted += pydantic_models |
|
8554cc4…
|
lmata
|
49 |
result.patterns_found["pydantic_models"] = pydantic_models |
|
8554cc4…
|
lmata
|
50 |
|
|
8554cc4…
|
lmata
|
51 |
background_tasks = self._enrich_background_tasks() |
|
8554cc4…
|
lmata
|
52 |
result.promoted += background_tasks |
|
8554cc4…
|
lmata
|
53 |
result.patterns_found["background_tasks"] = background_tasks |
|
8554cc4…
|
lmata
|
54 |
|
|
8554cc4…
|
lmata
|
55 |
return result |
|
8554cc4…
|
lmata
|
56 |
|
|
8554cc4…
|
lmata
|
57 |
# ── Pattern helpers ─────────────────────────────────────────────────────── |
|
8554cc4…
|
lmata
|
58 |
|
|
8554cc4…
|
lmata
|
59 |
def _enrich_routes(self) -> int: |
|
8554cc4…
|
lmata
|
60 |
""" |
|
8554cc4…
|
lmata
|
61 |
Find Function/Method nodes linked to Decorator nodes whose names match |
|
8554cc4…
|
lmata
|
62 |
@app.<method> or @router.<method> patterns (e.g. app.get, router.post). |
|
8554cc4…
|
lmata
|
63 |
|
|
8554cc4…
|
lmata
|
64 |
Falls back to querying the ``signature`` and ``docstring`` properties for |
|
8554cc4…
|
lmata
|
65 |
route decorator patterns when no Decorator nodes are present in the graph. |
|
8554cc4…
|
lmata
|
66 |
""" |
|
8554cc4…
|
lmata
|
67 |
promoted = 0 |
|
8554cc4…
|
lmata
|
68 |
|
|
8554cc4…
|
lmata
|
69 |
# Strategy 1: Decorator nodes connected via DECORATES edges |
|
8554cc4…
|
lmata
|
70 |
for http_method in _HTTP_METHODS: |
|
8554cc4…
|
lmata
|
71 |
result = self.store.query( |
|
8554cc4…
|
lmata
|
72 |
"MATCH (d:Decorator)-[:DECORATES]->(n) " |
|
8554cc4…
|
lmata
|
73 |
"WHERE d.name CONTAINS $pattern " |
|
8554cc4…
|
lmata
|
74 |
"RETURN n.name, n.file_path", |
|
8554cc4…
|
lmata
|
75 |
{"pattern": f".{http_method}"}, |
|
8554cc4…
|
lmata
|
76 |
) |
|
8554cc4…
|
lmata
|
77 |
rows = result.result_set or [] |
|
8554cc4…
|
lmata
|
78 |
for row in rows: |
|
8554cc4…
|
lmata
|
79 |
name, file_path = row[0], row[1] |
|
8554cc4…
|
lmata
|
80 |
if name and file_path: |
|
e5e1a3b…
|
lmata
|
81 |
self._promote_node(name, file_path, "Route", {"http_method": http_method}) |
|
8554cc4…
|
lmata
|
82 |
promoted += 1 |
|
8554cc4…
|
lmata
|
83 |
|
|
8554cc4…
|
lmata
|
84 |
# Strategy 2: signature / docstring heuristics (no Decorator nodes) |
|
8554cc4…
|
lmata
|
85 |
for http_method in _HTTP_METHODS: |
|
8554cc4…
|
lmata
|
86 |
for prop in ("signature", "docstring"): |
|
8554cc4…
|
lmata
|
87 |
result = self.store.query( |
|
8554cc4…
|
lmata
|
88 |
f"MATCH (n) WHERE (n:Function OR n:Method) " |
|
8554cc4…
|
lmata
|
89 |
f"AND n.{prop} IS NOT NULL " |
|
8554cc4…
|
lmata
|
90 |
f"AND n.{prop} CONTAINS $pattern " |
|
8554cc4…
|
lmata
|
91 |
"RETURN n.name, n.file_path", |
|
8554cc4…
|
lmata
|
92 |
{"pattern": f".{http_method}("}, |
|
8554cc4…
|
lmata
|
93 |
) |
|
8554cc4…
|
lmata
|
94 |
rows = result.result_set or [] |
|
8554cc4…
|
lmata
|
95 |
for row in rows: |
|
8554cc4…
|
lmata
|
96 |
name, file_path = row[0], row[1] |
|
8554cc4…
|
lmata
|
97 |
if name and file_path: |
|
e5e1a3b…
|
lmata
|
98 |
self._promote_node(name, file_path, "Route", {"http_method": http_method}) |
|
8554cc4…
|
lmata
|
99 |
promoted += 1 |
|
8554cc4…
|
lmata
|
100 |
|
|
8554cc4…
|
lmata
|
101 |
return promoted |
|
8554cc4…
|
lmata
|
102 |
|
|
8554cc4…
|
lmata
|
103 |
def _enrich_dependencies(self) -> int: |
|
8554cc4…
|
lmata
|
104 |
""" |
|
8554cc4…
|
lmata
|
105 |
Find Function/Method nodes whose signature contains ``Depends(``. |
|
8554cc4…
|
lmata
|
106 |
These are FastAPI dependency-injection callables. |
|
8554cc4…
|
lmata
|
107 |
""" |
|
8554cc4…
|
lmata
|
108 |
promoted = 0 |
|
8554cc4…
|
lmata
|
109 |
|
|
8554cc4…
|
lmata
|
110 |
for prop in ("signature", "docstring"): |
|
8554cc4…
|
lmata
|
111 |
result = self.store.query( |
|
8554cc4…
|
lmata
|
112 |
f"MATCH (n) WHERE (n:Function OR n:Method) " |
|
8554cc4…
|
lmata
|
113 |
f"AND n.{prop} IS NOT NULL " |
|
8554cc4…
|
lmata
|
114 |
f"AND n.{prop} CONTAINS $pattern " |
|
8554cc4…
|
lmata
|
115 |
"RETURN n.name, n.file_path", |
|
8554cc4…
|
lmata
|
116 |
{"pattern": "Depends("}, |
|
8554cc4…
|
lmata
|
117 |
) |
|
8554cc4…
|
lmata
|
118 |
rows = result.result_set or [] |
|
8554cc4…
|
lmata
|
119 |
for row in rows: |
|
8554cc4…
|
lmata
|
120 |
name, file_path = row[0], row[1] |
|
8554cc4…
|
lmata
|
121 |
if name and file_path: |
|
8554cc4…
|
lmata
|
122 |
self._promote_node(name, file_path, "Dependency") |
|
8554cc4…
|
lmata
|
123 |
promoted += 1 |
|
8554cc4…
|
lmata
|
124 |
|
|
8554cc4…
|
lmata
|
125 |
# Also match via Decorator nodes named "Depends" |
|
8554cc4…
|
lmata
|
126 |
result = self.store.query( |
|
8554cc4…
|
lmata
|
127 |
"MATCH (d:Decorator)-[:DECORATES]->(n) " |
|
8554cc4…
|
lmata
|
128 |
"WHERE d.name CONTAINS $pattern " |
|
8554cc4…
|
lmata
|
129 |
"RETURN n.name, n.file_path", |
|
8554cc4…
|
lmata
|
130 |
{"pattern": "Depends"}, |
|
8554cc4…
|
lmata
|
131 |
) |
|
8554cc4…
|
lmata
|
132 |
rows = result.result_set or [] |
|
8554cc4…
|
lmata
|
133 |
for row in rows: |
|
8554cc4…
|
lmata
|
134 |
name, file_path = row[0], row[1] |
|
8554cc4…
|
lmata
|
135 |
if name and file_path: |
|
8554cc4…
|
lmata
|
136 |
self._promote_node(name, file_path, "Dependency") |
|
8554cc4…
|
lmata
|
137 |
promoted += 1 |
|
8554cc4…
|
lmata
|
138 |
|
|
8554cc4…
|
lmata
|
139 |
return promoted |
|
8554cc4…
|
lmata
|
140 |
|
|
8554cc4…
|
lmata
|
141 |
def _enrich_pydantic_models(self) -> int: |
|
8554cc4…
|
lmata
|
142 |
""" |
|
8554cc4…
|
lmata
|
143 |
Find Class nodes that inherit from ``BaseModel`` via INHERITS edges, |
|
8554cc4…
|
lmata
|
144 |
or whose name appears as a base in the raw INHERITS graph. |
|
8554cc4…
|
lmata
|
145 |
""" |
|
8554cc4…
|
lmata
|
146 |
promoted = 0 |
|
8554cc4…
|
lmata
|
147 |
|
|
8554cc4…
|
lmata
|
148 |
# Primary: INHERITS edges pointing to a node named "BaseModel" |
|
8554cc4…
|
lmata
|
149 |
result = self.store.query( |
|
8554cc4…
|
lmata
|
150 |
"MATCH (n:Class)-[:INHERITS]->(base) " |
|
8554cc4…
|
lmata
|
151 |
"WHERE base.name = $base_name " |
|
8554cc4…
|
lmata
|
152 |
"RETURN n.name, n.file_path", |
|
8554cc4…
|
lmata
|
153 |
{"base_name": "BaseModel"}, |
|
8554cc4…
|
lmata
|
154 |
) |
|
8554cc4…
|
lmata
|
155 |
rows = result.result_set or [] |
|
8554cc4…
|
lmata
|
156 |
for row in rows: |
|
8554cc4…
|
lmata
|
157 |
name, file_path = row[0], row[1] |
|
8554cc4…
|
lmata
|
158 |
if name and file_path: |
|
8554cc4…
|
lmata
|
159 |
self._promote_node(name, file_path, "PydanticModel") |
|
8554cc4…
|
lmata
|
160 |
promoted += 1 |
|
8554cc4…
|
lmata
|
161 |
|
|
8554cc4…
|
lmata
|
162 |
# Fallback: docstring or source mentions BaseModel |
|
8554cc4…
|
lmata
|
163 |
result = self.store.query( |
|
8554cc4…
|
lmata
|
164 |
"MATCH (n:Class) " |
|
8554cc4…
|
lmata
|
165 |
"WHERE n.docstring IS NOT NULL AND n.docstring CONTAINS $pattern " |
|
8554cc4…
|
lmata
|
166 |
"RETURN n.name, n.file_path", |
|
8554cc4…
|
lmata
|
167 |
{"pattern": "BaseModel"}, |
|
8554cc4…
|
lmata
|
168 |
) |
|
8554cc4…
|
lmata
|
169 |
rows = result.result_set or [] |
|
8554cc4…
|
lmata
|
170 |
for row in rows: |
|
8554cc4…
|
lmata
|
171 |
name, file_path = row[0], row[1] |
|
8554cc4…
|
lmata
|
172 |
if name and file_path: |
|
8554cc4…
|
lmata
|
173 |
self._promote_node(name, file_path, "PydanticModel") |
|
8554cc4…
|
lmata
|
174 |
promoted += 1 |
|
8554cc4…
|
lmata
|
175 |
|
|
8554cc4…
|
lmata
|
176 |
return promoted |
|
8554cc4…
|
lmata
|
177 |
|
|
8554cc4…
|
lmata
|
178 |
def _enrich_background_tasks(self) -> int: |
|
8554cc4…
|
lmata
|
179 |
""" |
|
8554cc4…
|
lmata
|
180 |
Find Function/Method nodes that: |
|
8554cc4…
|
lmata
|
181 |
- are decorated with @app.on_event (via Decorator nodes or signature), or |
|
8554cc4…
|
lmata
|
182 |
- reference ``BackgroundTasks`` in their signature / docstring. |
|
8554cc4…
|
lmata
|
183 |
""" |
|
8554cc4…
|
lmata
|
184 |
promoted = 0 |
|
8554cc4…
|
lmata
|
185 |
|
|
8554cc4…
|
lmata
|
186 |
# Strategy 1: Decorator nodes matching on_event |
|
8554cc4…
|
lmata
|
187 |
result = self.store.query( |
|
8554cc4…
|
lmata
|
188 |
"MATCH (d:Decorator)-[:DECORATES]->(n) " |
|
8554cc4…
|
lmata
|
189 |
"WHERE d.name CONTAINS $pattern " |
|
8554cc4…
|
lmata
|
190 |
"RETURN n.name, n.file_path", |
|
8554cc4…
|
lmata
|
191 |
{"pattern": "on_event"}, |
|
8554cc4…
|
lmata
|
192 |
) |
|
8554cc4…
|
lmata
|
193 |
rows = result.result_set or [] |
|
8554cc4…
|
lmata
|
194 |
for row in rows: |
|
8554cc4…
|
lmata
|
195 |
name, file_path = row[0], row[1] |
|
8554cc4…
|
lmata
|
196 |
if name and file_path: |
|
8554cc4…
|
lmata
|
197 |
self._promote_node(name, file_path, "BackgroundTask") |
|
8554cc4…
|
lmata
|
198 |
promoted += 1 |
|
8554cc4…
|
lmata
|
199 |
|
|
8554cc4…
|
lmata
|
200 |
# Strategy 2: signature / docstring heuristics for on_event and BackgroundTasks |
|
8554cc4…
|
lmata
|
201 |
for pattern in ("on_event(", "BackgroundTasks"): |
|
8554cc4…
|
lmata
|
202 |
for prop in ("signature", "docstring"): |
|
8554cc4…
|
lmata
|
203 |
result = self.store.query( |
|
8554cc4…
|
lmata
|
204 |
f"MATCH (n) WHERE (n:Function OR n:Method) " |
|
8554cc4…
|
lmata
|
205 |
f"AND n.{prop} IS NOT NULL " |
|
8554cc4…
|
lmata
|
206 |
f"AND n.{prop} CONTAINS $pattern " |
|
8554cc4…
|
lmata
|
207 |
"RETURN n.name, n.file_path", |
|
8554cc4…
|
lmata
|
208 |
{"pattern": pattern}, |
|
8554cc4…
|
lmata
|
209 |
) |
|
8554cc4…
|
lmata
|
210 |
rows = result.result_set or [] |
|
8554cc4…
|
lmata
|
211 |
for row in rows: |
|
8554cc4…
|
lmata
|
212 |
name, file_path = row[0], row[1] |
|
8554cc4…
|
lmata
|
213 |
if name and file_path: |
|
8554cc4…
|
lmata
|
214 |
self._promote_node(name, file_path, "BackgroundTask") |
|
8554cc4…
|
lmata
|
215 |
promoted += 1 |
|
8554cc4…
|
lmata
|
216 |
|
|
8554cc4…
|
lmata
|
217 |
return promoted |