4
4
import sys
5
5
import os
6
6
import json
7
+ import logging
7
8
from getopt import getopt
8
9
9
10
from supremm .config import Config
11
+ from supremm .scripthelpers import setuplogger
10
12
11
13
def usage ():
12
14
""" print usage """
13
15
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" )
14
18
print (" -s --section SECTION output the configuration data from the specified section" )
15
19
print (" -i --item ITEM output the configuration data for the specified item" )
16
20
print (" -h --help print this help message" )
17
21
18
-
19
22
def getoptions ():
20
23
""" process comandline options """
21
24
22
- retdata = {"section" : None , "item" : None }
25
+ retdata = {"log" : logging .ERROR ,
26
+ "config" : None ,
27
+ "section" : None ,
28
+ "item" : None }
23
29
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 " ])
25
31
26
32
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 ]
27
37
if opt [0 ] in ("-s" , "--section" ):
28
- retdata ['section' ] = opt [1 ]. encode ( "utf-8" )
38
+ retdata ['section' ] = opt [1 ]
29
39
if opt [0 ] in ("-i" , "--item" ):
30
40
retdata ['item' ] = opt [1 ]
31
41
if opt [0 ] in ("-h" , "--help" ):
@@ -41,18 +51,44 @@ def getoptions():
41
51
def main ():
42
52
""" print out config data according to cmdline args """
43
53
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
+
46
62
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 )
52
67
68
+ if not opts ['section' ]:
69
+ print (conf )
70
+ sys .exit (0 )
71
+
72
+ try :
73
+ section = conf .getsection (opts ['section' ])
53
74
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' ]))
55
76
sys .exit (1 )
56
77
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
+
57
93
if __name__ == "__main__" :
58
94
main ()
0 commit comments