Skip to content

Commit 7c81feb

Browse files
authored
Merge pull request #263 from connersaeli/conf-check-py3
Updates to configuration check utility for Python 3
2 parents 5227f21 + 3141158 commit 7c81feb

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

src/supremm/config.py

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ def __init__(self, confpath=None):
4343

4444
self._xdmodconfig = None
4545

46+
def __str__(self):
47+
return json.dumps(self._config, indent=4)
48+
4649
@staticmethod
4750
def autodetectconfpath():
4851
""" search known paths for the configuration directory

src/supremm/supremmconf.py

+48-12
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,38 @@
44
import sys
55
import os
66
import json
7+
import logging
78
from getopt import getopt
89

910
from supremm.config import Config
11+
from supremm.scripthelpers import setuplogger
1012

1113
def usage():
1214
""" print usage """
1315
print("usage: {0} [OPTS]".format(os.path.basename(__file__)))
16+
print(" -d --debug set log level to debug")
17+
print(" -c --config specify the path to the configuration file")
1418
print(" -s --section SECTION output the configuration data from the specified section")
1519
print(" -i --item ITEM output the configuration data for the specified item")
1620
print(" -h --help print this help message")
1721

18-
1922
def getoptions():
2023
""" process comandline options """
2124

22-
retdata = {"section": None, "item": None}
25+
retdata = {"log" : logging.ERROR,
26+
"config" : None,
27+
"section": None,
28+
"item" : None}
2329

24-
opts, _ = getopt(sys.argv[1:], "s:i:h", ["section=", "item="])
30+
opts, _ = getopt(sys.argv[1:], "dc:s:i:h", ["debug", "config=", "section=", "item=", "help"])
2531

2632
for opt in opts:
33+
if opt[0] in ("-d", "--debug"):
34+
retdata['log'] = logging.DEBUG
35+
if opt[0] in ("-c", "--config"):
36+
retdata['config'] = opt[1]
2737
if opt[0] in ("-s", "--section"):
28-
retdata['section'] = opt[1].encode("utf-8")
38+
retdata['section'] = opt[1]
2939
if opt[0] in ("-i", "--item"):
3040
retdata['item'] = opt[1]
3141
if opt[0] in ("-h", "--help"):
@@ -41,18 +51,44 @@ def getoptions():
4151
def main():
4252
""" print out config data according to cmdline args """
4353
opts = getoptions()
44-
conf = Config()
45-
54+
55+
setuplogger(opts['log'])
56+
57+
if opts['config']:
58+
logging.debug("Using specified path: {}".format(opts['config']))
59+
else:
60+
logging.debug("Automatically detecting configuration path.")
61+
4662
try:
47-
section = conf.getsection(opts['section'])
48-
if opts['item'] != None:
49-
print(section[opts['item']])
50-
else:
51-
print(json.dumps(section, indent=4))
63+
conf = Config(opts['config'])
64+
except:
65+
logging.error("Configuration could not be found.")
66+
sys.exit(1)
5267

68+
if not opts['section']:
69+
print(conf)
70+
sys.exit(0)
71+
72+
try:
73+
section = conf.getsection(opts['section'])
5374
except KeyError:
54-
sys.stderr.write("Error section \"%s\" not defined in configuration file.\n" % (opts['section']))
75+
logging.error("Section '{}' not defined in configuration file.".format(opts['section']))
5576
sys.exit(1)
5677

78+
if opts['item']:
79+
try:
80+
item = section[opts['item']]
81+
except KeyError:
82+
logging.error("Item '{}' not defined in section '{}'.".format(opts['item'], opts['section']))
83+
sys.exit(1)
84+
85+
if isinstance(item, dict):
86+
item = json.dumps(item, indent=4)
87+
88+
print(item)
89+
90+
else:
91+
print(json.dumps(section, indent=4))
92+
5793
if __name__ == "__main__":
5894
main()

0 commit comments

Comments
 (0)