X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=tools%2Fnd_querycfg;h=b4cff10239b9d6d220948f25ae0432b4cd98536d;hb=585d2e26c6346432d4e3714ab678d4d52c777259;hp=9e73f558aaa01b83bf9aeeada43fb032386149d5;hpb=852a143c9b6e213f0579d4ede0010d6b5db4ac68;p=neurodebian.git 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())