From 919ba189a3c4c7aef84925ec9cba5bf51b629502 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Mon, 3 Dec 2012 10:12:22 -0500 Subject: [PATCH] ENH: improved nd_querycfg a bit: query content of a section or list of sections --- tools/nd_querycfg | 49 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/tools/nd_querycfg b/tools/nd_querycfg index 9e73f55..b4cff10 100755 --- a/tools/nd_querycfg +++ b/tools/nd_querycfg @@ -1,14 +1,43 @@ #!/usr/bin/python -# -# Dead simple script to query the NeuroDebian dev config. -# -import sys +""" +Dead simple script to query the NeuroDebian dev config (or any other Python config file) +""" + +import os, sys + from ConfigParser import SafeConfigParser +from optparse import OptionParser + +__prog__ = os.path.basename(sys.argv[0]) +__version__ = '0.0.2' + +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() -# XXX add check if it is there at all -# XXX support more locations -cfg_path="/etc/neurodebian/neurodebian.cfg" + 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 = SafeConfigParser() -cfg.read(cfg_path) -print cfg.get(sys.argv[1], sys.argv[2]) + cfg = SafeConfigParser() + 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()) -- 2.39.2