pecto-python 0.1.1

Python behavior extractor (FastAPI, Flask, Django)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
use super::common::*;
use crate::context::ParsedFile;
use pecto_core::model::*;
use std::collections::BTreeMap;

/// Extract endpoints from a Python file containing FastAPI, Flask, or Django routes.
pub fn extract(file: &ParsedFile) -> Option<Capability> {
    let root = file.tree.root_node();
    let source = file.source.as_bytes();

    let mut endpoints = Vec::new();

    for i in 0..root.named_child_count() {
        let node = root.named_child(i).unwrap();

        if node.kind() == "decorated_definition" {
            let decorators = collect_decorators(&node, source);
            let inner = match get_inner_definition(&node) {
                Some(n) => n,
                None => continue,
            };

            if inner.kind() == "function_definition" {
                // FastAPI/Flask route decorators
                for dec in &decorators {
                    if let Some(endpoint) = extract_route_endpoint(&inner, source, dec) {
                        endpoints.push(endpoint);
                    }
                }

                // Django REST Framework @api_view
                if let Some(endpoint) = extract_drf_api_view(&inner, source, &decorators) {
                    endpoints.push(endpoint);
                }
            }

            if inner.kind() == "class_definition" {
                // Django REST Framework ViewSets
                extract_drf_viewset(&inner, source, &decorators, &mut endpoints);
            }
        }
    }

    if endpoints.is_empty() {
        return None;
    }

    // Derive capability name from file
    let file_stem = file
        .path
        .rsplit('/')
        .next()
        .unwrap_or(&file.path)
        .trim_end_matches(".py");
    let capability_name = to_kebab_case(
        &file_stem
            .replace("_routes", "")
            .replace("_views", "")
            .replace("_router", "")
            .replace("_api", ""),
    );

    let mut capability = Capability::new(capability_name, file.path.clone());
    capability.endpoints = endpoints;
    Some(capability)
}

/// Extract endpoint from FastAPI/Flask route decorator.
fn extract_route_endpoint(
    func_node: &tree_sitter::Node,
    source: &[u8],
    decorator: &DecoratorInfo,
) -> Option<Endpoint> {
    let http_method = match decorator.name.as_str() {
        "get" | "GET" => HttpMethod::Get,
        "post" | "POST" => HttpMethod::Post,
        "put" | "PUT" => HttpMethod::Put,
        "delete" | "DELETE" => HttpMethod::Delete,
        "patch" | "PATCH" => HttpMethod::Patch,
        "route" => {
            // Flask @app.route — check methods kwarg
            extract_flask_method(&decorator.args)
        }
        _ => return None,
    };

    // Don't match standalone decorators that aren't route-like
    if !decorator.full_name.contains('.')
        && !matches!(
            decorator.name.as_str(),
            "get" | "post" | "put" | "delete" | "patch"
        )
    {
        return None;
    }

    // Extract path from first argument
    let path = decorator
        .args
        .first()
        .map(|a| clean_string_literal(a))
        .unwrap_or_default();

    if path.is_empty() && decorator.name != "route" {
        return None;
    }

    let _func_name = get_def_name(func_node, source);

    // Extract parameters from function signature
    let input = extract_function_params(func_node, source);

    // Check for security dependencies (FastAPI Depends)
    let security = extract_security(func_node, source);

    let behaviors = vec![Behavior {
        name: "success".to_string(),
        condition: None,
        returns: ResponseSpec {
            status: default_status(&http_method),
            body: extract_return_type(func_node, source),
        },
        side_effects: Vec::new(),
    }];

    Some(Endpoint {
        method: http_method,
        path,
        input,
        validation: Vec::new(),
        behaviors,
        security,
    })
}

/// Extract HTTP method from Flask @app.route(methods=["GET", "POST"])
fn extract_flask_method(args: &[String]) -> HttpMethod {
    for arg in args {
        if arg.contains("POST") {
            return HttpMethod::Post;
        }
        if arg.contains("PUT") {
            return HttpMethod::Put;
        }
        if arg.contains("DELETE") {
            return HttpMethod::Delete;
        }
        if arg.contains("PATCH") {
            return HttpMethod::Patch;
        }
    }
    HttpMethod::Get
}

/// Extract endpoint from DRF @api_view decorator.
fn extract_drf_api_view(
    func_node: &tree_sitter::Node,
    source: &[u8],
    decorators: &[DecoratorInfo],
) -> Option<Endpoint> {
    let api_view = decorators.iter().find(|d| d.name == "api_view")?;

    let method = if !api_view.args.is_empty() {
        extract_flask_method(&api_view.args)
    } else {
        HttpMethod::Get
    };

    let func_name = get_def_name(func_node, source);
    let path = format!("/{}", func_name.replace('_', "-"));

    Some(Endpoint {
        method,
        path,
        input: None,
        validation: Vec::new(),
        behaviors: vec![Behavior {
            name: "success".to_string(),
            condition: None,
            returns: ResponseSpec {
                status: 200,
                body: None,
            },
            side_effects: Vec::new(),
        }],
        security: None,
    })
}

/// Extract CRUD endpoints from DRF ViewSet class.
fn extract_drf_viewset(
    class_node: &tree_sitter::Node,
    source: &[u8],
    _decorators: &[DecoratorInfo],
    endpoints: &mut Vec<Endpoint>,
) {
    // Check if class inherits from known ViewSet bases
    let bases = get_class_bases(class_node, source);
    let is_viewset = bases.iter().any(|b| {
        b.contains("ViewSet")
            || b.contains("ModelViewSet")
            || b.contains("GenericAPIView")
            || b.contains("APIView")
    });

    if !is_viewset {
        return;
    }

    let class_name = get_def_name(class_node, source);
    let base_path = format!(
        "/{}",
        to_kebab_case(&class_name.replace("ViewSet", "").replace("View", ""))
    );

    let is_model_viewset = bases.iter().any(|b| b.contains("ModelViewSet"));

    if is_model_viewset {
        // ModelViewSet provides standard CRUD
        let crud = [
            (HttpMethod::Get, format!("{}/", base_path), "list"),
            (HttpMethod::Post, format!("{}/", base_path), "create"),
            (HttpMethod::Get, format!("{}/:id/", base_path), "retrieve"),
            (HttpMethod::Put, format!("{}/:id/", base_path), "update"),
            (HttpMethod::Delete, format!("{}/:id/", base_path), "destroy"),
        ];
        for (method, path, _name) in crud {
            endpoints.push(Endpoint {
                method,
                path,
                input: None,
                validation: Vec::new(),
                behaviors: vec![Behavior {
                    name: "success".to_string(),
                    condition: None,
                    returns: ResponseSpec {
                        status: 200,
                        body: None,
                    },
                    side_effects: Vec::new(),
                }],
                security: None,
            });
        }
    }
}

fn get_class_bases(class_node: &tree_sitter::Node, source: &[u8]) -> Vec<String> {
    let mut bases = Vec::new();
    if let Some(arg_list) = class_node.child_by_field_name("superclasses") {
        for i in 0..arg_list.named_child_count() {
            let arg = arg_list.named_child(i).unwrap();
            bases.push(node_text(&arg, source));
        }
    }
    bases
}

fn extract_function_params(func_node: &tree_sitter::Node, source: &[u8]) -> Option<EndpointInput> {
    let params = func_node.child_by_field_name("parameters")?;
    let mut path_params = Vec::new();
    let mut body = None;

    for i in 0..params.named_child_count() {
        let param = params.named_child(i).unwrap();

        let param_name = match param.kind() {
            "typed_parameter" | "typed_default_parameter" => param
                .child_by_field_name("name")
                .map(|n| node_text(&n, source))
                .unwrap_or_default(),
            "identifier" => node_text(&param, source),
            _ => continue,
        };

        // Skip self, request, db, response
        if matches!(
            param_name.as_str(),
            "self" | "request" | "db" | "response" | "session"
        ) {
            continue;
        }

        // Check type annotation
        let type_ann = param
            .child_by_field_name("type")
            .map(|t| node_text(&t, source));

        if let Some(ref t) = type_ann {
            // If type looks like a model (PascalCase), it's a body
            if t.chars().next().is_some_and(|c| c.is_uppercase())
                && !t.starts_with("Optional")
                && !t.starts_with("int")
                && !t.starts_with("str")
                && !t.starts_with("float")
                && !t.starts_with("bool")
            {
                body = Some(TypeRef {
                    name: t.clone(),
                    fields: BTreeMap::new(),
                });
                continue;
            }
        }

        // Simple types are path params
        if !param_name.is_empty() {
            path_params.push(Param {
                name: param_name,
                param_type: type_ann.unwrap_or_else(|| "str".to_string()),
                required: true,
            });
        }
    }

    if body.is_none() && path_params.is_empty() {
        return None;
    }

    Some(EndpointInput {
        body,
        path_params,
        query_params: Vec::new(),
    })
}

fn extract_security(func_node: &tree_sitter::Node, source: &[u8]) -> Option<SecurityConfig> {
    let params = func_node.child_by_field_name("parameters")?;
    let text = node_text(&params, source);

    // FastAPI: Depends(get_current_user)
    if text.contains("Depends") && (text.contains("current_user") || text.contains("auth")) {
        return Some(SecurityConfig {
            authentication: Some("required".to_string()),
            roles: Vec::new(),
            rate_limit: None,
            cors: None,
        });
    }

    None
}

fn extract_return_type(func_node: &tree_sitter::Node, source: &[u8]) -> Option<TypeRef> {
    let return_type = func_node.child_by_field_name("return_type")?;
    let type_text = node_text(&return_type, source);

    if type_text == "None" || type_text.is_empty() {
        return None;
    }

    Some(TypeRef {
        name: type_text,
        fields: BTreeMap::new(),
    })
}

fn default_status(method: &HttpMethod) -> u16 {
    match method {
        HttpMethod::Post => 201,
        HttpMethod::Delete => 204,
        _ => 200,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::context::ParsedFile;

    fn parse_file(source: &str, path: &str) -> ParsedFile {
        ParsedFile::parse(source.to_string(), path.to_string()).unwrap()
    }

    #[test]
    fn test_fastapi_routes() {
        let source = r#"
from fastapi import APIRouter, Depends

router = APIRouter()

@router.get("/users/{user_id}")
async def get_user(user_id: int) -> User:
    return db.get(user_id)

@router.post("/users")
async def create_user(user: UserCreate, current_user: User = Depends(get_current_user)):
    return db.create(user)

@router.delete("/users/{user_id}")
async def delete_user(user_id: int):
    db.delete(user_id)
"#;

        let file = parse_file(source, "routes/users.py");
        let capability = extract(&file).unwrap();

        assert_eq!(capability.name, "users");
        assert_eq!(capability.endpoints.len(), 3);

        let get = &capability.endpoints[0];
        assert!(matches!(get.method, HttpMethod::Get));
        assert_eq!(get.path, "/users/{user_id}");

        let post = &capability.endpoints[1];
        assert!(matches!(post.method, HttpMethod::Post));

        let delete = &capability.endpoints[2];
        assert!(matches!(delete.method, HttpMethod::Delete));
    }

    #[test]
    fn test_flask_routes() {
        let source = r#"
from flask import Blueprint

bp = Blueprint('items', __name__)

@bp.route("/items", methods=["GET"])
def list_items():
    return jsonify(items)

@bp.route("/items", methods=["POST"])
def create_item():
    return jsonify(item), 201
"#;

        let file = parse_file(source, "views/items.py");
        let capability = extract(&file).unwrap();

        assert_eq!(capability.name, "items");
        assert_eq!(capability.endpoints.len(), 2);

        assert!(matches!(capability.endpoints[0].method, HttpMethod::Get));
        assert!(matches!(capability.endpoints[1].method, HttpMethod::Post));
    }

    #[test]
    fn test_drf_api_view() {
        let source = r#"
from rest_framework.decorators import api_view

@api_view(['GET', 'POST'])
def user_list(request):
    pass
"#;

        let file = parse_file(source, "views/users.py");
        let capability = extract(&file).unwrap();

        assert_eq!(capability.endpoints.len(), 1);
    }

    #[test]
    fn test_no_routes() {
        let source = r#"
def helper_function():
    return 42

class Calculator:
    def add(self, a, b):
        return a + b
"#;

        let file = parse_file(source, "utils.py");
        assert!(extract(&file).is_none());
    }
}