-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_interpol
executable file
·74 lines (65 loc) · 2.85 KB
/
config_interpol
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
#!/usr/bin/env python
import os
import sys
from collections import OrderedDict
from configparser import ConfigParser, InterpolationMissingOptionError, NoOptionError
FAKE_SECTION = 'FAKESECTION'
def load_config(fp):
cfg = ConfigParser()
cfg.optionxform = str
# prepend with fake header
cfg.read_file([f'[{FAKE_SECTION}]\n' + os.linesep] + fp.readlines())
return cfg
def err(msg, code=1):
sys.stderr.write(msg + '\n')
exit(code)
if __name__ == "__main__":
overlay_config = None
if len(sys.argv) == 2 and os.path.isfile(sys.argv[1]):
with open(sys.argv[1], 'r') as fp:
overlay_config = load_config(fp)
config = load_config(sys.stdin)
sections = OrderedDict()
try:
for section in config.sections():
# we don't use config.items() here to avoid emitting the defaults too
for option in config.options(section=section):
value = None
if overlay_config and overlay_config.has_section(section) and overlay_config.has_option(section, option):
value = overlay_config.get(section=section, option=option, vars=os.environ)
if value is None:
value = config.get(section=section, option=option, vars=os.environ)
# store this now
if section not in sections:
sections[section] = dict()
if option not in sections[section]:
sections[section][option] = dict()
sections[section][option] = value
if overlay_config:
# now supplement the overlay options
for section in overlay_config.sections():
# we don't use config.items() here to avoid emitting the defaults too
for option in overlay_config.options(section=section):
value = overlay_config.get(section=section, option=option, vars=os.environ)
if section not in sections:
sections[section] = dict()
if option not in sections[section]:
sections[section][option] = dict()
sections[section][option] = value
except InterpolationMissingOptionError as e:
err(e.message)
except NoOptionError as e:
section_name = "section: '{}'."
if section == FAKE_SECTION:
section_name = "default section."
err(f"No option '{option}' in {section_name}", 2)
# now output, starting with the default section
if FAKE_SECTION in sections:
for option, value in list(sections[FAKE_SECTION].items()):
print(f'{option}={value}')
del sections[FAKE_SECTION]
# now do each section
for section in sections:
print(f'[{section}]')
for option, value in list(sections[section].items()):
print(f'{option}={value}')