-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathanalyzer.py
More file actions
132 lines (117 loc) · 6.17 KB
/
analyzer.py
File metadata and controls
132 lines (117 loc) · 6.17 KB
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
import os
import subprocess
from multilspy import SyncLanguageServer
from pathlib import Path
import tomllib
from ...entities.entity import Entity
from ...entities.file import File
from typing import Optional
from ..analyzer import AbstractAnalyzer, ResolvedEntityRef
from ...graph import Graph
import tree_sitter_python as tspython
from tree_sitter import Language, Node
import logging
logger = logging.getLogger('code_graph')
class PythonAnalyzer(AbstractAnalyzer):
def __init__(self) -> None:
super().__init__(Language(tspython.language()))
def add_dependencies(self, path: Path, files: list[Path]):
if Path(f"{path}/venv").is_dir():
return
subprocess.run(["python3", "-m", "venv", "venv"], cwd=str(path))
if Path(f"{path}/pyproject.toml").is_file():
subprocess.run(["pip", "install", "poetry"], cwd=str(path), env={"VIRTUAL_ENV": f"{path}/venv", "PATH": f"{path}/venv/bin:{os.environ['PATH']}"})
subprocess.run(["poetry", "install"], cwd=str(path), env={"VIRTUAL_ENV": f"{path}/venv", "PATH": f"{path}/venv/bin:{os.environ['PATH']}"})
try:
with open(f"{path}/pyproject.toml", 'rb') as file:
pyproject_data = tomllib.load(file)
dependencies = (pyproject_data.get("tool") or {}).get("poetry", {}).get("dependencies", {})
for requirement in dependencies:
files.extend(Path(f"{path}/venv/lib").rglob(f"**/site-packages/{requirement}/*.py"))
except Exception as e:
logger.warning("Failed to parse %s/pyproject.toml: %s", path, e)
elif Path(f"{path}/requirements.txt").is_file():
subprocess.run(["pip", "install", "-r", "requirements.txt"], cwd=str(path), env={"VIRTUAL_ENV": f"{path}/venv", "PATH": f"{path}/venv/bin:{os.environ['PATH']}"})
with open(f"{path}/requirements.txt", 'r') as file:
requirements = [line.strip().split("==") for line in file if line.strip()]
for requirement in requirements:
files.extend(Path(f"{path}/venv/lib/").rglob(f"**/site-packages/{requirement}/*.py"))
def get_entity_label(self, node: Node) -> str:
if node.type == 'class_definition':
return "Class"
elif node.type == 'function_definition':
return "Function"
raise ValueError(f"Unknown entity type: {node.type}")
def get_entity_name(self, node: Node) -> str:
if node.type in ['class_definition', 'function_definition']:
return node.child_by_field_name('name').text.decode('utf-8')
raise ValueError(f"Unknown entity type: {node.type}")
def get_entity_docstring(self, node: Node) -> Optional[str]:
if node.type in ['class_definition', 'function_definition']:
body = node.child_by_field_name('body')
if body.child_count > 0 and body.children[0].type == 'expression_statement':
docstring_node = body.children[0].child(0)
return docstring_node.text.decode('utf-8')
return None
raise ValueError(f"Unknown entity type: {node.type}")
def get_entity_types(self) -> list[str]:
return ['class_definition', 'function_definition']
def add_symbols(self, entity: Entity) -> None:
if entity.node.type == 'class_definition':
superclasses = entity.node.child_by_field_name("superclasses")
if superclasses:
base_classes_captures = self._captures("(argument_list (_) @base_class)", superclasses)
if 'base_class' in base_classes_captures:
for base_class in base_classes_captures['base_class']:
entity.add_symbol("base_class", base_class)
elif entity.node.type == 'function_definition':
captures = self._captures("(call) @reference.call", entity.node)
if 'reference.call' in captures:
for caller in captures['reference.call']:
entity.add_symbol("call", caller)
captures = self._captures("(typed_parameter type: (_) @parameter)", entity.node)
if 'parameter' in captures:
for parameter in captures['parameter']:
entity.add_symbol("parameters", parameter)
return_type = entity.node.child_by_field_name('return_type')
if return_type:
entity.add_symbol("return_type", return_type)
def is_dependency(self, file_path: str) -> bool:
return "venv" in file_path
def resolve_path(self, file_path: str, path: Path) -> str:
return file_path
def resolve_type(self, files: dict[Path, File], lsp: SyncLanguageServer, file_path: Path, path: Path, graph: Graph, node: Node) -> list[Entity | ResolvedEntityRef]:
if node.type == 'attribute':
node = node.child_by_field_name('attribute')
return self.resolve_entities(
files,
lsp,
file_path,
path,
node,
graph,
['class_definition'],
['Class'],
)
def resolve_method(self, files: dict[Path, File], lsp: SyncLanguageServer, file_path: Path, path: Path, graph: Graph, node: Node) -> list[Entity | ResolvedEntityRef]:
if node.type == 'call':
node = node.child_by_field_name('function')
if node.type == 'attribute':
node = node.child_by_field_name('attribute')
return self.resolve_entities(
files,
lsp,
file_path,
path,
node,
graph,
['function_definition', 'class_definition'],
['Function', 'Class'],
)
def resolve_symbol(self, files: dict[Path, File], lsp: SyncLanguageServer, file_path: Path, path: Path, graph: Graph, key: str, symbol: Node) -> list[Entity | ResolvedEntityRef]:
if key in ["base_class", "parameters", "return_type"]:
return self.resolve_type(files, lsp, file_path, path, graph, symbol)
elif key in ["call"]:
return self.resolve_method(files, lsp, file_path, path, graph, symbol)
else:
raise ValueError(f"Unknown key {key}")