|
| 1 | +"""Walk the symbol-table tree and stamp every node with its can:// id.""" |
| 2 | +from __future__ import annotations |
| 3 | +from typing import Dict |
| 4 | +from codeanalyzer.schema import ids |
| 5 | +from codeanalyzer.schema.py_schema import PyApplication, PyModule, PyClass, PyCallable |
| 6 | + |
| 7 | + |
| 8 | +def assign_ids(app: PyApplication, app_name: str) -> Dict[str, str]: |
| 9 | + """Sets `.id` on the app + every module/class/callable. Returns a |
| 10 | + `signature -> can://id` map for later stages (identity layer input).""" |
| 11 | + app.id = ids.application_id(app_name); app.kind = "application" |
| 12 | + sig_to_id: Dict[str, str] = {} |
| 13 | + |
| 14 | + def do_callable(parent_id: str, c: PyCallable) -> None: |
| 15 | + seg = ids.callable_sig_segment(c.name, [p.name for p in c.parameters]) |
| 16 | + c.id = ids.child_id(parent_id, seg) |
| 17 | + sig_to_id[c.signature] = c.id |
| 18 | + for ic in (c.inner_callables or {}).values(): |
| 19 | + do_callable(c.id, ic) |
| 20 | + for icl in (c.inner_classes or {}).values(): |
| 21 | + do_class(c.id, icl) |
| 22 | + |
| 23 | + def do_class(parent_id: str, cl: PyClass) -> None: |
| 24 | + cl.id = ids.child_id(parent_id, cl.name); cl.kind = "class" |
| 25 | + sig_to_id[cl.signature] = cl.id |
| 26 | + for m in (cl.methods or {}).values(): |
| 27 | + do_callable(cl.id, m) |
| 28 | + for ic in (cl.inner_classes or {}).values(): |
| 29 | + do_class(cl.id, ic) |
| 30 | + |
| 31 | + for file_key, mod in app.symbol_table.items(): |
| 32 | + mod.id = ids.module_id(app_name, file_key); mod.kind = "module" |
| 33 | + for fn in (mod.functions or {}).values(): |
| 34 | + do_callable(mod.id, fn) |
| 35 | + for cl in (mod.classes or {}).values(): |
| 36 | + do_class(mod.id, cl) |
| 37 | + return sig_to_id |
0 commit comments