-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_extractor.py
461 lines (324 loc) · 13 KB
/
python_extractor.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import ast
import hashlib
import os
from copy import deepcopy
from enum import Enum
from typing import Union, Dict, Optional
import black
from langchain_community.llms.ollama import Ollama
from langchain_core.prompts import ChatPromptTemplate
class Type(Enum):
FUNCTIONS = 0
CLASSES = 1
class ParentClassFinder(ast.NodeVisitor):
def __init__(self):
self.parent_classes = {}
self.current_class = None
def visit_ClassDef(self, node):
self.current_class = node
self.parent_classes[node] = None
self.generic_visit(node)
self.current_class = None
def visit_FunctionDef(self, node):
if self.current_class:
self.parent_classes[node] = self.current_class
else:
self.parent_classes[node] = None
self.generic_visit(node)
class PythonExtractor:
def __init__(self, root_directory: str | os.PathLike):
self.root_directory = root_directory
self.template_message = 'This is an autogenerated docstring'
self.ai_documenter = AIDocumenter()
def extract_classes_and_functions(self, file_content):
"""
Extract classes and functions from the provided Python file content.
Args:
file_content (str): The content of the Python file.
Returns:
dict: A dictionary with class and function names as keys and their definitions as values.
"""
tree = ast.parse(file_content)
finder = ParentClassFinder()
finder.visit(tree)
nodes = {}
for node in ast.walk(tree):
if isinstance(node, (ast.ClassDef, ast.FunctionDef)):
data = {}
data['source'] = ast.get_source_segment(file_content, node)
data['doc'] = ast.get_docstring(node)
data['node'] = node
type = Type.CLASSES if isinstance(node, ast.ClassDef) else Type.FUNCTIONS
data['type'] = type
data['parent'] = finder.parent_classes[node]
nodes[node] = data
for n in nodes:
parent_node = nodes[n]['parent']
if parent_node:
nodes[n]['parent_source'] = nodes[parent_node]['source']
else:
nodes[n]['parent_source'] = None
return tree, nodes
def read_file(self, file_path):
"""
Read the content of a file.
Args:
file_path (str): The path to the file.
Returns:
str: The content of the file.
"""
with open(file_path, "r") as file:
return file.read()
def find_python_files(self, folder_path):
"""
Find all Python files in the specified folder and its subfolders.
Args:
folder_path (str): The path to the folder.
Returns:
list: A list of file paths to Python files.
"""
python_files = []
for root, _, files in os.walk(folder_path):
for file in files:
if file.endswith(".py"):
python_files.append(os.path.join(root, file))
return python_files
def get_docstring(self, node: Union[ast.ClassDef, ast.FunctionDef]):
"""
Get the docstring of a given AST node.
Args:
node (ast.AST): The AST node to extract the docstring from.
Returns:
str: The extracted docstring or an empty string if not present.
"""
return ast.get_docstring(node)
def set_docstring(self, node: Union[ast.ClassDef, ast.FunctionDef], docstring):
"""
Set the docstring for a given AST node.
Args:
node (ast.AST): The AST node to set the docstring for.
docstring (str): The docstring to set.
"""
docstring_node = ast.Expr(value=ast.Constant(value=docstring))
node.body.insert(0, docstring_node)
def update_docstring(self, node: Union[ast.ClassDef, ast.FunctionDef], docstring):
"""
Update the docstring for a given AST node.
Args:
node (ast.AST): The AST node to set the docstring for.
docstring (str): The docstring to set.
"""
docstring_node = ast.Expr(value=ast.Constant(value=docstring))
node.body[0] = docstring_node
def extract(self):
"""
Main function to find Python files in the folder and extract classes and functions.
Args:
folder_path (str): The path to the folder.
"""
python_files = self.find_python_files(self.root_directory)
for file_path in python_files:
print(f"\nProcessing file: {file_path}")
file_content = self.read_file(file_path)
tree, extracted_content = self.extract_classes_and_functions(file_content)
modified = False
for func_node in extracted_content.values():
modified = self.generate_doc(func_node) or modified
if modified:
new_content = ast.unparse(tree)
formatted_new_content = black.format_str(new_content, mode=black.Mode())
self.write_file(file_path, formatted_new_content)
def write_file(self, file_path, content):
"""
Write content to a file.
Args:
file_path (str): The path to the file.
content (str): The content to write.
"""
with open(file_path, "w") as file:
file.write(content)
def generate_func_hash(self, node: ast.AST, has_documentation=False):
if has_documentation:
node: ast.AST = deepcopy(node)
# Remove the documentation when generating the hash, you only care about the contents of the function
node: Union[ast.ClassDef, ast.FunctionDef]
del node.body[0]
# Convert the AST node to a string representation
node_str = ast.dump(node)
# Create a SHA-256 hash object
hash_object = hashlib.sha256()
# Update the hash object with the bytes of the node string
hash_object.update(node_str.encode('utf-8'))
# Return the hexadecimal digest of the hash
return hash_object.hexdigest()
def generate_doc(self, node: Dict):
class_node: Union[ast.ClassDef, ast.FunctionDef] = node['node']
node_type: str = node['type']
print(f"Working on {node_type}: {class_node.name}", f"from {node['parent'].name}" if node['parent'] else "")
documentation = self.get_docstring(class_node)
hash = self.generate_func_hash(class_node, has_documentation=not (documentation is None))
modified = False
if documentation and self.template_message in documentation:
if node_type == Type.FUNCTIONS:
previous_hash = documentation.split(' ')[-1].strip()
if not (previous_hash == hash):
print(
"There is a hash mismatch between the function and the last time the documentation for it was generated.")
text = self.api_find_docstring(node)
text = self.parse_doc(text, hash)
self.update_docstring(class_node, text)
modified = True
if not documentation:
text = self.api_find_docstring(node)
text = self.parse_doc(text, hash)
self.set_docstring(class_node, text)
modified = True
return modified
def parse_doc(self, text: str, hash: str):
return os.linesep.join([
"",
text,
"",
self.template_message,
f'hash {hash}',
""
""
])
def api_find_docstring(self, node):
docstring = self.ai_documenter(node['source'], node['type'], node['parent_source'])
print(docstring)
return docstring
class AIDocumenter:
def __init__(self):
# model = Ollama(model="dolphin-mistral")
self.model = Ollama(model="codestral")
self.model.num_ctx = 32768
function_documentation_template = """
Class Context:
{context}
Write comprehensive documentation for the following Python code using reStructuredText (reST) format. The documentation should include a description, parameter explanations, return type, and examples. Ensure the documentation is clear, concise, and follows standard conventions. If the function is from a class use that context to improve the description.
Example output:
Function <Function Name>
========================
Description
-----------
<Brief description of the function's purpose.>
Parameters
----------
- a (int): <Explanation of the first parameter.>
- b (int): <Explanation of the second parameter.>
Returns
-------
- int: <Explanation of the return type.>
Examples
--------
.. code-block:: python
<Example 1>
<Example 2>
Notes
-----
<Any additional notes or caveats.>
Function:
{function}
Documentation:
"""
class_documentation_template = """
Write comprehensive header documentation for the following Python class using reStructuredText (reST) format. The documentation should include a description, attribute explanations, method summaries, and examples. Ensure the documentation is clear, concise, and follows standard conventions.
<Class Name>
============
Description
-----------
<Brief description of the class's purpose.>
Attributes
----------
- attribute_name (type): <Explanation of the attribute.>
Methods
-------
- method_name(parameters): <Brief description of the method.>
Examples
--------
.. code-block:: python
<Example 1>
<Example 2>
Notes
-----
<Any additional notes or caveats.>
Class:
{function}
Documentation:
"""
func_prompt = ChatPromptTemplate.from_messages(
[
(
"system",
function_documentation_template,
)
]
)
class_prompt = ChatPromptTemplate.from_messages(
[
(
"system",
class_documentation_template,
)
]
)
self.function_chain = func_prompt | self.model
self.class_chain = class_prompt | self.model
self.chain = {Type.FUNCTIONS: self.function_chain, Type.CLASSES: self.class_chain}
def normalize_left_strip(self, txt: str, type: str):
lines = txt.split(os.linesep)
line_strip = None
for i, line in enumerate(lines):
lines_stripped = line.lstrip()
for mixes in ['====', '----']:
if lines_stripped.startswith(mixes):
index = line.find(mixes)
if line_strip:
line_strip = min(index, line_strip)
else:
line_strip = index
if i > 0:
prev_line = lines[i - 1].strip()
if Type.FUNCTIONS == type and '====' in line:
if not (' ' in prev_line):
first_char_index = prev_line.index(prev_line[0])
lines[i - 1] = lines[i - 1][:first_char_index] + "Function " + lines[i - 1][
first_char_index:]
prev_line = lines[i - 1].strip()
num_characters = len(prev_line)
current_line = line.strip()
if len(prev_line) > len(current_line):
characters = current_line[0] * num_characters
lines[i] = lines[i][:index] + characters
if not line_strip:
return txt
for i in range(len(lines)):
first_char = lines[i].lstrip()
if len(first_char) == 0:
continue
text_index = lines[i].find(first_char[0])
max_stripping = min(line_strip, text_index)
lines[i] = lines[i][max_stripping:]
return os.linesep.join(lines)
def __call__(self, function_source: str, type: str, parent_source: Optional[str]):
template_input = {"function": function_source}
chain = self.chain[type]
if type == Type.FUNCTIONS:
# print(self.model.get_num_tokens(function_source), self.model.get_num_tokens(parent_source))
if parent_source:
# python_splitter = RecursiveCharacterTextSplitter.from_language(
# language=Language.PYTHON, chunk_size=50, chunk_overlap=0
# )
# python_docs = python_splitter.create_documents([PYTHON_CODE])
template_input['context'] = parent_source
else:
template_input['context'] = "This function is not part of a class."
response = chain.invoke(
template_input
).strip()
response = self.normalize_left_strip(response, type)
return response
if __name__ == '__main__':
data = PythonExtractor('codebases/')
data.extract()