-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_cr_and_diff.py
80 lines (63 loc) · 2.73 KB
/
create_cr_and_diff.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
import asyncio
import json
import os
import sys
from app.parsing import extract_cr
from app.parsing.difftool.diffmaker import CRDiffMaker
from app.parsing.formatter import CRFormatterFactory
async def diff(old_txt, new_txt, old_set_code=None, new_set_code=None, forced_matches=None):
old_txt = CRFormatterFactory.create_formatter(old_set_code).format(old_txt)
new_txt = CRFormatterFactory.create_formatter(new_set_code).format(new_txt)
old_json = await extract_cr.extract(old_txt)
new_json = await extract_cr.extract(new_txt)
diff_json = CRDiffMaker(forced_matches).diff(old_json["rules"], new_json["rules"])
return old_json, new_json, diff_json
async def diff_save(old, new, forced_matches=None):
if forced_matches:
for i in range(len(forced_matches)):
if type(forced_matches[i]) == str:
forced_matches[i] = (forced_matches[i], forced_matches[i])
# assumes the last three letters before extension are the set code
o_code = old[-7:-4]
n_code = new[-7:-4]
diff_code = o_code + "-" + n_code
with open(old, "r") as old_file:
old_txt = old_file.read()
with open(new, "r") as new_file:
new_txt = new_file.read()
old, new, dff = await diff(old_txt, new_txt, o_code, n_code, forced_matches)
with open(os.path.join(cr_out_dir, o_code + ".json"), "w") as file:
json.dump(old["rules"], file)
with open(os.path.join(cr_out_dir, n_code + ".json"), "w") as file:
json.dump(new["rules"], file)
with open(os.path.join(gloss_dir, o_code + ".json"), "w") as file:
json.dump(old["glossary"], file)
with open(os.path.join(gloss_dir, n_code + ".json"), "w") as file:
json.dump(new["glossary"], file)
with open(os.path.join(key_dir, o_code + ".json"), "w") as file:
json.dump(old["keywords"], file)
with open(os.path.join(key_dir, n_code + ".json"), "w") as file:
json.dump(new["keywords"], file)
with open(os.path.join(diff_dir, diff_code + ".json"), "w") as file:
json.dump(dff.diff, file)
with open(os.path.join(maps_dir, diff_code + ".json"), "w") as file:
json.dump(dff.matches, file)
print(o_code, n_code)
async def diffall():
filepaths = sorted([os.path.join(cr_in_dir, path) for path in os.listdir(cr_in_dir)], reverse=True)
for i in range(len(filepaths) - 1):
new = filepaths[i]
old = filepaths[i + 1]
await diff_save(old, new)
cr_in_dir = "app/static/raw_docs/cr"
cr_out_dir = "./gen/cr"
diff_dir = "./gen/diff"
maps_dir = "./gen/map"
gloss_dir = "./gen/gloss"
key_dir = "./gen/keywords"
if __name__ == "__main__":
# asyncio.run(diffall())
old = sys.argv[1]
new = sys.argv[2]
forced = []
asyncio.run(diff_save(old, new, forced))