-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.py
executable file
·48 lines (42 loc) · 1.5 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
#!/usr/bin/env python
import json, yaml
import sys
import logging
logger = logging.getLogger(__name__)
class Config(object):
def __init__(self):
self.cfg = None
def load(self, file):
with open(file) as f:
if file.endswith('.json'):
logger.debug('Loading {} as json'.format(file))
self.cfg = json.load(f)
else:
logger.debug('Loading {} as yaml'.format(file))
self.cfg = yaml.load(f)
def get(self, what, who=None, default=None):
'''Get config setting - first look in subconfig, if attribute is not there, look in global settings'''
if who:
subcfg = self.cfg.get(who, None)
if subcfg:
x = subcfg.get(what, None)
if x:
return x
c = self.cfg.get(what, None)
logger.debug("get({}, {}) -> '{}'".format(what, who, c))
if c:
return c
return default
def getpath(self, what, who=None):
'''Get path setting - concatinating global and sub configs'''
#FIXME: Handle missing trailing '/' etc
p1 = self.cfg.get(what, '')
if who:
subcfg = self.cfg.get(who, None)
if subcfg:
p2 = subcfg.get(what, '')
c= p1+p2
logger.debug("get({}, {}) -> '{}'".format(what, who, c))
return c
logger.debug("get({}, {}) -> '{}'".format(what, who, p1))
return p1.rstrip('/')