-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathparser.py
49 lines (39 loc) · 1.46 KB
/
parser.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
import logging
from docker_replay.args import config_args
from docker_replay.opts import config_opts
from docker_replay.models import DockerOpt, DockerArg
log = logging.getLogger('docker-replay')
class ConfigParser(object):
args = []
opts = []
def __init__(self, config):
self.config = config
# build all options
for op in config_opts:
o_val = self.get(op.key, op.otype.default)
self.opts += list(op.build(o_val))
for ap in config_args:
o_val = self.get(ap.key)
self.args += list(ap.build(o_val))
olen, alen = len(self.opts), len(self.args)
# filter null opts
self.opts = [ o for o in self.opts if not o.is_null() ]
self.args = [ o for o in self.args if not o.is_null() ]
log.info('parsed %d options (%d configured)' % (olen, len(self.opts)))
log.info('parsed %d args (%d configured)' % (alen, len(self.args)))
def get(self, key, default=None):
"""
Retrieve a top-level or nested key, e.g:
>>> get('Id')
>>> get('HostConfig.Binds')
"""
key_parts = key.split('.')
config = self.config
while key_parts:
try:
config = config[key_parts.pop(0)]
except KeyError:
log.warn('returning default for missing key: %s' % key)
return default
log.debug('get key: %s (%s)' % (key, config))
return config