diff --git a/src/supremm/config.py b/src/supremm/config.py index f3e37c57..83a6cde4 100644 --- a/src/supremm/config.py +++ b/src/supremm/config.py @@ -43,6 +43,9 @@ def __init__(self, confpath=None): self._xdmodconfig = None + def __str__(self): + return json.dumps(self._config, indent=4) + @staticmethod def autodetectconfpath(): """ search known paths for the configuration directory diff --git a/src/supremm/supremmconf.py b/src/supremm/supremmconf.py index db0e4780..9e11fd76 100644 --- a/src/supremm/supremmconf.py +++ b/src/supremm/supremmconf.py @@ -4,28 +4,38 @@ import sys import os import json +import logging from getopt import getopt from supremm.config import Config +from supremm.scripthelpers import setuplogger def usage(): """ print usage """ print("usage: {0} [OPTS]".format(os.path.basename(__file__))) + print(" -d --debug set log level to debug") + print(" -c --config specify the path to the configuration file") print(" -s --section SECTION output the configuration data from the specified section") print(" -i --item ITEM output the configuration data for the specified item") print(" -h --help print this help message") - def getoptions(): """ process comandline options """ - retdata = {"section": None, "item": None} + retdata = {"log" : logging.ERROR, + "config" : None, + "section": None, + "item" : None} - opts, _ = getopt(sys.argv[1:], "s:i:h", ["section=", "item="]) + opts, _ = getopt(sys.argv[1:], "dc:s:i:h", ["debug", "config=", "section=", "item=", "help"]) for opt in opts: + if opt[0] in ("-d", "--debug"): + retdata['log'] = logging.DEBUG + if opt[0] in ("-c", "--config"): + retdata['config'] = opt[1] if opt[0] in ("-s", "--section"): - retdata['section'] = opt[1].encode("utf-8") + retdata['section'] = opt[1] if opt[0] in ("-i", "--item"): retdata['item'] = opt[1] if opt[0] in ("-h", "--help"): @@ -41,18 +51,44 @@ def getoptions(): def main(): """ print out config data according to cmdline args """ opts = getoptions() - conf = Config() - + + setuplogger(opts['log']) + + if opts['config']: + logging.debug("Using specified path: {}".format(opts['config'])) + else: + logging.debug("Automatically detecting configuration path.") + try: - section = conf.getsection(opts['section']) - if opts['item'] != None: - print(section[opts['item']]) - else: - print(json.dumps(section, indent=4)) + conf = Config(opts['config']) + except: + logging.error("Configuration could not be found.") + sys.exit(1) + if not opts['section']: + print(conf) + sys.exit(0) + + try: + section = conf.getsection(opts['section']) except KeyError: - sys.stderr.write("Error section \"%s\" not defined in configuration file.\n" % (opts['section'])) + logging.error("Section '{}' not defined in configuration file.".format(opts['section'])) sys.exit(1) + if opts['item']: + try: + item = section[opts['item']] + except KeyError: + logging.error("Item '{}' not defined in section '{}'.".format(opts['item'], opts['section'])) + sys.exit(1) + + if isinstance(item, dict): + item = json.dumps(item, indent=4) + + print(item) + + else: + print(json.dumps(section, indent=4)) + if __name__ == "__main__": main()