-
Notifications
You must be signed in to change notification settings - Fork 1
/
notebookConversion.py
94 lines (80 loc) · 2.58 KB
/
notebookConversion.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
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
"""
Designed to read all the .ipynb files in a directory and make respective .py files for each.
"""
import json
import os
import hashlib
import csv
import traceback
def writefile(file_list, hash_list):
try:
with open('check.csv', 'w', newline='\n') as csv_file:
writer = csv.writer(csv_file, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for i in range(len(file_list)):
writer.writerow([file_list[i]] + [hash_list[i]])
except ValueError:
traceback.print_exc()
def readfile():
try:
output = []
with open('check.csv', newline='\n') as csv_file:
reader = csv.reader(csv_file, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for row in reader:
output.append(row)
return output
except ValueError:
traceback.print_exc()
def writecode(filename, codestring):
try:
pythonfile = filename[:-6]+".py"
with open(pythonfile, 'w') as writer:
writer.write(codestring)
except BaseException:
traceback.print_exc()
def extractcode(filename):
notebook = ''
try:
with open(filename, 'r', encoding='utf-8') as f:
notebook = json.load(f)
codeString = ''
for cell in notebook['cells']:
for line in cell['source']:
codeString += line
codeString += "\n\n"
# print(codeString, '\n')
writecode(filename, codeString)
except BaseException:
traceback.print_exc()
def hash(file):
block_size = 65536
hasher = hashlib.sha1()
with open(file, "rb") as open_file:
buf = open_file.read(block_size)
while len(buf) > 0:
hasher.update(buf)
buf = open_file.read(block_size)
return hasher.hexdigest()
def main(directory=""):
path = "./"
if directory != "":
path = directory
files = os.listdir(path)
hash_list = []
file_list = []
for f in files:
file_extension = f[-6:]
if file_extension == ".ipynb":
hashcode = hash(f)
file_list.append(f)
hash_list.append(hashcode)
print(f, hashcode)
else:
print(f)
obj = readfile()
for row in obj:
if not hash_list.__contains__(row[1]):
print("File has changed. Updating %s.py..." % (row[0][:-6]))
extractcode(row[0])
writefile(file_list, hash_list)
if __name__ == '__main__':
main()