forked from bookfere/Ebook-Translator-Calibre-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
72 lines (58 loc) · 1.74 KB
/
config.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
from calibre.utils.config import JSONConfig
preferences = JSONConfig('plugins/ebook_translator')
default_config = {
'to_library': True,
'output_path': None,
'translate_engine': 'Google(Free)',
'chatgpt_prompt': {},
'api_key': {},
'proxy_enabled': False,
'proxy_setting': [],
'request_attempt': 3,
'request_interval': 5,
'cache_enabled': True,
'log_translation': True,
'translation_position': 'after',
'translation_color': None,
'rule_mode': 'normal',
'filter_rules': [],
'custom_engines': {},
'glossary_enabled': False,
'glossary_path': None,
'merge_enabled': False,
'merge_length': 2000,
'preferred_language': {},
}
def init_config():
if not preferences:
save_config(default_config)
for key, value in default_config.items():
if key not in preferences:
set_config(key, value)
return {k: get_config(k, v) for k, v in default_config.items()}
def save_config(config):
for key, value in config.items():
set_config(key, value)
def get_configs(*keys):
return [get_config(key) for key in keys]
def get_config(key, default=None):
temp = preferences.copy()
for key in key.split('.'):
if isinstance(temp, dict) and key in temp:
temp = temp[key]
continue
temp = None
return temp if temp is not None else default
def set_config(key, value):
temp = preferences
keys = key.split('.')
while len(keys) > 0:
key = keys.pop(0)
if len(keys) > 0:
if key in temp and isinstance(temp[key], dict):
temp = temp[key]
continue
temp[key] = {}
temp = temp[key]
continue
temp[key] = value