-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_analyzer.py
More file actions
147 lines (128 loc) · 5.25 KB
/
function_analyzer.py
File metadata and controls
147 lines (128 loc) · 5.25 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import ast
import re
class FunctionAnalyzer():
def __init__(self):
pass
def get_function_name(self,code):
try:
tree = ast.parse(code)
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
return node.name
except:
return ""
def get_function_def_line(self,code):
try:
tree = ast.parse(code)
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
return node.lineno - 1
return -1
except:
return -1
def get_docstring(self,code):
try:
tree = ast.parse(code)
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
docstring = ast.get_docstring(node)
if docstring:
return docstring
return None
except SyntaxError:
return None
def remove_docstring_from_function(self,func_str):
# Parse the function string into an AST node
try:
tree = ast.parse(func_str)
# Find the function definition in the AST
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
if isinstance(node.body[0], ast.Expr) and isinstance(node.body[0].value, ast.Str):
# Remove the first node in the body if it's a docstring
node.body.pop(0)
break
# Convert the modified AST back to source code
return ast.unparse(tree)
except SyntaxError:
return func_str
def remove_leading_whitespace_for_def(self,code):
lines = code.split('\n')
updated_lines = []
for line in lines:
stripped_line = line.lstrip()
if stripped_line.startswith('def '):
updated_lines.append(stripped_line)
else:
updated_lines.append(line)
return '\n'.join(updated_lines)
def get_function_blocks(self,code):
try:
tree = ast.parse(self.remove_leading_whitespace_for_def(code))
function_blocks = []
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
start_line = node.lineno - 1
end_line = max([n.lineno for n in ast.walk(node) if hasattr(n, 'lineno')]) - 1
function_block = '\n'.join(code.splitlines()[start_line:end_line + 1])
function_blocks.append(function_block)
return function_blocks
except:
return []
def extract_python_code(self,text):
# Regular expression to match Python code blocks (assuming they are in triple backticks)
code_pattern = re.compile(r'```python(.*?)```', re.DOTALL)
# Find all matches
code_blocks = code_pattern.findall(text)
if code_blocks:
temp = code_blocks[0].replace('\\n', '\n').replace('\\t', '\t')
return temp
else:
return ""
def get_code_blocks(self,code):
try:
tree = ast.parse(code)
code_blocks = []
block_info = []
# Traverse the AST nodes
for node in ast.walk(tree):
if isinstance(node, (ast.ClassDef, ast.If, ast.For, ast.While, ast.With, ast.Try)):
start_line = node.lineno
end_line = max([n.lineno for n in ast.walk(node) if hasattr(n, 'lineno')])
# Extract the code block from the original code
block = '\n'.join(code.splitlines()[start_line - 1:end_line])
# block = {'block_content':block,'start_line':start_line - 1,'end_line':end_line}
code_blocks.append(block)
# if isinstance(node, (ast.If, ast.For, ast.While, ast.With,ast.Try)):
line = code.splitlines()[start_line]
indent_level = len(line) - len(line.lstrip())
block_info.append((start_line - 1,end_line,indent_level,type(node)))
elif isinstance(node, (ast.FunctionDef)):
start_line = node.lineno
end_line = max([n.lineno for n in ast.walk(node) if hasattr(n, 'lineno')])
# Extract the code block from the original code
block = '\n'.join(code.splitlines()[start_line:end_line])
# block = {'block_content':block,'start_line':start_line - 1,'end_line':end_line}
code_blocks.append(block)
# if isinstance(node, (ast.If, ast.For, ast.While, ast.With,ast.Try)):
line = code.splitlines()[start_line]
indent_level = len(line) - len(line.lstrip())
block_info.append((start_line,end_line,indent_level,type(node)))
return code_blocks, block_info
except:
return [],[]
def extract_relations(self,blocks):
siblings = {}
parents = {}
for i in range(len(blocks)):
for j in range(i,-1,-1):
if blocks[i][2] > blocks[j][2]:
if blocks[i][0]>= blocks[j][0] and blocks[i][1]<= blocks[j][1]:
parents[str(i)] = j
break
for i in range(len(blocks)):
if (i+1<len(blocks)):
if blocks[i][2] == blocks[i+1][2]:
if parents.get(str(i)) == parents.get(str(i+1)):
siblings[str(i)] = i+1
return siblings,parents