-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdata.py
80 lines (66 loc) · 3.73 KB
/
data.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 os
import collections
import json
GROUPING_JSON_VERSION = 1
KanjiGrouping = collections.namedtuple("KanjiGroups", ["version", "name", "lang", "source", "leftover_group", "groups"])
KanjiGroup = collections.namedtuple("KanjiGroup", ["name", "characters"])
ignore = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + \
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + \
"abcdefghijklmnopqrstuvwxyz" + \
"12345678901234567890" + \
"あいうゔえおぁぃぅぇぉかきくけこがぎぐげごさしすせそざじずぜぞ" + \
"たちつてとだぢづでどなにぬねのはひふへほばびぶべぼぱぴぷぺぽ" + \
"まみむめもやゃゆゅよょらりるれろわをんっ" + \
"アイウヴエオァィゥェォカキクケコガギグゲゴサシスセソザジズゼゾ" + \
"タチツテトダヂヅデドナニヌネノハヒフヘホバビブベボパピプペポ" + \
"マミムメモヤャユュヨョラリルレロワヲンッ" + \
"!\"$%&'()|=~-^@[;:],./`{+*}<>?\\_" + \
"@「;:」、。・‘{+*}<>?\_!”#$%&’()|=.〜~ー^ ゙゙゚" + \
"☆★*○●◎〇◯“…『』#♪゙〉〈→》《π×"
groupings = []
def load_from_folder(groupings, path):
for file in os.listdir(path):
filepath = path + "/" + file
try:
grouping_json = json.loads(open(filepath, "r", encoding = "utf-8", errors = "replace").read())
if "version" not in grouping_json or GROUPING_JSON_VERSION > grouping_json["version"]:
grouping_json = migrate_grouping(grouping_json)
groups = []
for group in grouping_json["groups"]:
groups.append(KanjiGroup(group["name"], group["characters"]))
groupings.append(KanjiGrouping(grouping_json["version"], grouping_json["name"], grouping_json["lang"], grouping_json["source"], grouping_json["leftover_group"], groups))
except Exception:
print(f"Failed to load Kanji Grid data file \"{filepath}\". It might be corrupted or outdated.")
def init_groups():
global groupings
groupings = []
cwd = os.path.dirname(__file__)
data_folder = cwd + "/data"
load_from_folder(groupings, data_folder)
# user_files persists across addon updates
user_data_folder = cwd + "/user_files/data"
os.makedirs(user_data_folder, exist_ok=True)
load_from_folder(groupings, user_data_folder)
groupings.sort(key = lambda group: group.lang + group.name)
def migrate_grouping(grouping_json):
if "version" not in grouping_json:
grouping_json["version"] = 0
grouping_json_updates = [grouping_update_1]
if len(grouping_json_updates) > grouping_json["version"]:
for grouping_json_update in grouping_json_updates[grouping_json["version"]:]:
grouping_json = grouping_json_update(grouping_json)
grouping_json["version"] = GROUPING_JSON_VERSION
return grouping_json
def grouping_update_1(grouping_json):
new_grouping_json = {}
new_grouping_json["version"] = grouping_json["version"]
new_grouping_json["name"] = grouping_json["name"]
new_grouping_json["lang"] = grouping_json["lang"]
new_grouping_json["source"] = grouping_json["source"]
new_grouping_json["leftover_group"] = grouping_json["data"][0][0]
del grouping_json["data"][0]
new_grouping_json["groups"] = []
for group in grouping_json["data"]:
new_grouping_json["groups"].append({"name": group[0], "characters": group[1]})
grouping_json = new_grouping_json
return grouping_json