Code

Split write_xml(), add write_xml_or_die()
authorSven Velt <sven@velt.de>
Thu, 23 Sep 2010 13:32:39 +0000 (15:32 +0200)
committerSven Velt <sven@velt.de>
Thu, 23 Sep 2010 13:32:39 +0000 (15:32 +0200)
Added "-q" to conf2xml and live2xml

Signed-off-by: Sven Velt <sven@velt.de>
nagixsc/__init__.py
nagixsc_conf2xml.py
nagixsc_live2xml.py

index 5bdde6815d5cfcbc99cebdeeebc0e72d044ecf1a..2a93b864f6315de3a0c7bbc53a506d861962c831 100644 (file)
@@ -288,23 +288,29 @@ def read_xml_from_string(content):
 def write_xml(xmldoc, outfile, httpuser=None, httppasswd=None):
        if outfile.startswith('http'):
                (headers, body) = encode_multipart(xmldoc, httpuser, httppasswd)
-
-               try:
-                       response = urllib2.urlopen(urllib2.Request(outfile, body, headers)).read()
-               except urllib2.HTTPError, error:
-                       print error
-                       sys.exit(11)
-               except urllib2.URLError, error:
-                       print error.reason[1]
-                       sys.exit(12)
-
-               print response
+               response = urllib2.urlopen(urllib2.Request(outfile, body, headers)).read()
+               return response
 
        elif outfile == '-':
                xmldoc.saveFormatFile('-', format=1)
+               return None
 
        else:
                xmldoc.saveFile(outfile)
+               return None
+
+
+def write_xml_or_die(xmldoc, outfile, httpuser=None, httppasswd=None):
+       try:
+               response = write_xml(xmldoc, outfile, httpuser=None, httppasswd=None)
+       except urllib2.HTTPError, error:
+               print error
+               sys.exit(11)
+       except urllib2.URLError, error:
+               print error.reason[1]
+               sys.exit(12)
+
+       return response
 
 
 ##############################################################################
index 508f0d0e8196e074940e4ea0c1e58240cfc50b72..153c910914b86da2b08849acfe15c7122578e1da 100755 (executable)
@@ -19,6 +19,7 @@ parser.add_option('-H', '', dest='host', help='Hostname/section to search for in
 parser.add_option('-D', '', dest='service', help='Service description to search for in config file (needs -H)')
 parser.add_option('-l', '', dest='httpuser', help='HTTP user name, if outfile is HTTP(S) URL')
 parser.add_option('-a', '', dest='httppasswd', help='HTTP password, if outfile is HTTP(S) URL')
+parser.add_option('-q', '', action='store_true', dest='quiet', help='Be quiet')
 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
 
 parser.set_defaults(conffile='nagixsc.conf')
@@ -52,5 +53,7 @@ checks = conf2dict(config, options.host, options.service)
 xmldoc = xml_from_dict(checks, options.encoding)
 
 # Output
-write_xml(xmldoc, options.outfile, options.httpuser, options.httppasswd)
+response = write_xml_or_die(xmldoc, options.outfile, options.httpuser, options.httppasswd)
+if response and not options.quiet:
+       print response
 
index 4702f95207361b548af9b0e1d3801d5ba97cd6ef..281c6757c36136ea750102e2a256be1133d71a6e 100755 (executable)
@@ -18,6 +18,7 @@ parser.add_option('-H', '', dest='host', help='Hostname/section to search for in
 parser.add_option('-D', '', dest='service', help='Service description to search for in config file (needs -H)')
 parser.add_option('-l', '', dest='httpuser', help='HTTP user name, if outfile is HTTP(S) URL')
 parser.add_option('-a', '', dest='httppasswd', help='HTTP password, if outfile is HTTP(S) URL')
+parser.add_option('-q', '', action='store_true', dest='quiet', help='Be quiet')
 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
 
 parser.set_defaults(socket=None)
@@ -53,5 +54,7 @@ checks = livestatus2dict(s_opts, options.host, options.service)
 xmldoc = xml_from_dict(checks, options.encoding)
 
 # Output
-write_xml(xmldoc, options.outfile, options.httpuser, options.httppasswd)
+response = write_xml_or_die(xmldoc, options.outfile, options.httpuser, options.httppasswd)
+if response and not options.quiet:
+       print response