-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.py
40 lines (29 loc) · 839 Bytes
/
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
#!/usr/bin/env python
#-*- coding: utf8 -*-
import os
import json
import logging
CONFIG_FILE = 'config.json'
DEFAULT_CONFIG = {}
def Parse():
if not os.path.isfile(CONFIG_FILE):
return DEFAULT_CONFIG
try:
with open(CONFIG_FILE, 'r') as fh:
cfg = json.load(fh)
except Exception as e:
logging.error('Error decoding config: %s', e)
return DEFAULT_CONFIG
if not isinstance(cfg, dict):
logging.error('Config is not a dict')
return DEFAULT_CONFIG
return cfg
def Store(cfg):
if not isinstance(cfg, dict):
logging.error('Trying to store config which is not a dict')
return
try:
with open(CONFIG_FILE, 'w') as fh:
json.dump(cfg, fh)
except Exception as e:
logging.error('Error storing config: %s', e)