-
Notifications
You must be signed in to change notification settings - Fork 14
/
config.py
55 lines (42 loc) · 1.19 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
import ujson
import sys
config_path = "/sd/config.json"
normal_brightness = 8
config_cache = None
def get_config():
global config_cache
if config_cache is None:
conf = {}
try:
f = open(config_path, "rb")
conf = ujson.load(f)
f.close()
except OSError:
print("config file not exist, use default dict")
except ValueError:
print("invalid config file format, use default dict")
if type(conf) is not dict:
conf = {}
config_cache = conf
return config_cache
def save_config(key, value):
config = get_config()
config[key] = value
save_config_to_file(config)
def save_config_to_file(config):
try:
f = open(config_path, "wb")
ujson.dump(config, f)
f.close()
except OSError as e:
sys.print_exception(e)
print("save_config_to_file end")
def get_config_by_key(key):
config = get_config()
if key in config:
return config[key]
else:
return None
def get_brightness():
brightness_conf = get_config_by_key("brightness")
return brightness_conf if brightness_conf is not None else normal_brightness