-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_kernels.py
executable file
·61 lines (54 loc) · 1.77 KB
/
process_kernels.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
#!/usr/local/bin/python3.8
import datajourney as DJ
import json as J
from os import listdir
from os.path import isfile, join
from graphviz import Source
def extractSourceCode(notebook):
src=""
for cell in jj['cells']:
if cell['cell_type'] == 'code':
source = cell['source']
if(isinstance(source, str) ):
src = src + "\n" + source
else:
for line in source:
src = src + "\n" + line
return src
import re
indir = "kernels/"
srcdir = "sources/"
outdir = "graphs/"
files = [f for f in listdir(indir) if isfile(join(indir, f))]
for f in files:
with open(indir + f) as notebook:
print("Processing: {0}".format(f))
jj = J.load(notebook)
src = extractSourceCode(jj)
if src.strip() == "":
print("EMPTY: no source code in notebook {0}".format(f))
continue
#
comm = ["^%","\n%","^!","\n!"]
for c in comm:
src = re.sub(c, "\n### ", src)
s = open(srcdir + f[:-5] + "py", "w")
s.write(src)
collector = DJ.FindDependencies(f)
try:
collector.collect(src)
g = collector.getStringCollected()
# Save
o = open(outdir + f[:-5] + "digraph", "w")
o.write(g)
# src = Source(g)
# src.format = "png"
# src.render(imgdir + f[:-5])
except AttributeError as err:
print("AttributeError: {0} [{1}]".format(err, f))
except SyntaxError as err:
print("SyntaxError: {0} [{1}]".format(err, f))
except TypeError as err:
print("TypeError: {0} [{1}]".format(err, f))
except Exception as err:
print("Exception: {0} [{1}]".format(err, f))