From ada7dd09c10865a67049a85ccd688167aefad3c2 Mon Sep 17 00:00:00 2001 From: jortel Date: Tue, 31 Aug 2010 15:25:01 +0000 Subject: Add option: 'prettyxml' default=0 to control output XML as pretty printed or not. --- suds/__init__.py | 2 +- suds/client.py | 23 ++++++++++++++--------- suds/options.py | 5 +++++ suds/sax/document.py | 24 +++++++++++++++++------- suds/sax/element.py | 23 +++++++++++++++++++++++ 5 files changed, 60 insertions(+), 17 deletions(-) diff --git a/suds/__init__.py b/suds/__init__.py index 896c91d..c6a35e6 100644 --- a/suds/__init__.py +++ b/suds/__init__.py @@ -27,7 +27,7 @@ import sys # __version__ = '0.4' -__build__="(beta) R690-20100824" +__build__="(beta) R692-20100831" # # Exceptions diff --git a/suds/client.py b/suds/client.py index d56cb99..5a74097 100644 --- a/suds/client.py +++ b/suds/client.py @@ -592,14 +592,14 @@ class SoapClient: 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", @@ -607,11 +607,11 @@ class SoapClient: 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} """ @@ -620,12 +620,17 @@ class SoapClient: 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() diff --git a/suds/options.py b/suds/options.py index b4876d3..86ea245 100644 --- a/suds/options.py +++ b/suds/options.py @@ -84,6 +84,10 @@ class Options(Skin): 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} @@ -111,6 +115,7 @@ class Options(Skin): 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), []), diff --git a/suds/sax/document.py b/suds/sax/document.py index c7129cb..5a004eb 100644 --- a/suds/sax/document.py +++ b/suds/sax/document.py @@ -27,6 +27,8 @@ log = getLogger(__name__) class Document(Element): """ simple document """ + + DECL = '' def __init__(self, root=None): Element.__init__(self, 'document') @@ -34,18 +36,26 @@ class Document(Element): 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 = '' - root = self.root() - if root is not None: - result += '\n' - result += root.str() - return unicode(result) + return self.str() \ No newline at end of file diff --git a/suds/sax/element.py b/suds/sax/element.py index 0a015f1..9dec1f9 100644 --- a/suds/sax/element.py +++ b/suds/sax/element.py @@ -767,6 +767,29 @@ class Element: result.append('' % 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('' % self.qname()) + result = ''.join(result) + return result def nsdeclarations(self): """ -- cgit v1.2.1