codegraphcontext-rust 0.9.24__cp314-cp314-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. codegraphcontext/__init__.py +2 -0
  2. codegraphcontext/__main__.py +5 -0
  3. codegraphcontext/_cgc_rust.cp314-win_amd64.pyd +4 -0
  4. codegraphcontext/cli/__init__.py +1 -0
  5. codegraphcontext/cli/cli_helpers.py +774 -0
  6. codegraphcontext/cli/config_manager.py +1051 -0
  7. codegraphcontext/cli/entry.py +21 -0
  8. codegraphcontext/cli/main.py +2386 -0
  9. codegraphcontext/cli/registry_commands.py +404 -0
  10. codegraphcontext/cli/setup_macos.py +93 -0
  11. codegraphcontext/cli/setup_wizard.py +991 -0
  12. codegraphcontext/cli/visualizer.py +50 -0
  13. codegraphcontext/core/__init__.py +165 -0
  14. codegraphcontext/core/bundle_registry.py +181 -0
  15. codegraphcontext/core/cgc_bundle.py +858 -0
  16. codegraphcontext/core/cgcignore.py +118 -0
  17. codegraphcontext/core/database.py +273 -0
  18. codegraphcontext/core/database_falkordb.py +480 -0
  19. codegraphcontext/core/database_falkordb_remote.py +199 -0
  20. codegraphcontext/core/database_kuzu.py +636 -0
  21. codegraphcontext/core/falkor_worker.py +132 -0
  22. codegraphcontext/core/jobs.py +132 -0
  23. codegraphcontext/core/watcher.py +260 -0
  24. codegraphcontext/prompts.py +125 -0
  25. codegraphcontext/server.py +471 -0
  26. codegraphcontext/tool_definitions.py +220 -0
  27. codegraphcontext/tools/__init__.py +0 -0
  28. codegraphcontext/tools/advanced_language_query_tool.py +103 -0
  29. codegraphcontext/tools/code_finder.py +1160 -0
  30. codegraphcontext/tools/graph_builder.py +337 -0
  31. codegraphcontext/tools/handlers/analysis_handlers.py +114 -0
  32. codegraphcontext/tools/handlers/indexing_handlers.py +116 -0
  33. codegraphcontext/tools/handlers/management_handlers.py +339 -0
  34. codegraphcontext/tools/handlers/query_handlers.py +83 -0
  35. codegraphcontext/tools/handlers/watcher_handlers.py +83 -0
  36. codegraphcontext/tools/indexing/__init__.py +1 -0
  37. codegraphcontext/tools/indexing/angular_routes.py +396 -0
  38. codegraphcontext/tools/indexing/cgc_wiki_cli.py +1334 -0
  39. codegraphcontext/tools/indexing/constants.py +70 -0
  40. codegraphcontext/tools/indexing/context_generator.py +461 -0
  41. codegraphcontext/tools/indexing/discovery.py +64 -0
  42. codegraphcontext/tools/indexing/engine.py +114 -0
  43. codegraphcontext/tools/indexing/execution_flows.py +355 -0
  44. codegraphcontext/tools/indexing/module_grouping.py +551 -0
  45. codegraphcontext/tools/indexing/noise_filter.py +131 -0
  46. codegraphcontext/tools/indexing/op_param_extraction.py +287 -0
  47. codegraphcontext/tools/indexing/persistence/__init__.py +3 -0
  48. codegraphcontext/tools/indexing/persistence/duckdb_writer.py +1116 -0
  49. codegraphcontext/tools/indexing/persistence/parquet_loader.py +643 -0
  50. codegraphcontext/tools/indexing/persistence/writer.py +1562 -0
  51. codegraphcontext/tools/indexing/pipeline.py +171 -0
  52. codegraphcontext/tools/indexing/pre_scan.py +105 -0
  53. codegraphcontext/tools/indexing/rationale_extraction.py +154 -0
  54. codegraphcontext/tools/indexing/resolution/__init__.py +8 -0
  55. codegraphcontext/tools/indexing/resolution/calls.py +253 -0
  56. codegraphcontext/tools/indexing/resolution/inheritance.py +91 -0
  57. codegraphcontext/tools/indexing/route_extraction.py +659 -0
  58. codegraphcontext/tools/indexing/sanitize.py +41 -0
  59. codegraphcontext/tools/indexing/schema.py +79 -0
  60. codegraphcontext/tools/indexing/schema_contract.py +44 -0
  61. codegraphcontext/tools/indexing/scip_pipeline.py +140 -0
  62. codegraphcontext/tools/languages/c.py +563 -0
  63. codegraphcontext/tools/languages/cpp.py +616 -0
  64. codegraphcontext/tools/languages/csharp.py +551 -0
  65. codegraphcontext/tools/languages/dart.py +378 -0
  66. codegraphcontext/tools/languages/elixir.py +460 -0
  67. codegraphcontext/tools/languages/go.py +499 -0
  68. codegraphcontext/tools/languages/haskell.py +426 -0
  69. codegraphcontext/tools/languages/java.py +500 -0
  70. codegraphcontext/tools/languages/javascript.py +589 -0
  71. codegraphcontext/tools/languages/kotlin.py +640 -0
  72. codegraphcontext/tools/languages/perl.py +260 -0
  73. codegraphcontext/tools/languages/php.py +519 -0
  74. codegraphcontext/tools/languages/python.py +578 -0
  75. codegraphcontext/tools/languages/ruby.py +537 -0
  76. codegraphcontext/tools/languages/rust.py +295 -0
  77. codegraphcontext/tools/languages/scala.py +519 -0
  78. codegraphcontext/tools/languages/swift.py +491 -0
  79. codegraphcontext/tools/languages/typescript.py +606 -0
  80. codegraphcontext/tools/languages/typescriptjsx.py +151 -0
  81. codegraphcontext/tools/package_resolver.py +472 -0
  82. codegraphcontext/tools/query_tool_languages/c_toolkit.py +5 -0
  83. codegraphcontext/tools/query_tool_languages/cpp_toolkit.py +87 -0
  84. codegraphcontext/tools/query_tool_languages/csharp_toolkit.py +5 -0
  85. codegraphcontext/tools/query_tool_languages/dart_toolkit.py +5 -0
  86. codegraphcontext/tools/query_tool_languages/go_toolkit.py +5 -0
  87. codegraphcontext/tools/query_tool_languages/haskell_toolkit.py +5 -0
  88. codegraphcontext/tools/query_tool_languages/java_toolkit.py +5 -0
  89. codegraphcontext/tools/query_tool_languages/javascript_toolkit.py +5 -0
  90. codegraphcontext/tools/query_tool_languages/perl_toolkit.py +5 -0
  91. codegraphcontext/tools/query_tool_languages/python_toolkit.py +5 -0
  92. codegraphcontext/tools/query_tool_languages/ruby_toolkit.py +5 -0
  93. codegraphcontext/tools/query_tool_languages/rust_toolkit.py +5 -0
  94. codegraphcontext/tools/query_tool_languages/scala_toolkit.py +5 -0
  95. codegraphcontext/tools/query_tool_languages/swift_toolkit.py +5 -0
  96. codegraphcontext/tools/query_tool_languages/typescript_toolkit.py +5 -0
  97. codegraphcontext/tools/scip_indexer.py +467 -0
  98. codegraphcontext/tools/scip_pb2.py +2455 -0
  99. codegraphcontext/tools/system.py +133 -0
  100. codegraphcontext/tools/tree_sitter_parser.py +103 -0
  101. codegraphcontext/utils/debug_log.py +90 -0
  102. codegraphcontext/utils/path_ignore.py +55 -0
  103. codegraphcontext/utils/repo_path.py +27 -0
  104. codegraphcontext/utils/tree_sitter_manager.py +285 -0
  105. codegraphcontext/utils/visualize_graph.py +0 -0
  106. codegraphcontext/viz/server.py +282 -0
  107. codegraphcontext_rust-0.9.24.dist-info/METADATA +543 -0
  108. codegraphcontext_rust-0.9.24.dist-info/RECORD +112 -0
  109. codegraphcontext_rust-0.9.24.dist-info/WHEEL +4 -0
  110. codegraphcontext_rust-0.9.24.dist-info/entry_points.txt +4 -0
  111. codegraphcontext_rust-0.9.24.dist-info/licenses/LICENSE +21 -0
  112. codegraphcontext_rust-0.9.24.dist-info/sboms/cgc-python.cyclonedx.json +2302 -0
@@ -0,0 +1,578 @@
1
+ import os
2
+ import tempfile
3
+ import nbformat
4
+ from nbconvert import PythonExporter
5
+ from pathlib import Path
6
+ from typing import Any, Dict, Optional, Tuple
7
+ import ast
8
+ import logging
9
+ import warnings
10
+ from codegraphcontext.utils.debug_log import debug_log, info_logger, error_logger, warning_logger, debug_logger
11
+ from codegraphcontext.utils.tree_sitter_manager import execute_query
12
+
13
+ # Suppress verbose traitlets/nbconvert DEBUG logs
14
+ logging.getLogger('traitlets').setLevel(logging.WARNING)
15
+ logging.getLogger('nbconvert').setLevel(logging.WARNING)
16
+
17
+ # Suppress IPython UserWarning from nbconvert
18
+ warnings.filterwarnings('ignore', message='.*IPython is needed to transform IPython syntax.*')
19
+
20
+
21
+ PY_QUERIES = {
22
+ "imports": """
23
+ (import_statement name: (_) @import)
24
+ (import_from_statement) @from_import_stmt
25
+ """,
26
+ "classes": """
27
+ (class_definition
28
+ name: (identifier) @name
29
+ superclasses: (argument_list)? @superclasses
30
+ body: (block) @body)
31
+ """,
32
+ "functions": """
33
+ (function_definition
34
+ name: (identifier) @name
35
+ parameters: (parameters) @parameters
36
+ body: (block) @body
37
+ return_type: (_)? @return_type)
38
+ """,
39
+ "calls": """
40
+ (call
41
+ function: (identifier) @name)
42
+ (call
43
+ function: (attribute attribute: (identifier) @name) @full_call)
44
+ """,
45
+ "variables": """
46
+ (assignment
47
+ left: (identifier) @name)
48
+ """,
49
+ "lambda_assignments": """
50
+ (assignment
51
+ left: (identifier) @name
52
+ right: (lambda) @lambda_node)
53
+ """,
54
+ "docstrings": """
55
+ (expression_statement (string) @docstring)
56
+ """,
57
+ "dict_method_refs": """
58
+ (dictionary
59
+ (pair
60
+ key: (_) @key
61
+ value: (attribute) @method_ref))
62
+ """,
63
+ }
64
+
65
+ class PythonTreeSitterParser:
66
+ """A Python-specific parser using tree-sitter, encapsulating language-specific logic."""
67
+
68
+ def __init__(self, generic_parser_wrapper):
69
+ self.generic_parser_wrapper = generic_parser_wrapper
70
+ self.language_name = generic_parser_wrapper.language_name
71
+ self.language = generic_parser_wrapper.language
72
+ self.parser = generic_parser_wrapper.parser
73
+
74
+ def _get_node_text(self, node) -> str:
75
+ return node.text.decode('utf-8')
76
+
77
+ def _get_parent_context(self, node, types=('function_definition', 'class_definition')):
78
+ curr = node.parent
79
+ while curr:
80
+ if curr.type in types:
81
+ name_node = curr.child_by_field_name('name')
82
+ return self._get_node_text(name_node) if name_node else None, curr.type, curr.start_point[0] + 1
83
+ curr = curr.parent
84
+ return None, None, None
85
+
86
+ def _calculate_complexity(self, node):
87
+ complexity_nodes = {
88
+ "if_statement", "for_statement", "while_statement", "except_clause",
89
+ "with_statement", "boolean_operator", "list_comprehension",
90
+ "generator_expression", "case_clause"
91
+ }
92
+ count = 1
93
+
94
+ def traverse(n):
95
+ nonlocal count
96
+ if n.type in complexity_nodes:
97
+ count += 1
98
+ for child in n.children:
99
+ traverse(child)
100
+
101
+ traverse(node)
102
+ return count
103
+
104
+ def _get_docstring(self, body_node):
105
+ if body_node and body_node.child_count > 0:
106
+ first_child = body_node.children[0]
107
+ if first_child.type == 'expression_statement' and first_child.children[0].type == 'string':
108
+ try:
109
+ return ast.literal_eval(self._get_node_text(first_child.children[0]))
110
+ except (ValueError, SyntaxError):
111
+ return self._get_node_text(first_child.children[0])
112
+ return None
113
+
114
+ def parse(self, path: Path, is_dependency: bool = False, is_notebook: bool = False, index_source: bool = False) -> Dict:
115
+ """Parses a file and returns its structure in a standardized dictionary format."""
116
+ original_file_path = path
117
+ temp_py_file = None
118
+ source_code = None
119
+ self.index_source = index_source
120
+
121
+ try:
122
+ if is_notebook:
123
+ info_logger(f"Converting notebook {path} to temporary Python file.")
124
+ with open(path, 'r', encoding='utf-8') as f:
125
+ notebook_node = nbformat.read(f, as_version=4)
126
+
127
+ exporter = PythonExporter()
128
+ python_code, _ = exporter.from_notebook_node(notebook_node)
129
+
130
+ with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.py', encoding='utf-8') as tf:
131
+ tf.write(python_code)
132
+ temp_py_file = Path(tf.name)
133
+
134
+ # The file to be parsed is now the temporary file
135
+ path = temp_py_file
136
+
137
+ with open(path, "r", encoding="utf-8") as f:
138
+ source_code = f.read()
139
+
140
+ tree = self.parser.parse(bytes(source_code, "utf8"))
141
+ root_node = tree.root_node
142
+
143
+ functions = self._find_functions(root_node)
144
+ functions.extend(self._find_lambda_assignments(root_node, index_source))
145
+ classes = self._find_classes(root_node)
146
+ imports = self._find_imports(root_node)
147
+ function_calls = self._find_calls(root_node)
148
+ variables = self._find_variables(root_node)
149
+
150
+ return {
151
+ "path": str(original_file_path), # Always return the original path
152
+ "functions": functions,
153
+ "classes": classes,
154
+ "variables": variables,
155
+ "imports": imports,
156
+ "function_calls": function_calls,
157
+ "is_dependency": is_dependency,
158
+ "lang": self.language_name,
159
+ }
160
+ except Exception as e:
161
+ error_logger(f"Failed to parse {original_file_path}: {e}")
162
+ return {"path": str(original_file_path), "error": str(e)}
163
+ finally:
164
+ if temp_py_file and temp_py_file.exists():
165
+ os.remove(temp_py_file)
166
+ info_logger(f"Removed temporary file: {temp_py_file}")
167
+
168
+ def _find_lambda_assignments(self, root_node, index_source: bool = False):
169
+ functions = []
170
+ query_str = PY_QUERIES.get('lambda_assignments')
171
+ if not query_str: return []
172
+
173
+ for match in execute_query(self.language, query_str, root_node):
174
+ capture_name = match[1]
175
+ node = match[0]
176
+
177
+ if capture_name == 'name':
178
+ assignment_node = node.parent
179
+ lambda_node = assignment_node.child_by_field_name('right')
180
+ name = self._get_node_text(node)
181
+ params_node = lambda_node.child_by_field_name('parameters')
182
+
183
+ context, context_type, _ = self._get_parent_context(assignment_node)
184
+ class_context, _, _ = self._get_parent_context(assignment_node, types=('class_definition',))
185
+
186
+ func_data = {
187
+ "name": name,
188
+ "line_number": node.start_point[0] + 1,
189
+ "end_line": assignment_node.end_point[0] + 1,
190
+ "args": [p for p in [self._get_node_text(p) for p in params_node.children if p.type == 'identifier'] if p] if params_node else [],
191
+ "cyclomatic_complexity": 1,
192
+ "context": context,
193
+ "context_type": context_type,
194
+ "class_context": class_context,
195
+ "decorators": [],
196
+ "lang": self.language_name,
197
+ "is_dependency": False,
198
+ }
199
+ if self.index_source:
200
+ func_data["source"] = self._get_node_text(assignment_node)
201
+ func_data["docstring"] = None
202
+
203
+ functions.append(func_data)
204
+ return functions
205
+
206
+ def _find_functions(self, root_node, index_source: bool = False):
207
+ functions = []
208
+ query_str = PY_QUERIES['functions']
209
+ for match in execute_query(self.language, query_str, root_node):
210
+ capture_name = match[1]
211
+ node = match[0]
212
+
213
+ if capture_name == 'name':
214
+ func_node = node.parent
215
+ name = self._get_node_text(node)
216
+ params_node = func_node.child_by_field_name('parameters')
217
+ body_node = func_node.child_by_field_name('body')
218
+
219
+ decorators = [self._get_node_text(child) for child in func_node.children if child.type == 'decorator']
220
+
221
+ context, context_type, _ = self._get_parent_context(func_node)
222
+ class_context, _, _ = self._get_parent_context(func_node, types=('class_definition',))
223
+
224
+ args = []
225
+ if params_node:
226
+ for p in params_node.children:
227
+ arg_text = None
228
+ if p.type == 'identifier':
229
+ # Simple parameter: def foo(x)
230
+ arg_text = self._get_node_text(p)
231
+ elif p.type == 'default_parameter':
232
+ # Parameter with default: def foo(x=5)
233
+ name_node = p.child_by_field_name('name')
234
+ if name_node:
235
+ arg_text = self._get_node_text(name_node)
236
+ elif p.type == 'typed_parameter':
237
+ # Typed parameter: def foo(x: int)
238
+ name_node = p.child_by_field_name('name')
239
+ if name_node:
240
+ arg_text = self._get_node_text(name_node)
241
+ elif p.type == 'typed_default_parameter':
242
+ # Typed parameter with default: def foo(x: int = 5) or def foo(x: str = typer.Argument(...))
243
+ name_node = p.child_by_field_name('name')
244
+ if name_node:
245
+ arg_text = self._get_node_text(name_node)
246
+ elif p.type == 'list_splat_pattern' or p.type == 'dictionary_splat_pattern':
247
+ # *args or **kwargs
248
+ arg_text = self._get_node_text(p)
249
+
250
+ if arg_text:
251
+ args.append(arg_text)
252
+
253
+ func_data = {
254
+ "name": name,
255
+ "line_number": node.start_point[0] + 1,
256
+ "end_line": func_node.end_point[0] + 1,
257
+ "args": args,
258
+ "cyclomatic_complexity": self._calculate_complexity(func_node),
259
+ "context": context,
260
+ "context_type": context_type,
261
+ "class_context": class_context,
262
+ "decorators": [d for d in decorators if d],
263
+ "lang": self.language_name,
264
+ "is_dependency": False,
265
+ }
266
+
267
+ # Always extract docstring (lightweight, no index_source needed)
268
+ func_data["docstring"] = self._get_docstring(body_node)
269
+ if self.index_source:
270
+ func_data["source"] = self._get_node_text(func_node)
271
+
272
+ functions.append(func_data)
273
+ return functions
274
+
275
+ def _find_classes(self, root_node, index_source: bool = False):
276
+ classes = []
277
+ query_str = PY_QUERIES['classes']
278
+ for match in execute_query(self.language, query_str, root_node):
279
+ capture_name = match[1]
280
+ node = match[0]
281
+
282
+ if capture_name == 'name':
283
+ class_node = node.parent
284
+ name = self._get_node_text(node)
285
+ body_node = class_node.child_by_field_name('body')
286
+ superclasses_node = class_node.child_by_field_name('superclasses')
287
+
288
+ bases = []
289
+ if superclasses_node:
290
+ bases = [self._get_node_text(child) for child in superclasses_node.children if child.type in ('identifier', 'attribute')]
291
+
292
+ decorators = [self._get_node_text(child) for child in class_node.children if child.type == 'decorator']
293
+
294
+ context, _, _ = self._get_parent_context(class_node)
295
+
296
+ class_data = {
297
+ "name": name,
298
+ "line_number": node.start_point[0] + 1,
299
+ "end_line": class_node.end_point[0] + 1,
300
+ "bases": [b for b in bases if b],
301
+ "context": context,
302
+ "decorators": [d for d in decorators if d],
303
+ "lang": self.language_name,
304
+ "is_dependency": False,
305
+ }
306
+ # Always extract docstring (lightweight, no index_source needed)
307
+ class_data["docstring"] = self._get_docstring(body_node)
308
+ if self.index_source:
309
+ class_data["source"] = self._get_node_text(class_node)
310
+
311
+ classes.append(class_data)
312
+ return classes
313
+
314
+ def _find_imports(self, root_node):
315
+ imports = []
316
+ seen_modules = set()
317
+ query_str = PY_QUERIES['imports']
318
+ for node, capture_name in execute_query(self.language, query_str, root_node):
319
+ if capture_name in ('import', 'from_import_stmt'):
320
+ # For 'import_statement'
321
+ if capture_name == 'import':
322
+ node_text = self._get_node_text(node)
323
+ alias = None
324
+ if ' as ' in node_text:
325
+ parts = node_text.split(' as ')
326
+ full_name = parts[0].strip()
327
+ alias = parts[1].strip()
328
+ else:
329
+ full_name = node_text.strip()
330
+
331
+ if full_name in seen_modules:
332
+ continue
333
+ seen_modules.add(full_name)
334
+
335
+ import_data = {
336
+ "name": full_name,
337
+ "full_import_name": full_name,
338
+ "line_number": node.start_point[0] + 1,
339
+ "alias": alias,
340
+ "context": self._get_parent_context(node)[:2],
341
+ "lang": self.language_name,
342
+ "is_dependency": False,
343
+ }
344
+ imports.append(import_data)
345
+ # For 'import_from_statement'
346
+ elif capture_name == 'from_import_stmt':
347
+ module_name_node = node.child_by_field_name('module_name')
348
+ if not module_name_node: continue
349
+
350
+ module_name = self._get_node_text(module_name_node)
351
+
352
+ # Handle 'from ... import ...'
353
+ import_list_node = node.child_by_field_name('name')
354
+ if import_list_node:
355
+ for child in import_list_node.children:
356
+ imported_name = None
357
+ alias = None
358
+ if child.type == 'aliased_import':
359
+ name_node = child.child_by_field_name('name')
360
+ alias_node = child.child_by_field_name('alias')
361
+ if name_node: imported_name = self._get_node_text(name_node)
362
+ if alias_node: alias = self._get_node_text(alias_node)
363
+ elif child.type == 'dotted_name' or child.type == 'identifier':
364
+ imported_name = self._get_node_text(child)
365
+
366
+ if imported_name:
367
+ full_import_name = f"{module_name}.{imported_name}"
368
+ if full_import_name in seen_modules:
369
+ continue
370
+ seen_modules.add(full_import_name)
371
+ imports.append({
372
+ "name": imported_name,
373
+ "full_import_name": full_import_name,
374
+ "line_number": child.start_point[0] + 1,
375
+ "alias": alias,
376
+ "context": self._get_parent_context(child)[:2],
377
+ "lang": self.language_name,
378
+ "is_dependency": False,
379
+ })
380
+
381
+ return imports
382
+
383
+ def _find_calls(self, root_node):
384
+ calls = []
385
+
386
+ # First, find all direct function calls
387
+ query_str = PY_QUERIES['calls']
388
+ for node, capture_name in execute_query(self.language, query_str, root_node):
389
+ if capture_name == 'name':
390
+ call_node = node.parent if node.parent.type == 'call' else node.parent.parent
391
+ full_call_node = call_node.child_by_field_name('function')
392
+
393
+ args = []
394
+ arguments_node = call_node.child_by_field_name('arguments')
395
+ if arguments_node:
396
+ for arg in arguments_node.children:
397
+ arg_text = self._get_node_text(arg)
398
+ if arg_text and arg_text not in ('(', ')', ','):
399
+ args.append(arg_text)
400
+
401
+ call_data = {
402
+ "name": self._get_node_text(node),
403
+ "full_name": self._get_node_text(full_call_node),
404
+ "line_number": node.start_point[0] + 1,
405
+ "args": args,
406
+ "inferred_obj_type": None,
407
+ "context": self._get_parent_context(node),
408
+ "class_context": self._get_parent_context(node, types=('class_definition',))[:2],
409
+ "lang": self.language_name,
410
+ "is_dependency": False,
411
+ }
412
+ calls.append(call_data)
413
+
414
+ # Second, find dictionary-based method references (indirect calls)
415
+ # This handles patterns like: tool_map = {"name": self.method, ...}
416
+ # followed by: handler = tool_map.get(name); handler()
417
+ dict_method_calls = self._find_dict_method_references(root_node)
418
+ calls.extend(dict_method_calls)
419
+
420
+ return calls
421
+
422
+ def _find_dict_method_references(self, root_node):
423
+ """
424
+ Detects indirect function calls through dictionary mappings.
425
+
426
+ Example pattern:
427
+ tool_map = {
428
+ "add_code": self.add_code_to_graph_tool,
429
+ "find_code": self.find_code_tool,
430
+ }
431
+ handler = tool_map.get(tool_name)
432
+ if handler:
433
+ handler(**args)
434
+
435
+ This creates CALLS relationships from the context function to all
436
+ methods referenced in the dictionary.
437
+ """
438
+ calls = []
439
+ query_str = PY_QUERIES.get('dict_method_refs')
440
+ if not query_str:
441
+ return calls
442
+
443
+ # Track dictionaries that contain method references
444
+ dict_assignments = {} # dict_var_name -> list of method references
445
+
446
+ for node, capture_name in execute_query(self.language, query_str, root_node):
447
+ if capture_name == 'method_ref':
448
+ # Found a method reference in a dictionary value
449
+ # Navigate up to find the assignment
450
+ dict_node = node.parent # pair node
451
+ while dict_node and dict_node.type != 'dictionary':
452
+ dict_node = dict_node.parent
453
+
454
+ if dict_node:
455
+ # Find the assignment node
456
+ assignment_node = dict_node.parent
457
+ if assignment_node and assignment_node.type == 'assignment':
458
+ # Get the variable name being assigned
459
+ left_node = assignment_node.child_by_field_name('left')
460
+ if left_node:
461
+ var_name = self._get_node_text(left_node)
462
+ method_ref = self._get_node_text(node)
463
+
464
+ # Extract just the method name (remove 'self.')
465
+ method_name = method_ref.split('.')[-1] if '.' in method_ref else method_ref
466
+
467
+ if var_name not in dict_assignments:
468
+ dict_assignments[var_name] = {
469
+ 'methods': [],
470
+ 'context': self._get_parent_context(assignment_node),
471
+ 'line_number': assignment_node.start_point[0] + 1
472
+ }
473
+
474
+ dict_assignments[var_name]['methods'].append({
475
+ 'name': method_name,
476
+ 'full_name': method_ref,
477
+ 'line_number': node.start_point[0] + 1
478
+ })
479
+
480
+ # Now create call relationships for each method in the dictionaries
481
+ # The context is the function where the dictionary is defined
482
+ for dict_var, data in dict_assignments.items():
483
+ context, context_type, context_line = data['context']
484
+ class_context, _, _ = (None, None, None)
485
+
486
+ for method_info in data['methods']:
487
+ call_data = {
488
+ "name": method_info['name'],
489
+ "full_name": method_info['full_name'],
490
+ "line_number": method_info['line_number'],
491
+ "args": [], # We don't know the args at this point
492
+ "inferred_obj_type": None,
493
+ "context": (context, context_type, context_line),
494
+ "class_context": (class_context, None),
495
+ "lang": self.language_name,
496
+ "is_dependency": False,
497
+ "is_indirect_call": True, # Mark as indirect for debugging
498
+ }
499
+ calls.append(call_data)
500
+
501
+ return calls
502
+
503
+ def _find_variables(self, root_node):
504
+ variables = []
505
+ query_str = PY_QUERIES['variables']
506
+ for match in execute_query(self.language, query_str, root_node):
507
+ capture_name = match[1]
508
+ node = match[0]
509
+
510
+ if capture_name == 'name':
511
+ assignment_node = node.parent
512
+
513
+ # Skip lambda assignments, they are handled by _find_lambda_assignments
514
+ right_node = assignment_node.child_by_field_name('right')
515
+ if right_node and right_node.type == 'lambda':
516
+ continue
517
+
518
+ name = self._get_node_text(node)
519
+ value = self._get_node_text(right_node) if right_node else None
520
+
521
+ type_node = assignment_node.child_by_field_name('type')
522
+ type_text = self._get_node_text(type_node) if type_node else None
523
+
524
+ context, _, _ = self._get_parent_context(node)
525
+ class_context, _, _ = self._get_parent_context(node, types=('class_definition',))
526
+
527
+ variable_data = {
528
+ "name": name,
529
+ "line_number": node.start_point[0] + 1,
530
+ "value": value,
531
+ "type": type_text,
532
+ "context": context,
533
+ "class_context": class_context,
534
+ "lang": self.language_name,
535
+ "is_dependency": False,
536
+ }
537
+ variables.append(variable_data)
538
+ return variables
539
+
540
+ def pre_scan_python(files: list[Path], parser_wrapper) -> dict:
541
+ """Scans Python files to create a map of class/function names to their file paths."""
542
+ imports_map = {}
543
+ query_str = """
544
+ (class_definition name: (identifier) @name)
545
+ (function_definition name: (identifier) @name)
546
+ """
547
+
548
+ for path in files:
549
+ temp_py_file = None
550
+ try:
551
+ source_to_parse = ""
552
+ if path.suffix == '.ipynb':
553
+ with open(path, 'r', encoding='utf-8') as f:
554
+ notebook_node = nbformat.read(f, as_version=4)
555
+ exporter = PythonExporter()
556
+ python_code, _ = exporter.from_notebook_node(notebook_node)
557
+ with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.py', encoding='utf-8') as tf:
558
+ tf.write(python_code)
559
+ temp_py_file = Path(tf.name)
560
+ with open(temp_py_file, "r", encoding="utf-8") as f:
561
+ source_to_parse = f.read()
562
+ else:
563
+ with open(path, "r", encoding="utf-8") as f:
564
+ source_to_parse = f.read()
565
+
566
+ tree = parser_wrapper.parser.parse(bytes(source_to_parse, "utf8"))
567
+
568
+ for capture, _ in execute_query(parser_wrapper.language, query_str, tree.root_node):
569
+ name = capture.text.decode('utf-8')
570
+ if name not in imports_map:
571
+ imports_map[name] = []
572
+ imports_map[name].append(str(path.resolve()))
573
+ except Exception as e:
574
+ warning_logger(f"Tree-sitter pre-scan failed for {path}: {e}")
575
+ finally:
576
+ if temp_py_file and temp_py_file.exists():
577
+ os.remove(temp_py_file)
578
+ return imports_map