forked from jonfriskics/PythonFlask-JobBoard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
309 lines (233 loc) · 8.57 KB
/
utils.py
File metadata and controls
309 lines (233 loc) · 8.57 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
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
import ast
import inspect
import json
import os
import collections
from bs4 import BeautifulSoup
from jinja2 import Environment, PackageLoader, exceptions, meta, nodes
env = Environment(loader=PackageLoader("jobs", "templates"))
def flatten(d, parent_key="", sep="_"):
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, collections.MutableMapping):
items.extend(flatten(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items)
def get_decorators(source):
decorators = {}
def visit_FunctionDef(node):
decorators[node.name] = []
for n in node.decorator_list:
name = ""
if isinstance(n, ast.Call):
name = n.func.attr if isinstance(n.func, ast.Attribute) else n.func.id
else:
name = n.attr if isinstance(n, ast.Attribute) else n.id
args = [a.s for a in n.args] if hasattr(n, "args") else []
decorators[node.name].append((name, args))
node_iter = ast.NodeVisitor()
node_iter.visit_FunctionDef = visit_FunctionDef
node_iter.visit(ast.parse(inspect.getsource(source)))
return decorators
def get_functions(source):
functions = []
def visit_Call(node):
path = node.func.attr if isinstance(node.func, ast.Attribute) else node.func.id
if len(node.args) != 0:
args = []
for arg in node.args:
arg_dict = build_dict(arg)
arg_dict.pop("value", None)
for val in arg_dict.values():
args.append(str(val))
path += ":" + ":".join(args)
if len(node.keywords) != 0:
path += ":" + ":".join(
[
str(val)
for keyword in node.keywords
for val in build_dict(keyword).values()
]
)
functions.append(path)
node_iter = ast.NodeVisitor()
node_iter.visit_Call = visit_Call
node_iter.visit(ast.parse(inspect.getsource(source)))
return functions
def get_functions_returns(source):
returns = []
def visit_Return(node):
return_dict = build_dict(node)
return_dict.pop("value/args/value", None)
return_dict.pop("value/args/args/value", None)
returns.append(return_dict)
node_iter = ast.NodeVisitor()
node_iter.visit_Return = visit_Return
node_iter.visit(ast.parse(inspect.getsource(source)))
return returns
def get_statements(source):
statements = []
def visit_If(node):
statements.append(build_dict(node))
node_iter = ast.NodeVisitor()
node_iter.visit_If = visit_If
node_iter.visit(ast.parse(inspect.getsource(source)))
return statements
def build_dict(node):
result = {}
if node.__class__.__name__ == "Is" or node.__class__.__name__ == "Eq":
result["node_type"] = node.__class__.__name__
for attr in dir(node):
if (
not attr.startswith("_")
and attr != "ctx"
and attr != "lineno"
and attr != "end_lineno"
and attr != "col_offset"
and attr != "end_col_offset"
and attr != "kind"
and attr != "n"
):
value = getattr(node, attr)
if isinstance(value, ast.AST):
value = build_dict(value)
elif isinstance(value, list):
final = [build_dict(n) for n in value]
value = final[0] if len(final) == 1 else final
if value != []:
result[attr] = value
return flatten(result, sep="/")
def list_routes(app):
rules = []
for rule in app.url_map.iter_rules():
methods = ",".join(sorted(rule.methods))
if rule.endpoint is not "static":
rules.append(rule.endpoint + ":" + methods + ":" + str(rule))
return rules
def template_values(name, function):
values = []
for call in parsed_content(name).find_all(nodes.Call):
if call.node.name == function:
values.append(
call.args[0].value
+ ":"
+ call.kwargs[0].key
+ ":"
+ call.kwargs[0].value.value
)
return values
def template_functions(name, function_name):
functions = []
for call in parsed_content(name).find_all(nodes.Call):
if call.node.name == function_name:
args_string = ""
if isinstance(call.node, nodes.Name) and isinstance(
call.args[0], nodes.Name
):
args_string += call.node.name + ":" + call.args[0].name
else:
args = getattr(call, "args")[0]
if isinstance(args, nodes.Const):
args_string += args.value + ":"
kwargs = (
call.kwargs[0]
if len(getattr(call, "kwargs")) > 0
else getattr(call, "kwargs")
)
if isinstance(kwargs, nodes.Keyword):
args_string += kwargs.key + ":"
if isinstance(kwargs.value, nodes.Const):
args_string += kwargs.value.value
else:
if isinstance(kwargs.value, nodes.Name):
args_string += kwargs.value.name
else:
args_string += kwargs.value.node.name
if isinstance(kwargs.value.arg, nodes.Const):
args_string += ":" + kwargs.value.arg.value
functions.append(args_string)
return functions
def show_jobs_for():
values = []
for node in parsed_content("_macros").find_all(nodes.For):
values.append(node.target.name + ":" + node.iter.name)
for call in parsed_content("_macros").find_all(nodes.Call):
if call.node.name == "show_job" and call.args[0].name == "job":
values.append("show_job:job")
return values
def employer_for():
values = []
for node in parsed_content("employer").find_all(nodes.For):
path = node.target.name
if isinstance(node.iter, nodes.Name):
path += ":" + node.iter.name
elif isinstance(node.iter, nodes.Call):
path += (
":"
+ node.iter.node.name
+ ":"
+ str(node.iter.args[0].value)
+ ":"
+ str(node.iter.args[1].node.name)
+ ":"
+ str(node.iter.args[1].arg.value)
)
values.append(path)
return values
def template_macros(name):
macros = []
for macro in parsed_content(name).find_all(nodes.Macro):
macros.append(macro.name + ":" + macro.args[0].name)
return macros
def template_block(name):
blocks = []
for block in parsed_content(name).find_all(nodes.Block):
blocks.append(block.name)
return blocks
def template_macro_soup(name, macro_name):
for macro in parsed_content(name).find_all(nodes.Macro):
if macro.name == macro_name:
html = ""
for template_data in macro.find_all(nodes.TemplateData):
html += template_data.data
return source_soup(html)
def template_data(name):
html = ""
for node in parsed_content(name).find_all(nodes.TemplateData):
html += node.data
return source_soup(html)
def template_variables(name):
return [
item.node.name + ":" + item.arg.value
for item in parsed_content(name).find_all(nodes.Getitem)
]
def template_exists(name):
return os.path.isfile("jobs/templates/" + name + ".html")
def template_source(name):
try:
return env.loader.get_source(env, name + ".html")[0]
except exceptions.TemplateNotFound:
return None
def source_soup(source):
return BeautifulSoup(source, "html.parser")
def template_soup(name):
return BeautifulSoup(template_source(name), "html.parser")
def template_find(name, tag, limit=None):
return BeautifulSoup(template_source(name), "html.parser").find_all(
tag, limit=limit
)
def parsed_content(name):
return env.parse(template_source(name))
def template_extends(name):
return list(meta.find_referenced_templates(parsed_content(name)))
def template_import(name):
for node in parsed_content(name).find_all(nodes.FromImport):
return (
node.template.value
+ ":"
+ ":".join(node.names)
+ ":"
+ str(node.with_context)
)