-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpython_analysis.py
164 lines (139 loc) · 5.89 KB
/
python_analysis.py
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
################################################################################
# Copyright IBM Corporation 2024
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
################################################################################
"""
Python module
"""
from pathlib import Path
from typing import List
from cldk.analysis import SymbolTable
from cldk.analysis.python.treesitter import PythonSitter
from cldk.models.python.models import PyMethod, PyImport, PyModule, PyClass
class PythonAnalysis(SymbolTable):
"""Python Analysis Class"""
def __init__(
self,
analysis_backend: str,
eager_analysis: bool,
project_dir: str | Path | None,
source_code: str | None,
analysis_backend_path: str | None,
analysis_json_path: str | Path | None,
use_graalvm_binary: bool = None,
) -> None:
self.project_dir = project_dir
self.source_code = source_code
self.analysis_json_path = analysis_json_path
self.analysis_backend_path = analysis_backend_path
self.eager_analysis = eager_analysis
self.use_graalvm_binary = use_graalvm_binary
# Initialize the analysis analysis_backend
if analysis_backend.lower() == "codeql":
raise NotImplementedError("Support for {analysis_backend} has not been implemented yet.")
elif analysis_backend.lower() == "codeanalyzer":
raise NotImplementedError("Support for {analysis_backend} has not been implemented yet.")
elif analysis_backend.lower() == "treesitter":
self.analysis_backend: PythonSitter = PythonSitter()
else:
raise NotImplementedError("Support for {analysis_backend} has not been implemented yet.")
def get_methods(self) -> List[PyMethod]:
"""
Given an application or a source code, get all the methods
"""
return self.analysis_backend.get_all_methods(self.source_code)
def get_functions(self) -> List[PyMethod]:
"""
Given an application or a source code, get all the methods
"""
return self.analysis_backend.get_all_functions(self.source_code)
def get_modules(self) -> List[PyModule]:
"""
Given the project directory, get all the modules
"""
return self.analysis_backend.get_all_modules(self.project_dir)
def get_method_details(self, method_signature: str) -> PyMethod:
"""
Given the code body and the method signature, returns the method details related to that method
Parameters
----------
method_signature: method signature
Returns
-------
PyMethod: Returns the method details related to that method
"""
return self.analysis_backend.get_method_details(self.source_code, method_signature)
def is_parsable(self, source_code: str) -> bool:
"""
Check if the code is parsable
Args:
source_code: source code
Returns:
True if the code is parsable, False otherwise
"""
return PythonSitter().is_parsable(source_code)
def get_raw_ast(self, source_code: str) -> str:
"""
Get the raw AST
Args:
code: source code
Returns:
Tree: the raw AST
"""
return PythonSitter().get_raw_ast(source_code)
def get_imports(self) -> List[PyImport]:
"""
Given an application or a source code, get all the imports
"""
return self.analysis_backend.get_all_imports_details(self.source_code)
def get_variables(self, **kwargs):
"""
Given an application or a source code, get all the variables
"""
raise NotImplementedError("Support for this functionality has not been implemented yet.")
def get_classes(self) -> List[PyClass]:
"""
Given an application or a source code, get all the classes
"""
return self.analysis_backend.get_all_classes(self.source_code)
def get_classes_by_criteria(self, **kwargs):
"""
Given an application or a source code, get all the classes given the inclusion and exclution criteria
"""
raise NotImplementedError("Support for this functionality has not been implemented yet.")
def get_sub_classes(self, **kwargs):
"""
Given an application or a source code, get all the sub-classes
"""
raise NotImplementedError("Support for this functionality has not been implemented yet.")
def get_nested_classes(self, **kwargs):
"""
Given an application or a source code, get all the nested classes
"""
raise NotImplementedError("Support for this functionality has not been implemented yet.")
def get_constructors(self, **kwargs):
"""
Given an application or a source code, get all the constructors
"""
raise NotImplementedError("Support for this functionality has not been implemented yet.")
def get_methods_in_class(self, **kwargs):
"""
Given an application or a source code, get all the methods within the given class
"""
raise NotImplementedError("Support for this functionality has not been implemented yet.")
def get_fields(self, **kwargs):
"""
Given an application or a source code, get all the fields
"""
raise NotImplementedError("Support for this functionality has not been implemented yet.")