-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscover_imports.py
39 lines (30 loc) · 977 Bytes
/
discover_imports.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
import ast
from pprint import pprint
from pathlib import Path
def main():
analyzer = Analyzer()
for path in Path('.').rglob('*.py'):
if path.parts[0] == 'venv':
continue
if '.ipynb_checkpoints' in path.parts:
continue
print(path)
with open(path, "r") as source:
tree = ast.parse(source.read())
analyzer.visit(tree)
analyzer.report()
class Analyzer(ast.NodeVisitor):
def __init__(self):
self.stats = {"import": set(), "from": set()}
def visit_Import(self, node):
for alias in node.names:
self.stats["import"].add(alias.name)
self.generic_visit(node)
def visit_ImportFrom(self, node):
for alias in node.names:
self.stats["from"].add(node.module)
self.generic_visit(node)
def report(self):
pprint(self.stats['import'].union(self.stats['from']))
if __name__ == "__main__":
main()