forked from rsalveti/esp8266
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
40 lines (34 loc) · 1.1 KB
/
utils.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
# Utils (config, etc)
#
# Copyright: Ricardo Salveti <[email protected]>
#
# SPDX-License-Identifier: MIT
class Config:
def __init__(self, defaults = {}, config_file = "/config.json"):
self.config = defaults
self.config_file = config_file
import ujson as json
try:
with open(config_file) as f:
config = json.loads(f.read())
except (OSError, ValueError):
print("Could not load %s, saving defaults" % config_file)
self.store_config()
else:
self.config.update(config)
print("Loaded config from", config_file)
def store_config(self):
import ujson as json
try:
with open(self.config_file, "w") as f:
f.write(json.dumps(self.config))
except OSError:
print("Could not save", self.config_file)
def get(self, name, default = None):
try:
return self.config[name]
except KeyError:
return default
def set(self, name, value):
self.config[name] = value
self.store_config()