-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig_util.py
139 lines (129 loc) · 4.48 KB
/
config_util.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
config_schema = {
"version": {
"default": 1,
},
"pattern": {
"default": "",
},
"literal": {
"default": False,
},
"interval": {
"default": 180,
},
"groupby": {
"default": 0,
},
"sortby": {
"default": 2,
"enum": range(0, 4)
},
"lang": {
"default": "ja",
},
"unseen": {
"default": True,
},
"tooltips": {
"default": True,
},
"kanjionly": {
"default": True,
},
"saveimagequality": {
"default": 1,
},
"saveimagedelay": {
"default": 1000,
},
"onclickaction": {
"default": "browse",
"enum": ["", "browse", "copy", "search"],
},
"jafontcss": {
"default": "font-family: \"ヒラギノ角ゴ Pro W3\", \"Hiragino Kaku Gothic Pro\", Osaka, \"メイリオ\", Meiryo, \"MS Pゴシック\", \"MS PGothic\", \"MS UI Gothic\", Mincho, sans-serif;",
},
"zhfontcss": {
"default": "font-family: PingFang SC, Hiragino Sans GB, \"Microsoft YaHei New\", \"Microsoft Yahei\", \"微软雅黑\", 宋体, SimSun, STXihei, \"华文细黑\", sans-serif;",
},
"zhhansfontcss": {
"default": "font-family: PingFang SC, Hiragino Sans GB, \"Microsoft YaHei New\", \"Microsoft Yahei\", \"微软雅黑\", 宋体, SimSun, STXihei, \"华文细黑\", sans-serif;",
},
"zhhantfontcss": {
"default": "font-family: \"微軟正黑體\", \"Microsoft JhengHei\", \"Microsoft JhengHei UI\", \"微軟雅黑\", \"Microsoft YaHei\", \"Microsoft YaHei UI\", sans-serif;",
},
"kofontcss": {
"default": "font-family: \"Nanum Barun Gothic\", \"New Gulim\", \"새굴림\", \"애플 고딕\", \"맑은 고딕\", \"Malgun Gothic\", Dotum, \"돋움\", \"Noto Sans CJK KR\", \"Noto Sans CJK TC\", \"Noto Sans KR\", \"Noto Sans TC\", sans-serif;",
},
"vifontcss": {
"default": "font-family: \"Han-Nom Gothic\", \"Han Nom Gothic\", sans-serif;",
},
"jasearch": {
"default": "https://jisho.org/search/%s %23kanji",
},
"zhsearch": {
"default": "",
},
"zhhanssearch": {
"default": "",
},
"zhhantsearch": {
"default": "",
},
"kosearch": {
"default": "",
},
"visearch": {
"default": "",
},
"searchfilter": {
"default": ""
},
}
def set_config(mw, namespace_config):
config = dict(namespace_config.__dict__)
for key in list(config.keys()):
if key not in config_schema.keys():
del config[key]
mw.addonManager.writeConfig(__name__, config)
def get_config(mw):
config = mw.addonManager.getConfig(__name__)
if "defaults" in config: #migrate legacy configs that nested settings inside "defaults"
config = config["defaults"]
mw.addonManager.writeConfig(__name__, config)
if config_schema["version"]["default"] > config["version"]:
config = migrate_config(config)
mw.addonManager.writeConfig(__name__, config)
return validate_config(config)
def reset_config(mw):
default_config = dict(map(lambda item: (item[0], item[1]["default"]), config_schema.items()))
mw.addonManager.writeConfig(__name__, default_config)
def validate_config(config):
for config_schema_key in config_schema.keys():
if config_schema_key in config.keys():
if type(config_schema[config_schema_key]["default"]) is type(config[config_schema_key]):
if "enum" in config_schema[config_schema_key]:
if config[config_schema_key] in config_schema[config_schema_key]["enum"]:
continue
else:
continue
config[config_schema_key] = config_schema[config_schema_key]["default"]
return config
def migrate_config(config):
config_updates = [config_update_1]
if len(config_updates) > config["version"]:
for config_update in config_updates[config["version"]:]:
config = config_update(config)
config["version"] = config_schema["version"]["default"]
return config
def config_update_1(config):
if "browseonclick" in config and "copyonclick" in config:
if not config["browseonclick"] and not config["copyonclick"]:
config["onclickaction"] = "search"
elif config["copyonclick"]:
config["onclickaction"] = "copy"
else:
config["onclickaction"] = "browse"
del config["browseonclick"]
del config["copyonclick"]
return config