Add prepare file script
Browse files- prepare_file.py +79 -0
prepare_file.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Will open up a pytho file and split out all functions into chunks
|
3 |
+
with relevent data for embedding
|
4 |
+
|
5 |
+
To use: Open up a file and store its contents as `code_string`:
|
6 |
+
with open("RAG-accelerate/src/accelerator.py", "r") as w:
|
7 |
+
code_string = w.read()
|
8 |
+
"""
|
9 |
+
import re
|
10 |
+
func_list=[]
|
11 |
+
func = ''
|
12 |
+
tab = ''
|
13 |
+
brackets = {'(':0, '[':0, '{':0}
|
14 |
+
close = {')':'(', ']':'[', '}':'{'}
|
15 |
+
string=''
|
16 |
+
tab_f=''
|
17 |
+
c1=''
|
18 |
+
multiline=False
|
19 |
+
check=False
|
20 |
+
in_class = False
|
21 |
+
decorator = None
|
22 |
+
class_name = ""
|
23 |
+
for line in code_string.split('\n'):
|
24 |
+
tab = re.findall(r'^\s*',line)[0]
|
25 |
+
if re.findall(r'^\s*@', line):
|
26 |
+
decorator = line
|
27 |
+
if (re.findall(r'^\s*def', line) or re.findall(r'^\s*class', line)) and not string and not multiline:
|
28 |
+
if re.findall(r'^\s*class', line):
|
29 |
+
in_class = True
|
30 |
+
class_name = re.findall(r'class\s+(\w+)\s*[\(:]', line)[0]
|
31 |
+
elif len(line.lstrip(" ")) == 0:
|
32 |
+
in_class = False
|
33 |
+
func += line + '\n'
|
34 |
+
tab_f = tab
|
35 |
+
check=True
|
36 |
+
if func:
|
37 |
+
if not check:
|
38 |
+
if sum(brackets.values()) == 0 and not string and not multiline:
|
39 |
+
if len(tab) <= len(tab_f):
|
40 |
+
# Append the metadata
|
41 |
+
metadata = f'File: accelerator.py\nClass: {class_name}'
|
42 |
+
if decorator is not None:
|
43 |
+
func = f'{decorator}\n{func}'
|
44 |
+
decorator = None
|
45 |
+
func = f'{metadata}\nSource:\n{func}'
|
46 |
+
func_list.append(func)
|
47 |
+
func=''
|
48 |
+
c1=''
|
49 |
+
c2=''
|
50 |
+
continue
|
51 |
+
func += line + '\n'
|
52 |
+
check = False
|
53 |
+
for c0 in line:
|
54 |
+
if c0 == '#' and not string and not multiline:
|
55 |
+
break
|
56 |
+
if c1 != '\\':
|
57 |
+
if c0 in ['"', "'"]:
|
58 |
+
if c2 == c1 == c0 == '"' and string != "'":
|
59 |
+
multiline = not multiline
|
60 |
+
string = ''
|
61 |
+
continue
|
62 |
+
if not multiline:
|
63 |
+
if c0 in string:
|
64 |
+
string = ''
|
65 |
+
else:
|
66 |
+
if not string:
|
67 |
+
string = c0
|
68 |
+
if not string and not multiline:
|
69 |
+
if c0 in brackets:
|
70 |
+
brackets[c0] += 1
|
71 |
+
if c0 in close:
|
72 |
+
b = close[c0]
|
73 |
+
brackets[b] -= 1
|
74 |
+
c2=c1
|
75 |
+
c1=c0
|
76 |
+
|
77 |
+
for f in func_list:
|
78 |
+
print('-'*40)
|
79 |
+
print(f)
|