-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparse_code.py
More file actions
73 lines (61 loc) · 2.11 KB
/
Copy pathparse_code.py
File metadata and controls
73 lines (61 loc) · 2.11 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
import ast
import itertools
import os
from pathlib import Path
from typing import Callable, Iterable, List, Tuple
_IGNORE_DIRS = {
"notebooks",
".ipynb_checkpoints",
os.sep + "build" + os.sep,
}
def parse_file(filepath):
with open(filepath) as f:
file_parsed = ast.parse(f.read())
return file_parsed
def get_all_python_files(repo_path, repo) -> List[Path]:
python_files = []
for fp_py in Path(repo_path).glob("**/*.py"):
if fp_py.is_dir() or any(d in str(fp_py) for d in _IGNORE_DIRS):
continue
python_files.append(fp_py)
return python_files
def get_function_definitions(module):
funcdefs = [
n for n in ast.walk(module) if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef))
]
return funcdefs
def iterate_over_expressions(node: ast.AST) -> Iterable[ast.AST]:
"""Ignoring function definitions."""
additionals_subnodes_info: List[Tuple[Tuple, Callable]] = [
((ast.If, ast.While), lambda n: [n.test]),
((ast.For,), lambda n: [n.iter]),
((ast.AsyncFor,), lambda n: [n.iter]),
((ast.With, ast.AsyncWith), lambda n: [s.context_expr for s in n.items]),
]
nodes_with_subnodes = (
ast.FunctionDef,
ast.AsyncFunctionDef,
ast.If,
ast.For,
ast.AsyncFor,
ast.Module,
ast.ClassDef,
ast.Try,
ast.With,
ast.AsyncWith,
ast.While,
)
for bases, subnodes_getter in additionals_subnodes_info:
if isinstance(node, bases):
yield from subnodes_getter(node)
nodes_to_iter = (
_get_try_node_children(node) if isinstance(node, ast.Try) else getattr(node, "body", [])
)
for child_node in nodes_to_iter:
if isinstance(child_node, nodes_with_subnodes):
if not isinstance(child_node, (ast.FunctionDef, ast.AsyncFunctionDef)):
yield from iterate_over_expressions(child_node)
else:
yield child_node
def _get_try_node_children(try_node: ast.Try):
return itertools.chain(try_node.body, try_node.finalbody, *[n.body for n in try_node.handlers])