Commits

Anonymous committed 87dc664

Add option: 'prettyxml' default=0 to control output XML as pretty printed or not.

Comments (0)

Files changed (5)

 #
 
 __version__ = '0.4'
-__build__="(beta) R690-20100824"
+__build__="(beta) R692-20100831"
 
 #
 # Exceptions
         timer.start()
         result = None
         binding = self.method.binding.input
-        msg = binding.get_message(self.method, args, kwargs)
+        soapenv = binding.get_message(self.method, args, kwargs)
         timer.stop()
         metrics.log.debug(
                 "message for '%s' created: %s",
                 self.method.name,
                 timer)
         timer.start()
-        result = self.send(msg)
+        result = self.send(soapenv)
         timer.stop()
         metrics.log.debug(
                 "method '%s' invoked: %s",
                 timer)
         return result
     
-    def send(self, msg):
+    def send(self, soapenv):
         """
         Send soap message.
-        @param msg: A soap message to send.
-        @type msg: basestring
+        @param soapenv: A soap envelope to send.
+        @type soapenv: L{Document}
         @return: The reply to the sent message.
         @rtype: I{builtin} or I{subclass of} L{Object}
         """
         binding = self.method.binding.input
         transport = self.options.transport
         retxml = self.options.retxml
-        log.debug('sending to (%s)\nmessage:\n%s', location, msg)
+        prettyxml = self.options.prettyxml
+        log.debug('sending to (%s)\nmessage:\n%s', location, soapenv)
         try:
-            self.last_sent(Document(msg))
+            self.last_sent(soapenv)
             plugins = PluginContainer(self.options.plugins)
-            plugins.message.marshalled(envelope=msg.root())
-            soapenv = str(msg)
+            plugins.message.marshalled(envelope=soapenv.root())
+            if prettyxml:
+                soapenv = soapenv.str()
+            else:
+                soapenv = soapenv.plain()
+            soapenv = soapenv.encode('utf-8')
             plugins.message.sending(envelope=soapenv)
             request = Request(location, soapenv)
             request.headers = self.headers()
             of the python object graph.
                 - type: I{bool}
                 - default: False
+        - B{prettyxml} - Flag that causes I{pretty} xml to be rendered when generating
+            the outbound soap envelope.
+                - type: I{bool}
+                - default: False
         - B{autoblend} - Flag that ensures that the schema(s) defined within the
             WSDL import each other.
                 - type: I{bool}
             Definition('xstq', bool, True),
             Definition('prefixes', bool, True),
             Definition('retxml', bool, False),
+            Definition('prettyxml', bool, False),
             Definition('autoblend', bool, False),
             Definition('cachingpolicy', int, 0),
             Definition('plugins', (list, tuple), []),

suds/sax/document.py

 
 class Document(Element):
     """ simple document """
+    
+    DECL = '<?xml version="1.0" encoding="UTF-8"?>'
 
     def __init__(self, root=None):
         Element.__init__(self, 'document')
             self.append(root)
         
     def root(self):
-        if len(self.children) > 0:
+        if len(self.children):
             return self.children[0]
         else:
             return None
         
+    def str(self):
+        s = []
+        s.append(self.DECL)
+        s.append('\n')
+        s.append(self.root().str())
+        return ''.join(s)
+    
+    def plain(self):
+        s = []
+        s.append(self.DECL)
+        s.append(self.root().plain())
+        return ''.join(s)
+
     def __str__(self):
         return unicode(self).encode('utf-8')
     
     def __unicode__(self):
-        result = '<?xml version="1.0" encoding="UTF-8"?>'
-        root = self.root()
-        if root is not None:
-            result += '\n'
-            result += root.str()
-        return unicode(result)
+        return self.str()

suds/sax/element.py

         result.append('</%s>' % self.qname())
         result = ''.join(result)
         return result
+    
+    def plain(self):
+        """
+        Get a string representation of this XML fragment.
+        @return: A I{plain} string.
+        @rtype: basestring
+        """
+        result = []
+        result.append('<%s' % self.qname())
+        result.append(self.nsdeclarations())
+        for a in [unicode(a) for a in self.attributes]:
+            result.append(' %s' % a)
+        if self.isempty():
+            result.append('/>')
+            return ''.join(result)
+        result.append('>')
+        if self.hasText():
+            result.append(self.text.escape())
+        for c in self.children:
+            result.append(c.plain())
+        result.append('</%s>' % self.qname())
+        result = ''.join(result)
+        return result
 
     def nsdeclarations(self):
         """