Skip to content

Commit ac1aba4

Browse files
authored
Merge pull request #258 from connersaeli/conf-check-py2
Updates to the configuration check utility for Python 2.7
2 parents ae2c923 + 72440e4 commit ac1aba4

File tree

2 files changed

+55
-15
lines changed

2 files changed

+55
-15
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

+52-15
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
11
#!/usr/bin/env python
22
""" helper utiility to print out config info """
3+
from __future__ import print_function
34

45
import sys
56
import os
67
import json
8+
import logging
79
from getopt import getopt
810

911
from supremm.config import Config
12+
from supremm.scripthelpers import setuplogger
1013

1114
def usage():
1215
""" print usage """
13-
print "usage: {0} [OPTS]".format(os.path.basename(__file__))
14-
print " -s --section SECTION output the configuration data from the specified section"
15-
print " -i --item ITEM output the configuration data for the specified item"
16-
print " -h --help print this help message"
17-
16+
print("usage: {0} [OPTS]".format(os.path.basename(__file__)))
17+
print(" -d --debug set log level to debug")
18+
print(" -c --config specify the path to the configuration file")
19+
print(" -s --section SECTION output the configuration data from the specified section")
20+
print(" -i --item ITEM output the configuration data for the specified item")
21+
print(" -h --help print this help message")
1822

1923
def getoptions():
2024
""" process comandline options """
2125

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

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

2633
for opt in opts:
34+
if opt[0] in ("-d", "--debug"):
35+
retdata['log'] = logging.DEBUG
36+
if opt[0] in ("-c", "--config"):
37+
retdata['config'] = opt[1]
2738
if opt[0] in ("-s", "--section"):
2839
retdata['section'] = opt[1].encode("utf-8")
2940
if opt[0] in ("-i", "--item"):
@@ -41,18 +52,44 @@ def getoptions():
4152
def main():
4253
""" print out config data according to cmdline args """
4354
opts = getoptions()
44-
conf = Config()
45-
55+
56+
setuplogger(opts['log'])
57+
58+
if opts['config']:
59+
logging.debug("Using specified path: {}".format(opts['config']))
60+
else:
61+
logging.debug("Automatically detecting configuration path.")
62+
4663
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)
64+
conf = Config(opts['config'])
65+
except:
66+
logging.error("Configuration could not be found.")
67+
sys.exit(1)
5268

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

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

0 commit comments

Comments
 (0)