Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to configuration check utility for Python 3 #263

Merged
merged 3 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/supremm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 48 additions & 12 deletions src/supremm/supremmconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand All @@ -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()