-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathconfig.py
77 lines (58 loc) · 1.76 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
73
74
75
76
77
"""
Author: Roee Hay / Aleph Research / HCL Technologies
"""
from serializable import *
import json
import ConfigParser
DATA_PATH = "./data.json"
USER_CONFIG_PATH = "./abootool.cfg"
import io
config = None
class MetaConfig(type):
def __getattr__(cls, name):
return cls.get_config().__getattr__(name)
def __setattr__(cls, name, val):
return cls.get_config().__setattr__(name, val)
def __repr__(cls):
return cls.get_config().__repr__()
class Config(Serializable):
__metaclass__ = MetaConfig
config = None
@classmethod
def overlay(cls, data):
dest = {}
for t in data:
if data[t]:
dest[t] = data[t]
cls.get_config().set_data(dest)
@classmethod
def get_config(cls):
global config
if not config:
config = Config()
config.set_data(json.load(file(DATA_PATH, "rb")))
data = "[root]\n"+file(USER_CONFIG_PATH, "rb").read()
fp = io.BytesIO(data)
parser = ConfigParser.RawConfigParser()
parser.readfp(fp)
cfg = {}
for k in parser.options("root"):
try:
cfg[k] = parser.getboolean("root", k)
continue;
except ValueError:
pass
try:
cfg[k] = parser.getint("root", k)
continue;
except ValueError:
pass
try:
cfg[k] = parser.getfloat("root", k)
continue;
except ValueError:
pass
cfg[k] = parser.get("root", k)
config.set_data(cfg)
return config
Config.get_config()