codeanalyzer-python 0.1.3__py3-none-any.whl → 0.1.5__py3-none-any.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.
@@ -1,13 +1,14 @@
1
+ import ast
2
+ import tokenize
3
+ from ast import AST, ClassDef
1
4
  from io import StringIO
2
5
  from pathlib import Path
3
- import tokenize
4
6
  from typing import Dict, List, Optional
5
- import astor
7
+
6
8
  import jedi
7
- from codeanalyzer.utils import logger
8
- from jedi.api.project import Project
9
9
  from jedi.api import Script
10
- from rich.progress import track
10
+ from jedi.api.project import Project
11
+
11
12
  from codeanalyzer.schema.py_schema import (
12
13
  PyCallable,
13
14
  PyCallableParameter,
@@ -20,9 +21,7 @@ from codeanalyzer.schema.py_schema import (
20
21
  PySymbol,
21
22
  PyVariableDeclaration,
22
23
  )
23
- import ast
24
- from ast import AST, ClassDef
25
-
24
+ from codeanalyzer.utils import logger
26
25
  from codeanalyzer.utils.progress_bar import ProgressBar
27
26
 
28
27
 
@@ -98,13 +97,13 @@ class SymbolTableBuilder:
98
97
 
99
98
  return (
100
99
  PyModule.builder()
101
- .with_file_path(str(py_file))
102
- .with_module_name(py_file.stem)
103
- .with_comments(self._pycomments(module, source))
104
- .with_imports(self._imports(module))
105
- .with_variables(self._module_variables(module, script))
106
- .with_classes(classes)
107
- .with_functions(functions)
100
+ .file_path(str(py_file))
101
+ .module_name(py_file.stem)
102
+ .comments(self._pycomments(module, source))
103
+ .imports(self._imports(module))
104
+ .variables(self._module_variables(module, script))
105
+ .classes(classes)
106
+ .functions(functions)
108
107
  .build()
109
108
  )
110
109
 
@@ -126,13 +125,13 @@ class SymbolTableBuilder:
126
125
  for alias in node.names:
127
126
  imports.append(
128
127
  PyImport.builder()
129
- .with_module(alias.name) # for "import os", alias.name = "os"
130
- .with_name(alias.asname or alias.name) # name in local scope
131
- .with_alias(alias.name if alias.asname else None)
132
- .with_start_line(getattr(node, "lineno", -1))
133
- .with_end_line(getattr(node, "end_lineno", node.lineno))
134
- .with_start_column(getattr(node, "col_offset", -1))
135
- .with_end_column(getattr(node, "end_col_offset", -1))
128
+ .module(alias.name) # for "import os", alias.name = "os"
129
+ .name(alias.asname or alias.name) # name in local scope
130
+ .alias(alias.name if alias.asname else None)
131
+ .start_line(getattr(node, "lineno", -1))
132
+ .end_line(getattr(node, "end_lineno", node.lineno))
133
+ .start_column(getattr(node, "col_offset", -1))
134
+ .end_column(getattr(node, "end_col_offset", -1))
136
135
  .build()
137
136
  )
138
137
 
@@ -145,13 +144,13 @@ class SymbolTableBuilder:
145
144
  qualified_module = "." * node.level + (module_name or "")
146
145
  imports.append(
147
146
  PyImport.builder()
148
- .with_module(qualified_module)
149
- .with_name(alias.asname or alias.name)
150
- .with_alias(alias.name if alias.asname else None)
151
- .with_start_line(getattr(node, "lineno", -1))
152
- .with_end_line(getattr(node, "end_lineno", node.lineno))
153
- .with_start_column(getattr(node, "col_offset", -1))
154
- .with_end_column(getattr(node, "end_col_offset", -1))
147
+ .module(qualified_module)
148
+ .name(alias.asname or alias.name)
149
+ .alias(alias.name if alias.asname else None)
150
+ .start_line(getattr(node, "lineno", -1))
151
+ .end_line(getattr(node, "end_lineno", node.lineno))
152
+ .start_column(getattr(node, "col_offset", -1))
153
+ .end_column(getattr(node, "end_col_offset", -1))
155
154
  .build()
156
155
  )
157
156
 
@@ -183,30 +182,30 @@ class SymbolTableBuilder:
183
182
  f"{script.path.__str__().replace('/', '.').replace('.py', '')}.{class_node.name}",
184
183
  )
185
184
 
186
- code: str = astor.to_source(class_node).strip()
185
+ code: str = ast.unparse(class_node).strip()
187
186
 
188
187
  py_class = (
189
188
  PyClass.builder()
190
- .with_name(class_node.name)
191
- .with_signature(signature)
192
- .with_start_line(class_node.lineno)
193
- .with_end_line(
189
+ .name(class_node.name)
190
+ .signature(signature)
191
+ .start_line(class_node.lineno)
192
+ .end_line(
194
193
  getattr(
195
194
  class_node, "end_lineno", class_node.lineno + len(class_node.body)
196
195
  )
197
196
  )
198
- .with_comments(self._pycomments(class_node, code))
199
- .with_code(code)
200
- .with_base_classes(
197
+ .comments(self._pycomments(class_node, code))
198
+ .code(code)
199
+ .base_classes(
201
200
  [
202
201
  ast.unparse(base)
203
202
  for base in class_node.bases
204
203
  if isinstance(base, ast.expr)
205
204
  ]
206
205
  )
207
- .with_methods(self._callables(class_node, script))
208
- .with_attributes(self._class_attributes(class_node, script))
209
- .with_inner_classes(
206
+ .methods(self._callables(class_node, script))
207
+ .attributes(self._class_attributes(class_node, script))
208
+ .inner_classes(
210
209
  {
211
210
  k: v
212
211
  for child in class_node.body
@@ -243,7 +242,7 @@ class SymbolTableBuilder:
243
242
  child, "end_lineno", start_line + len(child.body)
244
243
  )
245
244
  code_start_line = child.body[0].lineno if child.body else start_line
246
- code = astor.to_source(child).strip()
245
+ code: str = ast.unparse(child).strip()
247
246
  decorators = [ast.unparse(d) for d in child.decorator_list]
248
247
 
249
248
  try:
@@ -260,27 +259,27 @@ class SymbolTableBuilder:
260
259
 
261
260
  callables[method_name] = (
262
261
  PyCallable.builder()
263
- .with_name(method_name)
264
- .with_path(script.path.__str__())
265
- .with_signature(signature)
266
- .with_decorators(decorators)
267
- .with_code(code)
268
- .with_start_line(start_line)
269
- .with_end_line(end_line)
270
- .with_code_start_line(code_start_line)
271
- .with_accessed_symbols(self._accessed_symbols(child, script))
272
- .with_call_sites(self._call_sites(child, script))
273
- .with_local_variables(self._local_variables(child, script))
274
- .with_cyclomatic_complexity(self._cyclomatic_complexity(child))
275
- .with_parameters(self._callable_parameters(child, script))
276
- .with_return_type(
262
+ .name(method_name)
263
+ .path(script.path.__str__())
264
+ .signature(signature)
265
+ .decorators(decorators)
266
+ .code(code)
267
+ .start_line(start_line)
268
+ .end_line(end_line)
269
+ .code_start_line(code_start_line)
270
+ .accessed_symbols(self._accessed_symbols(child, script))
271
+ .call_sites(self._call_sites(child, script))
272
+ .local_variables(self._local_variables(child, script))
273
+ .cyclomatic_complexity(self._cyclomatic_complexity(child))
274
+ .parameters(self._callable_parameters(child, script))
275
+ .return_type(
277
276
  ast.unparse(child.returns)
278
277
  if child.returns
279
278
  else self._infer_type(
280
279
  script, child.lineno, child.col_offset
281
280
  )
282
281
  )
283
- .with_comments(self._pycomments(child, code))
282
+ .comments(self._pycomments(child, code))
284
283
  .build()
285
284
  )
286
285
 
@@ -327,12 +326,12 @@ class SymbolTableBuilder:
327
326
 
328
327
  comments.append(
329
328
  PyComment.builder()
330
- .with_content(docstring_content)
331
- .with_start_line(start_line)
332
- .with_end_line(end_line)
333
- .with_start_column(start_column)
334
- .with_end_column(end_column)
335
- .with_is_docstring(True)
329
+ .content(docstring_content)
330
+ .start_line(start_line)
331
+ .end_line(end_line)
332
+ .start_column(start_column)
333
+ .end_column(end_column)
334
+ .is_docstring(True)
336
335
  .build()
337
336
  )
338
337
 
@@ -348,12 +347,12 @@ class SymbolTableBuilder:
348
347
  comment_text = tok.string.lstrip("#").strip()
349
348
  comments.append(
350
349
  PyComment.builder()
351
- .with_content(comment_text)
352
- .with_start_line(tok_line)
353
- .with_end_line(tok_line)
354
- .with_start_column(tok_col)
355
- .with_end_column(tok_col + len(tok.string))
356
- .with_is_docstring(False)
350
+ .content(comment_text)
351
+ .start_line(tok_line)
352
+ .end_line(tok_line)
353
+ .start_column(tok_col)
354
+ .end_column(tok_col + len(tok.string))
355
+ .is_docstring(False)
357
356
  .build()
358
357
  )
359
358
 
@@ -380,14 +379,14 @@ class SymbolTableBuilder:
380
379
  if isinstance(target, ast.Name):
381
380
  attributes[target.id] = (
382
381
  PyClassAttribute.builder()
383
- .with_name(target.id)
384
- .with_type(
382
+ .name(target.id)
383
+ .type(
385
384
  self._infer_type(
386
385
  script, target.lineno, target.col_offset
387
386
  )
388
387
  )
389
- .with_start_line(getattr(target, "lineno", -1))
390
- .with_end_line(getattr(stmt, "end_lineno", stmt.lineno))
388
+ .start_line(getattr(target, "lineno", -1))
389
+ .end_line(getattr(stmt, "end_lineno", stmt.lineno))
391
390
  .build()
392
391
  )
393
392
 
@@ -396,16 +395,16 @@ class SymbolTableBuilder:
396
395
  if isinstance(target, ast.Name):
397
396
  attributes[target.id] = (
398
397
  PyClassAttribute.builder()
399
- .with_name(target.id)
400
- .with_type(
398
+ .name(target.id)
399
+ .type(
401
400
  ast.unparse(stmt.annotation)
402
401
  if stmt.annotation
403
402
  else self._infer_type(
404
403
  script, target.lineno, target.col_offset
405
404
  )
406
405
  )
407
- .with_start_line(getattr(target, "lineno", -1))
408
- .with_end_line(getattr(stmt, "end_lineno", stmt.lineno))
406
+ .start_line(getattr(target, "lineno", -1))
407
+ .end_line(getattr(stmt, "end_lineno", stmt.lineno))
409
408
  .build()
410
409
  )
411
410
  # We may also encounter `__slots__` in class definitions.
@@ -428,10 +427,10 @@ class SymbolTableBuilder:
428
427
  value = elt.s if isinstance(elt, ast.Str) else elt.value
429
428
  attributes[value] = (
430
429
  PyClassAttribute.builder()
431
- .with_name(value)
432
- .with_type("slot")
433
- .with_start_line(getattr(stmt, "lineno", -1))
434
- .with_end_line(getattr(stmt, "end_lineno", stmt.lineno))
430
+ .name(value)
431
+ .type("slot")
432
+ .start_line(getattr(stmt, "lineno", -1))
433
+ .end_line(getattr(stmt, "end_lineno", stmt.lineno))
435
434
  .build()
436
435
  )
437
436
 
@@ -472,15 +471,15 @@ class SymbolTableBuilder:
472
471
  ) -> PyCallableParameter:
473
472
  return (
474
473
  PyCallableParameter.builder()
475
- .with_name(arg_node.arg)
476
- .with_type(resolve_type(arg_node))
477
- .with_default_value(ast.unparse(default) if default else None)
478
- .with_start_line(getattr(arg_node, "lineno", -1))
479
- .with_end_line(
474
+ .name(arg_node.arg)
475
+ .type(resolve_type(arg_node))
476
+ .default_value(ast.unparse(default) if default else None)
477
+ .start_line(getattr(arg_node, "lineno", -1))
478
+ .end_line(
480
479
  getattr(arg_node, "end_lineno", getattr(arg_node, "lineno", -1))
481
480
  )
482
- .with_start_column(getattr(arg_node, "col_offset", -1))
483
- .with_end_column(getattr(arg_node, "end_col_offset", -1))
481
+ .start_column(getattr(arg_node, "col_offset", -1))
482
+ .end_column(getattr(arg_node, "end_col_offset", -1))
484
483
  .build()
485
484
  )
486
485
 
@@ -559,17 +558,17 @@ class SymbolTableBuilder:
559
558
 
560
559
  call_sites.append(
561
560
  PyCallsite.builder()
562
- .with_method_name(method_name)
563
- .with_receiver_expr(receiver_expr)
564
- .with_receiver_type(receiver_type)
565
- .with_argument_types(argument_types)
566
- .with_return_type(return_type)
567
- .with_callee_signature(callee_signature)
568
- .with_is_constructor_call(method_name == "__init__")
569
- .with_start_line(getattr(node, "lineno", -1))
570
- .with_start_column(getattr(node, "col_offset", -1))
571
- .with_end_line(getattr(node, "end_lineno", -1))
572
- .with_end_column(getattr(node, "end_col_offset", -1))
561
+ .method_name(method_name)
562
+ .receiver_expr(receiver_expr)
563
+ .receiver_type(receiver_type)
564
+ .argument_types(argument_types)
565
+ .return_type(return_type)
566
+ .callee_signature(callee_signature)
567
+ .is_constructor_call(method_name == "__init__")
568
+ .start_line(getattr(node, "lineno", -1))
569
+ .start_column(getattr(node, "col_offset", -1))
570
+ .end_line(getattr(node, "end_lineno", -1))
571
+ .end_column(getattr(node, "end_col_offset", -1))
573
572
  .build()
574
573
  )
575
574
 
@@ -611,23 +610,23 @@ class SymbolTableBuilder:
611
610
  if isinstance(target, ast.Name):
612
611
  module_vars.append(
613
612
  PyVariableDeclaration.builder()
614
- .with_name(target.id)
615
- .with_type(
613
+ .name(target.id)
614
+ .type(
616
615
  self._infer_type(
617
616
  script, target.lineno, target.col_offset
618
617
  )
619
618
  )
620
- .with_initializer(
619
+ .initializer(
621
620
  ast.unparse(node.value) if node.value else None
622
621
  )
623
- .with_value(None)
624
- .with_scope("module")
625
- .with_start_line(getattr(target, "lineno", -1))
626
- .with_end_line(
622
+ .value(None)
623
+ .scope("module")
624
+ .start_line(getattr(target, "lineno", -1))
625
+ .end_line(
627
626
  getattr(node, "end_lineno", getattr(node, "lineno", -1))
628
627
  )
629
- .with_start_column(getattr(target, "col_offset", -1))
630
- .with_end_column(getattr(target, "end_col_offset", -1))
628
+ .start_column(getattr(target, "col_offset", -1))
629
+ .end_column(getattr(target, "end_col_offset", -1))
631
630
  .build()
632
631
  )
633
632
 
@@ -638,23 +637,21 @@ class SymbolTableBuilder:
638
637
  if isinstance(target, ast.Name):
639
638
  module_vars.append(
640
639
  PyVariableDeclaration.builder()
641
- .with_name(target.id)
642
- .with_type(
640
+ .name(target.id)
641
+ .type(
643
642
  ast.unparse(node.annotation)
644
643
  if node.annotation
645
644
  else self._infer_type(script, node.lineno, node.col_offset)
646
645
  )
647
- .with_initializer(
648
- ast.unparse(node.value) if node.value else None
649
- )
650
- .with_value(None)
651
- .with_scope("module")
652
- .with_start_line(getattr(target, "lineno", -1))
653
- .with_end_line(
646
+ .initializer(ast.unparse(node.value) if node.value else None)
647
+ .value(None)
648
+ .scope("module")
649
+ .start_line(getattr(target, "lineno", -1))
650
+ .end_line(
654
651
  getattr(node, "end_lineno", getattr(node, "lineno", -1))
655
652
  )
656
- .with_start_column(getattr(target, "col_offset", -1))
657
- .with_end_column(getattr(target, "end_col_offset", -1))
653
+ .start_column(getattr(target, "col_offset", -1))
654
+ .end_column(getattr(target, "end_col_offset", -1))
658
655
  .build()
659
656
  )
660
657
 
@@ -682,23 +679,23 @@ class SymbolTableBuilder:
682
679
  if isinstance(target, ast.Name):
683
680
  local_vars.append(
684
681
  PyVariableDeclaration.builder()
685
- .with_name(target.id)
686
- .with_type(
682
+ .name(target.id)
683
+ .type(
687
684
  self._infer_type(
688
685
  script, target.lineno, target.col_offset
689
686
  )
690
687
  )
691
- .with_initializer(
688
+ .initializer(
692
689
  ast.unparse(node.value) if node.value else None
693
690
  )
694
- .with_value(None)
695
- .with_scope("function")
696
- .with_start_line(getattr(target, "lineno", -1))
697
- .with_end_line(
691
+ .value(None)
692
+ .scope("function")
693
+ .start_line(getattr(target, "lineno", -1))
694
+ .end_line(
698
695
  getattr(node, "end_lineno", getattr(node, "lineno", -1))
699
696
  )
700
- .with_start_column(getattr(target, "col_offset", -1))
701
- .with_end_column(getattr(target, "end_col_offset", -1))
697
+ .start_column(getattr(target, "col_offset", -1))
698
+ .end_column(getattr(target, "end_col_offset", -1))
702
699
  .build()
703
700
  )
704
701
  # This handles instance attribute assignments like self.attr = value
@@ -709,23 +706,23 @@ class SymbolTableBuilder:
709
706
  ):
710
707
  local_vars.append(
711
708
  PyVariableDeclaration.builder()
712
- .with_name(target.attr)
713
- .with_type(
709
+ .name(target.attr)
710
+ .type(
714
711
  self._infer_type(
715
712
  script, target.lineno, target.col_offset
716
713
  )
717
714
  )
718
- .with_initializer(
715
+ .initializer(
719
716
  ast.unparse(node.value) if node.value else None
720
717
  )
721
- .with_value(None)
722
- .with_scope("class")
723
- .with_start_line(getattr(target, "lineno", -1))
724
- .with_end_line(
718
+ .value(None)
719
+ .scope("class")
720
+ .start_line(getattr(target, "lineno", -1))
721
+ .end_line(
725
722
  getattr(node, "end_lineno", getattr(node, "lineno", -1))
726
723
  )
727
- .with_start_column(getattr(target, "col_offset", -1))
728
- .with_end_column(getattr(target, "end_col_offset", -1))
724
+ .start_column(getattr(target, "col_offset", -1))
725
+ .end_column(getattr(target, "end_col_offset", -1))
729
726
  .build()
730
727
  )
731
728
 
@@ -741,17 +738,17 @@ class SymbolTableBuilder:
741
738
  if isinstance(target, ast.Name):
742
739
  local_vars.append(
743
740
  PyVariableDeclaration.builder()
744
- .with_name(target.id)
745
- .with_type(annotation_str)
746
- .with_initializer(initializer_str)
747
- .with_value(None)
748
- .with_scope("function")
749
- .with_start_line(getattr(target, "lineno", -1))
750
- .with_end_line(
741
+ .name(target.id)
742
+ .type(annotation_str)
743
+ .initializer(initializer_str)
744
+ .value(None)
745
+ .scope("function")
746
+ .start_line(getattr(target, "lineno", -1))
747
+ .end_line(
751
748
  getattr(node, "end_lineno", getattr(node, "lineno", -1))
752
749
  )
753
- .with_start_column(getattr(target, "col_offset", -1))
754
- .with_end_column(getattr(target, "end_col_offset", -1))
750
+ .start_column(getattr(target, "col_offset", -1))
751
+ .end_column(getattr(target, "end_col_offset", -1))
755
752
  .build()
756
753
  )
757
754
  # Annotated instance attribute: self.attr: int = SOME_VALUE
@@ -762,17 +759,17 @@ class SymbolTableBuilder:
762
759
  ):
763
760
  local_vars.append(
764
761
  PyVariableDeclaration.builder()
765
- .with_name(target.attr)
766
- .with_type(annotation_str)
767
- .with_initializer(initializer_str)
768
- .with_value(None)
769
- .with_scope("class")
770
- .with_start_line(getattr(target, "lineno", -1))
771
- .with_end_line(
762
+ .name(target.attr)
763
+ .type(annotation_str)
764
+ .initializer(initializer_str)
765
+ .value(None)
766
+ .scope("class")
767
+ .start_line(getattr(target, "lineno", -1))
768
+ .end_line(
772
769
  getattr(node, "end_lineno", getattr(node, "lineno", -1))
773
770
  )
774
- .with_start_column(getattr(target, "col_offset", -1))
775
- .with_end_column(getattr(target, "end_col_offset", -1))
771
+ .start_column(getattr(target, "col_offset", -1))
772
+ .end_column(getattr(target, "end_col_offset", -1))
776
773
  .build()
777
774
  )
778
775
 
@@ -859,14 +856,14 @@ class SymbolTableBuilder:
859
856
 
860
857
  return (
861
858
  PySymbol.builder()
862
- .with_name(name)
863
- .with_scope(scope)
864
- .with_kind(kind)
865
- .with_type(inferred_type)
866
- .with_qualified_name(qname)
867
- .with_is_builtin(is_builtin)
868
- .with_lineno(lineno)
869
- .with_col_offset(col_offset)
859
+ .name(name)
860
+ .scope(scope)
861
+ .kind(kind)
862
+ .type(inferred_type)
863
+ .qualified_name(qname)
864
+ .is_builtin(is_builtin)
865
+ .lineno(lineno)
866
+ .col_offset(col_offset)
870
867
  .build()
871
868
  )
872
869
 
@@ -1,5 +1,4 @@
1
- from .logging import logger
2
- from .logging import _set_log_level
1
+ from .logging import _set_log_level, logger
3
2
  from .progress_bar import ProgressBar
4
3
 
5
4
  __all__ = ["logger", "_set_log_level", "ProgressBar"]
@@ -1,6 +1,7 @@
1
+ import logging
2
+
1
3
  from rich.console import Console
2
4
  from rich.logging import RichHandler
3
- import logging
4
5
 
5
6
  # Set up base logger with RichHandler
6
7
  console = Console()
@@ -1,15 +1,16 @@
1
+ import logging
2
+ from typing import Optional
3
+
1
4
  from rich.console import Console
2
5
  from rich.progress import (
6
+ BarColumn,
3
7
  Progress,
4
8
  SpinnerColumn,
9
+ TaskID,
5
10
  TextColumn,
6
- BarColumn,
7
11
  TimeElapsedColumn,
8
12
  TimeRemainingColumn,
9
- TaskID,
10
13
  )
11
- from typing import Optional
12
- import logging
13
14
 
14
15
 
15
16
  class ProgressBar: