Skip to content

Commit

Permalink
Split write_xml(), add write_xml_or_die()
Browse files Browse the repository at this point in the history
Added "-q" to conf2xml and live2xml

Signed-off-by: Sven Velt <sven@velt.de>
  • Loading branch information
wAmpIre committed Sep 23, 2010
1 parent 812c635 commit 8ed4dff
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
28 changes: 17 additions & 11 deletions nagixsc/__init__.py
Expand Up @@ -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


##############################################################################
Expand Down
5 changes: 4 additions & 1 deletion nagixsc_conf2xml.py
Expand Up @@ -19,6 +19,7 @@
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')
Expand Down Expand Up @@ -52,5 +53,7 @@
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

5 changes: 4 additions & 1 deletion nagixsc_live2xml.py
Expand Up @@ -18,6 +18,7 @@
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)
Expand Down Expand Up @@ -53,5 +54,7 @@
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

0 comments on commit 8ed4dff

Please sign in to comment.