-
Notifications
You must be signed in to change notification settings - Fork 1
/
to_ipynb.py
46 lines (34 loc) · 1.1 KB
/
to_ipynb.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
import ast
import importlib
import importlib.util as util
import sys
from typing import List
USERDEFINE = 0
OTHERS = 1
def origin_checker(spec: importlib.machinery.ModuleSpec,
prefix: str = "/app") -> int:
origin: str = spec.origin # type: ignore
if origin.startswith(prefix):
return USERDEFINE
return OTHERS
def import_collector(pytree: ast.Module):
modules_list: List[importlib.machinery.ModuleSpec] = []
# from_modules_list: List[importlib.machinery.ModuleSpec] = []
for node in pytree.body:
if isinstance(node, ast.Import):
for name in node.names:
spec = util.find_spec(name.name)
if spec is not None:
modules_list.append(spec)
elif isinstance(node, ast.ImportFrom):
pass
return modules_list
if __name__ == "__main__":
sys.path.append("./")
pyfile = sys.argv[1]
with open(pyfile, mode="r", encoding="utf-8") as f:
pysrc = f.read()
pytree = ast.parse(pysrc)
modules_list = import_collector(pytree)
import pdb
pdb.set_trace()