1
1
#!/usr/bin/env python
2
2
""" helper utiility to print out config info """
3
+ from __future__ import print_function
3
4
4
5
import sys
5
6
import os
6
7
import json
8
+ import logging
7
9
from getopt import getopt
8
10
9
11
from supremm .config import Config
12
+ from supremm .scripthelpers import setuplogger
10
13
11
14
def usage ():
12
15
""" 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" )
18
22
19
23
def getoptions ():
20
24
""" process comandline options """
21
25
22
- retdata = {"section" : None , "item" : None }
26
+ retdata = {"log" : logging .ERROR ,
27
+ "config" : None ,
28
+ "section" : None ,
29
+ "item" : None }
23
30
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 " ])
25
32
26
33
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 ]
27
38
if opt [0 ] in ("-s" , "--section" ):
28
39
retdata ['section' ] = opt [1 ].encode ("utf-8" )
29
40
if opt [0 ] in ("-i" , "--item" ):
@@ -41,18 +52,44 @@ def getoptions():
41
52
def main ():
42
53
""" print out config data according to cmdline args """
43
54
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
+
46
63
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 )
52
68
69
+ if not opts ['section' ]:
70
+ print (conf )
71
+ sys .exit (0 )
72
+
73
+ try :
74
+ section = conf .getsection (opts ['section' ])
53
75
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' ]))
55
77
sys .exit (1 )
56
78
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
+
57
94
if __name__ == "__main__" :
58
95
main ()
0 commit comments