-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.py
211 lines (182 loc) · 10 KB
/
extension.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
import socketio
import eventlet
sio = socketio.Server()
app = socketio.WSGIApp(sio)
import re
import inspect
import requests
import random
import string
import ast
api_key = "l7s5ukux5iy7i5qprh4b348xcr8tymlbvy2f7ysepcno637il"
url = f"https://api.wordnik.com/v4/words.json/randomWord?api_key={api_key}"
@sio.on('send_variable')
def receive_variable(sid, contents):
globFilePath = contents["filePath"]
# Do something with the variable
if(contents["filePath"].endswith(".c") or contents["filePath"].endswith('.cpp')):
contents = contents['fileContent']
function_pattern = re.compile(r"void\s+(\w+)\s*\(")
macro_pattern = re.compile(r"#define\s+(\w+)")
variable_pattern = re.compile(r"\b(\w+)\b\s*(?:=.*|;)")
# Find all matches of each pattern in the file
functions = function_pattern.findall(contents)
macros = macro_pattern.findall(contents)
variables = variable_pattern.findall(contents)
# Create a dictionary to store the variables and their corresponding functions/classes
var_functions = {}
varr = []
# Add code to iterate through variables inside functions and classes
module = inspect.getmodule(inspect.currentframe())
for name, obj in inspect.getmembers(module):
if inspect.isfunction(obj) or inspect.isclass(obj):
code = obj.__code__
function_variables = set(code.co_names)
for var in code.co_consts:
if inspect.iscode(var):
function_variables.update(var.co_names)
for var_name in var.co_names:
var_functions[var_name] = name
for var_name in function_variables:
var_functions[var_name] = name
# Print the C file outline
print("C File Outline:")
if functions:
print("Functions:")
for func in functions:
if len(func) > 3 and func != "return" and func != "main" and func != "break" :
res = ''.join(random.choices(string.ascii_letters, k=7))
contents = contents.replace(func, res)
if macros:
print("Macros:")
for macro in macros:
if len(macro) > 3 :
response = requests.get(url)
if response.status_code == 200:
data = response.json()
random_word = data["word"].replace("-", "_")
random_word = random_word.replace("'", "")
contents = contents.replace(macro, random_word)
else:
print("Failed to retrieve a random word")
res = ''.join(random.choices(string.ascii_letters, k=7))
contents = contents.replace(macro, res)
if variables:
print("Variables:")
for variable in variables:
function_name = var_functions.get(variable, None)
if len(variable) > 3 and variable != "break" and variable != "return" and variable != "main":
res = ''.join(random.choices(string.ascii_letters, k=7))
contents = contents.replace(variable, res)
if function_name:
print("- " + variable + " (from function/class " + function_name + ")")
varr.append(variable)
else:
print("- " + variable)
varr.append(variable)
else :
class OutlineVisitor(ast.NodeVisitor):
def __init__(self):
self.functions = []
self.classes = []
self.variables = {}
self.class_variables = {}
def visit_FunctionDef(self, node):
self.functions.append(node.name)
self.variables[node.name] = []
# Visit the function body to find variables defined inside
for child_node in ast.walk(node):
if isinstance(child_node, ast.Assign):
for target in child_node.targets:
if isinstance(target, ast.Name):
self.variables[node.name].append(target.id)
def visit_ClassDef(self, node):
self.classes.append(node.name)
self.class_variables[node.name] = []
# Visit the class body to find variables and functions defined inside
for child_node in node.body:
if isinstance(child_node, ast.FunctionDef):
self.class_variables[node.name].append(child_node.name)
self.variables[child_node.name] = []
for sub_child_node in ast.walk(child_node):
if isinstance(sub_child_node, ast.Assign):
for target in sub_child_node.targets:
if isinstance(target, ast.Name):
self.variables[child_node.name].append(target.id)
elif isinstance(child_node, ast.Assign):
for target in child_node.targets:
if isinstance(target, ast.Name):
self.class_variables[node.name].append(target.id)
def visit_Name(self, node):
if isinstance(node.ctx, ast.Store):
# If the name is being assigned to, add it to the list of variables
if node.id not in self.variables:
self.variables[node.id] = []
for func in reversed(self.functions):
if node.id in self.variables[func]:
return
for cls in reversed(self.classes):
if node.id in self.class_variables[cls]:
return
self.variables[node.id].append(None)
contents = contents['fileContent']
tree = ast.parse(contents)
# Traverse the AST using the OutlineVisitor
visitor = OutlineVisitor()
visitor.visit(tree)
# Print the Python file outline
print("Python File Outline:")
if visitor.functions:
print("Functions:")
for func in visitor.functions:
if len(func) > 3 and func != "return" and func != "main" and func != "break" and func != "__init__":
res = ''.join(random.choices(string.ascii_letters, k=7))
contents = contents.replace(func, res)
print("- " + func)
if visitor.variables.get(func):
print(" Variables:")
for var in visitor.variables[func]:
if len(var) > 3 and var != "break" and var != "return" and var != "main" and var != "__init__":
res = ''.join(random.choices(string.ascii_letters, k=7))
contents = contents.replace(var, res)
print(" - " + var)
if visitor.classes:
print("Classes:")
for cls in visitor.classes:
if len(cls) > 3 and cls != "break" and cls != "return" and cls != "main" and cls != "__init__":
res = ''.join(random.choices(string.ascii_letters, k=7))
contents = contents.replace(cls, res)
print("- " + cls)
if visitor.class_variables.get(cls):
print(" Variables:")
for var in visitor.class_variables[cls]:
if len(var) > 3 and var != "break" and var != "return" and var != "main" and var != "__init__":
res = ''.join(random.choices(string.ascii_letters, k=7))
contents = contents.replace(var, res)
print(" - " + var)
if visitor.functions:
print(" Functions:")
for func in visitor.functions:
if func in visitor.class_variables.get(cls, []):
if len(func) > 3 and func != "break" and func != "return" and func != "main" and func != "__init__":
res = ''.join(random.choices(string.ascii_letters, k=7))
contents = contents.replace(func, res)
print(" - " + func)
if visitor.variables.get(func):
print(" Variables:")
for var in visitor.variables[func]:
if len(var) > 3 and var != "break" and var != "return" and var != "main" and var != "__init__":
res = ''.join(random.choices(string.ascii_letters, k=7))
contents = contents.replace(var, res)
print(" - " + var)
if visitor.variables:
print("Variables:")
for var in visitor.variables:
if var not in visitor.functions and var not in visitor.classes:
if len(var) > 3 and var != "break" and var != "return" and var != "main" and var != "__init__":
res = ''.join(random.choices(string.ascii_letters, k=7))
contents = contents.replace(var, res)
print("- " + var)
sio.emit('result', {"globFilePath": globFilePath, "contents": contents}, room=sid)
if __name__ == '__main__':
eventlet.wsgi.server(eventlet.listen(('localhost', 5000)), app)