-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcentroid.py
More file actions
105 lines (85 loc) · 3.12 KB
/
centroid.py
File metadata and controls
105 lines (85 loc) · 3.12 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import json
from collections import defaultdict
class Config:
def __init__(self, config):
if type(config) is dict or type(config) is list:
self.raw_config = config
else:
self.raw_config = json.loads(config)
self._validate_unique_keys()
def __getattr__(self, attrib):
return self[attrib]
# config['key']
def __getitem__(self, key):
value = _get_value(key, self.raw_config)
if type(value) is dict or type(value) is list:
return Config(value)
return value
def __iter__(self):
if type(self.raw_config) is dict:
for key, value in self.raw_config.items():
if type(value) is dict:
yield key, Config(value)
else:
yield key, value
else:
for value in self.raw_config:
if type(value) is dict:
yield Config(value)
else:
yield value
# to string
def __str__(self):
return str(json.dumps(self.raw_config))
def _validate_unique_keys(self):
normalized_keys = defaultdict(list)
for key, _ in self.raw_config.items():
normalized_keys[_get_normalised_key(key)].append(key)
dups = list()
for _, value in normalized_keys.items():
if len(value) > 1:
dups.extend(value)
if len(dups) > 0:
raise KeyError("centroid.Config instance contains duplicate keys: " + ", ".join(dups))
def for_environment(self, env):
env_json = self.raw_config[env]
actual_key = _get_actual_key('all', self.raw_config)
if actual_key is None:
config = Config(env_json)
else:
all_json = _get_value(actual_key, self.raw_config)
config = Config(_dict_merge(all_json, env_json))
if not config.__contains__('environment'):
config.raw_config['environment'] = env
return config
def __contains__(self, key):
return _get_actual_key(key, self.raw_config) is not None
@staticmethod
def from_file(filename):
with open(filename) as json_file:
str_json = json_file.read()
return Config(str_json)
# case insensitive hashtable helpers
def _get_normalised_key(unnormalisedKey):
return unnormalisedKey.replace('_','').lower()
def _get_value(key, hashtable):
if type(key) is int:
return hashtable[key]
actual_key = _get_actual_key(key, hashtable)
if actual_key is None:
raise KeyError('centroid.Config instance does not contain key: ' + key)
return hashtable[actual_key]
def _get_actual_key(key, hashtable):
result = [ k for k in list(hashtable.keys()) if _get_normalised_key(key) == _get_normalised_key(k) ]
if len(result) > 0:
return result[0]
return None
def _dict_merge(left, right):
if not isinstance(right, dict):
return right
for k, v in right.items():
if k in left and isinstance(left[k], dict):
left[k] = _dict_merge(left[k], v)
else:
left[k] = v
return left