forked from WIPACrepo/pyglidein
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
49 lines (43 loc) · 1.49 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
from ConfigParser import SafeConfigParser
import os
import ast
class Config(dict):
def __init__(self, path, default='etc/client_defaults.cfg'):
self.path = path
# read defaults
tmp = SafeConfigParser()
tmp.optionxform = str
tmp.read(default)
self._config_options_dict(tmp)
# read file
tmp = SafeConfigParser()
tmp.optionxform = str
tmp.read(path)
self._config_options_dict(tmp)
self._populate_partitions()
def _config_options_dict(self, config):
"""
Parsing config file
Args:
config: Python config parser object
"""
for section in config.sections():
if section not in self:
self[section] = {}
for option in config.options(section):
val = config.get(section, option)
try:
val = ast.literal_eval(val)
except Exception as e:
pass
self[section][option] = val
def _populate_partitions(self):
cluster_config = self.get('Cluster', dict())
if 'partitions' in cluster_config:
cluster_config['partitions'] = [k.strip() for k in cluster_config['partitions'].split(',')]
for k in cluster_config['partitions']:
if not k in self:
continue
config = dict(cluster_config)
config.update(self[k])
self[k] = config