-
Notifications
You must be signed in to change notification settings - Fork 0
/
setting.py
66 lines (52 loc) · 1.78 KB
/
setting.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
# -*- coding: utf-8 -*-
import enum
import json
import os
from language import Language, Locale
from theme import Theme
from utils import FileUtils, LanguageUtils, ThemeUtils
# settings(settings.json)
class Setting:
def __init__(self):
self.language = Language.Chinese.value
self.theme = Theme.COMMON_DEFAULT
def get_locale(self):
if self.language == Language.English.value:
return Locale.English
else:
return Locale.Chinese
def get_language(self):
return self.language
def get_theme(self):
return FileUtils.get_fullname(self.theme)
def load(self):
path = os.path.join(os.getcwd(), 'setting.json')
if not os.path.exists(path):
print('Setting: file does not exist(%s)' % path)
return
with open(path, 'r', encoding='utf-8') as f:
content = f.read()
if len(content) == 0:
return
# noinspection PyBroadException
try:
setting = json.loads(content)
except:
setting = None
if not setting:
return
if isinstance(setting, dict):
language = setting.get('language')
if LanguageUtils.validate(language):
self.language = language
theme = setting.get('theme')
if ThemeUtils.validate(theme):
self.theme = theme
@staticmethod
def save(language, theme):
if not language or not theme:
return
path = os.path.join(os.getcwd(), 'setting.json')
with open(path, 'w', encoding='utf-8') as f:
setting = {'language': language, 'theme': theme}
f.write(json.dumps(setting))