From 6c57bb055346384760129c25ad5402913982eaac Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Mon, 24 Sep 2012 20:59:51 -0700 Subject: Simplify stringifying XML --- sleekxmpp/xmlstream/stanzabase.py | 11 ++++------- sleekxmpp/xmlstream/tostring.py | 17 ++++++++--------- sleekxmpp/xmlstream/xmlstream.py | 4 +++- 3 files changed, 15 insertions(+), 17 deletions(-) (limited to 'sleekxmpp/xmlstream') diff --git a/sleekxmpp/xmlstream/stanzabase.py b/sleekxmpp/xmlstream/stanzabase.py index abe0abdc..233e91f6 100644 --- a/sleekxmpp/xmlstream/stanzabase.py +++ b/sleekxmpp/xmlstream/stanzabase.py @@ -1400,10 +1400,8 @@ class ElementBase(object): :param bool top_level_ns: Display the top-most namespace. Defaults to True. """ - stanza_ns = '' if top_level_ns else self.namespace return tostring(self.xml, xmlns='', - stanza_ns=stanza_ns, - top_level=not top_level_ns) + top_level=True) def __repr__(self): """Use the stanza's serialized XML as its representation.""" @@ -1592,11 +1590,10 @@ class StanzaBase(ElementBase): :param bool top_level_ns: Display the top-most namespace. Defaults to ``False``. """ - stanza_ns = '' if top_level_ns else self.namespace - return tostring(self.xml, xmlns='', - stanza_ns=stanza_ns, + xmlns = self.stream.default_ns if self.stream else '' + return tostring(self.xml, xmlns=xmlns, stream=self.stream, - top_level=not top_level_ns) + top_level=(self.stream is None)) #: A JSON/dictionary version of the XML content exposed through diff --git a/sleekxmpp/xmlstream/tostring.py b/sleekxmpp/xmlstream/tostring.py index f22e7770..0b73d8dc 100644 --- a/sleekxmpp/xmlstream/tostring.py +++ b/sleekxmpp/xmlstream/tostring.py @@ -24,19 +24,18 @@ if sys.version_info < (3, 0): XML_NS = 'http://www.w3.org/XML/1998/namespace' -def tostring(xml=None, xmlns='', stanza_ns='', stream=None, +def tostring(xml=None, xmlns='', stream=None, outbuffer='', top_level=False, open_only=False): """Serialize an XML object to a Unicode string. - If namespaces are provided using ``xmlns`` or ``stanza_ns``, then - elements that use those namespaces will not include the xmlns attribute - in the output. + If an outer xmlns is provided using ``xmlns``, then the current element's + namespace will not be included if it matches the outer namespace. An + exception is made for elements that have an attached stream, and appear + at the stream root. :param XML xml: The XML object to serialize. :param string xmlns: Optional namespace of an element wrapping the XML object. - :param string stanza_ns: The namespace of the stanza object that contains - the XML object. :param stream: The XML stream that generated the XML object. :param string outbuffer: Optional buffer for storing serializations during recursive calls. @@ -71,8 +70,8 @@ def tostring(xml=None, xmlns='', stanza_ns='', stream=None, # Output the tag name and derived namespace of the element. namespace = '' - if top_level and tag_xmlns not in ['', default_ns, stream_ns] or \ - tag_xmlns not in ['', xmlns, stanza_ns, stream_ns]: + if top_level and tag_xmlns not in [default_ns, xmlns, stream_ns] \ + or not top_level and tag_xmlns != xmlns: namespace = ' xmlns="%s"' % tag_xmlns if stream and tag_xmlns in stream.namespace_map: mapped_namespace = stream.namespace_map[tag_xmlns] @@ -110,7 +109,7 @@ def tostring(xml=None, xmlns='', stanza_ns='', stream=None, output.append(escape(xml.text, use_cdata)) if len(xml): for child in xml: - output.append(tostring(child, tag_xmlns, stanza_ns, stream)) + output.append(tostring(child, tag_xmlns, stream)) output.append("" % tag_name) elif xml.text: # If we only have text content. diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py index a58b63d4..dff32461 100644 --- a/sleekxmpp/xmlstream/xmlstream.py +++ b/sleekxmpp/xmlstream/xmlstream.py @@ -1244,7 +1244,9 @@ class XMLStream(object): data = filter(data) if data is None: return - str_data = str(data) + str_data = tostring(data.xml, xmlns=self.default_ns, + stream=self, + top_level=True) self.send_raw(str_data, now) else: self.send_raw(data, now) -- cgit v1.2.3 From a4abdf9fa6bf3aa8f1394fb5aa57632211a4705d Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Mon, 24 Sep 2012 21:00:23 -0700 Subject: Fix deleting non-existent stanza plugins. --- sleekxmpp/xmlstream/stanzabase.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'sleekxmpp/xmlstream') diff --git a/sleekxmpp/xmlstream/stanzabase.py b/sleekxmpp/xmlstream/stanzabase.py index 233e91f6..a75d7079 100644 --- a/sleekxmpp/xmlstream/stanzabase.py +++ b/sleekxmpp/xmlstream/stanzabase.py @@ -488,7 +488,7 @@ class ElementBase(object): """ return self.init_plugin(attrib, lang) - def _get_plugin(self, name, lang=None): + def _get_plugin(self, name, lang=None, check=False): if lang is None: lang = self.get_lang() @@ -501,12 +501,12 @@ class ElementBase(object): if (name, None) in self.plugins: return self.plugins[(name, None)] else: - return self.init_plugin(name, lang) + return None if check else self.init_plugin(name, lang) else: if (name, lang) in self.plugins: return self.plugins[(name, lang)] else: - return self.init_plugin(name, lang) + return None if check else self.init_plugin(name, lang) def init_plugin(self, attrib, lang=None, existing_xml=None, reuse=True): """Enable and initialize a stanza plugin. @@ -862,7 +862,7 @@ class ElementBase(object): else: self._del_attr(attrib) elif attrib in self.plugin_attrib_map: - plugin = self._get_plugin(attrib, lang) + plugin = self._get_plugin(attrib, lang, check=True) if not plugin: return self if plugin.is_extension: -- cgit v1.2.3 From 671f680bb39f366ad13bf937c7b611f667343314 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Tue, 25 Sep 2012 02:34:51 -0700 Subject: Add support for XEP-0280 Message Carbons --- sleekxmpp/xmlstream/stanzabase.py | 7 ------- 1 file changed, 7 deletions(-) (limited to 'sleekxmpp/xmlstream') diff --git a/sleekxmpp/xmlstream/stanzabase.py b/sleekxmpp/xmlstream/stanzabase.py index a75d7079..4f58953b 100644 --- a/sleekxmpp/xmlstream/stanzabase.py +++ b/sleekxmpp/xmlstream/stanzabase.py @@ -525,13 +525,6 @@ class ElementBase(object): if reuse and (attrib, lang) in self.plugins: return self.plugins[(attrib, lang)] - if existing_xml is None: - existing_xml = self.xml.find(plugin_class.tag_name()) - - if existing_xml is not None: - if existing_xml.attrib.get('{%s}lang' % XML_NS, default_lang) != lang: - existing_xml = None - plugin = plugin_class(parent=self, xml=existing_xml) if plugin.is_extension: -- cgit v1.2.3