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
|
#!/usr/bin/python3
"""
Dead simple script to query the NeuroDebian dev config (or any other Python config file)
"""
import os, sys
from configparser import ConfigParser
from optparse import OptionParser
__prog__ = os.path.basename(sys.argv[0])
__version__ = '0.1.0'
if __name__ == '__main__':
parser = OptionParser(
usage="%s [OPTIONS] [section] [field]\n" % __prog__ + __doc__,
version="%prog " + __version__)
parser.add_option(
'-f', '--config-file', default="/etc/neurodebian/neurodebian.cfg",
help="name of the config file")
parser.add_option(
'-F', '--value-separator', default="=",
help="string to separate key from value")
opts, argv = parser.parse_args()
if not os.path.exists(opts.config_file):
sys.stderr.write("ERROR: File %s does not exist\n" % opts.config_file)
sys.exit(1)
cfg = ConfigParser()
cfg.read(opts.config_file)
if len(argv) == 2:
print(cfg.get(argv[0], argv[1]))
elif len(argv) == 1:
# Print values of the section, =-separate key from value
print('\n'.join([opts.value_separator.join(x)
for x in cfg.items(argv[0])]))
else:
# Print section names
print('\n'.join(cfg.sections()))
|