-
Notifications
You must be signed in to change notification settings - Fork 0
/
configobj-get.py
executable file
·46 lines (40 loc) · 1.46 KB
/
configobj-get.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
#!/usr/bin/env python
from configobj import ConfigObj
from os.path import expanduser
import logging
import argparse
import sys
def getter(config_object, element, *elements, fail=True):
logging.debug('obj={}\nelm={}'.format(config_object, element))
if element == '*':
if len(elements) == 0:
yield from config_object.values()
else:
for thing in config_object.values():
yield from getter(thing, *elements, fail=False)
elif element in config_object:
if len(elements) == 0:
yield config_object[element]
else:
yield from getter(config_object[element], *elements)
elif fail:
raise KeyError()
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--debug', action='store_true',
help='print debugging info')
parser.add_argument('-e', '--expand', '--expanduser', action='store_true',
help="expand ~user strings in the result")
parser.add_argument('element', type=lambda x: x.split('.'),
help='the element to get')
parser.add_argument('file', type=argparse.FileType('r'), default=sys.stdin,
nargs='?', help='the config file to parse')
args = parser.parse_args()
if args.debug:
logging.basicConfig(level=logging.DEBUG)
logging.debug(args)
config = ConfigObj(args.file)
for item in getter(config, *args.element):
if args.expand:
print(expanduser(item))
else:
print(item)