Code

New function write_xml
[nagixsc.git] / nagixsc / __init__.py
index c47c0ea64ae85accf780046295bd1048b1461834..03884e36b9f22df2a4cda5d51ede2c410382fe4d 100644 (file)
@@ -13,6 +13,7 @@ import socket
 import string
 import subprocess
 import sys
+import urllib2
 
 def debug(level, verb, string):
        if level <= verb:
@@ -254,7 +255,6 @@ def dict2out_checkresult(checks, xmltimestamp, opt_checkresultdir, opt_verb):
 
 def read_xml(options):
        if options.url != None:
-               import urllib2
 
                if options.httpuser and options.httppasswd:
                        passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
@@ -285,6 +285,28 @@ def read_xml_from_string(content):
        return libxml2.parseDoc(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
+
+       elif outfile == '-':
+               xmldoc.saveFormatFile('-', format=1)
+
+       else:
+               xmldoc.saveFile(outfile)
+
+
 ##############################################################################
 
 def xml_check_version(xmldoc):
@@ -446,7 +468,7 @@ def reset_future_timestamp(timestamp, now):
 
 ##############################################################################
 
-def encode_multipart(xmldoc, httpuser, httppasswd):
+def encode_multipart(xmldoc, httpuser=None, httppasswd=None):
        BOUNDARY = mimetools.choose_boundary()
        CRLF = '\r\n'
        L = []