From 395618d3d37c5c4092466f4c5ea6919925fda681 Mon Sep 17 00:00:00 2001 From: Nathan Fritz Date: Fri, 29 Jan 2010 02:04:15 -0800 Subject: fixed unicode problems in 2.6 --- sleekxmpp/xmlstream/stanzabase.py | 68 +++++---------------------------------- 1 file changed, 8 insertions(+), 60 deletions(-) (limited to 'sleekxmpp') diff --git a/sleekxmpp/xmlstream/stanzabase.py b/sleekxmpp/xmlstream/stanzabase.py index feb9b268..480e9602 100644 --- a/sleekxmpp/xmlstream/stanzabase.py +++ b/sleekxmpp/xmlstream/stanzabase.py @@ -1,6 +1,12 @@ from xml.etree import cElementTree as ET import logging import traceback +import sys + +if sys.version_info < (3,0): + from . import tostring26 as tostring +else: + from . import tostring xmltester = type(ET.Element('xml')) @@ -23,7 +29,7 @@ class JID(object): def __str__(self): return self.jid -class ElementBase(object): +class ElementBase(tostring.ToString): name = 'stanza' plugin_attrib = 'plugin' namespace = 'jabber:client' @@ -333,63 +339,5 @@ class StanzaBase(ElementBase): logging.error(traceback.format_tb(e)) def send(self): - self.stream.sendRaw(str(self)) - - def __str__(self, xml=None, xmlns='', stringbuffer=''): - if xml is None: - xml = self.xml - newoutput = [stringbuffer] - #TODO respect ET mapped namespaces - itag = xml.tag.split('}', 1)[-1] - if '}' in xml.tag: - ixmlns = xml.tag.split('}', 1)[0][1:] - else: - ixmlns = '' - nsbuffer = '' - if xmlns != ixmlns and ixmlns != '' and ixmlns != self.namespace: - if self.stream is not None and ixmlns in self.stream.namespace_map: - if self.stream.namespace_map[ixmlns] != '': - itag = "%s:%s" % (self.stream.namespace_map[ixmlns], itag) - else: - nsbuffer = """ xmlns="%s\"""" % ixmlns - if ixmlns not in ('', xmlns, self.namespace): - nsbuffer = """ xmlns="%s\"""" % ixmlns - newoutput.append("<%s" % itag) - newoutput.append(nsbuffer) - for attrib in xml.attrib: - if '{' not in attrib: - newoutput.append(""" %s="%s\"""" % (attrib, self.xmlesc(xml.attrib[attrib]))) - if len(xml) or xml.text or xml.tail: - newoutput.append(">") - if xml.text: - newoutput.append(self.xmlesc(xml.text)) - if len(xml): - for child in xml.getchildren(): - newoutput.append(self.__str__(child, ixmlns)) - newoutput.append("" % (itag, )) - if xml.tail: - newoutput.append(self.xmlesc(xml.tail)) - elif xml.text: - newoutput.append(">%s" % (self.xmlesc(xml.text), itag)) - else: - newoutput.append(" />") - return ''.join(newoutput) + self.stream.sendRaw(self.__str__()) - def xmlesc(self, text): - text = list(text) - cc = 0 - matches = ('&', '<', '"', '>', "'") - for c in text: - if c in matches: - if c == '&': - text[cc] = '&' - elif c == '<': - text[cc] = '<' - elif c == '>': - text[cc] = '>' - elif c == "'": - text[cc] = ''' - else: - text[cc] = '"' - cc += 1 - return ''.join(text) -- cgit v1.2.3 From 23b9930c44608e633437c8ec80ef687dd9c0a8b2 Mon Sep 17 00:00:00 2001 From: Nathan Fritz Date: Fri, 29 Jan 2010 02:11:45 -0800 Subject: added separate tostring files --- sleekxmpp/xmlstream/tostring.py | 60 ++++++++++++++++++++++++++++++++++++ sleekxmpp/xmlstream/tostring26.py | 65 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 sleekxmpp/xmlstream/tostring.py create mode 100644 sleekxmpp/xmlstream/tostring26.py (limited to 'sleekxmpp') diff --git a/sleekxmpp/xmlstream/tostring.py b/sleekxmpp/xmlstream/tostring.py new file mode 100644 index 00000000..6603cbb8 --- /dev/null +++ b/sleekxmpp/xmlstream/tostring.py @@ -0,0 +1,60 @@ + +class ToString(object): + def __str__(self, xml=None, xmlns='', stringbuffer=''): + if xml is None: + xml = self.xml + newoutput = [stringbuffer] + #TODO respect ET mapped namespaces + itag = xml.tag.split('}', 1)[-1] + if '}' in xml.tag: + ixmlns = xml.tag.split('}', 1)[0][1:] + else: + ixmlns = '' + nsbuffer = '' + if xmlns != ixmlns and ixmlns != '' and ixmlns != self.namespace: + if self.stream is not None and ixmlns in self.stream.namespace_map: + if self.stream.namespace_map[ixmlns] != '': + itag = "%s:%s" % (self.stream.namespace_map[ixmlns], itag) + else: + nsbuffer = """ xmlns="%s\"""" % ixmlns + if ixmlns not in ('', xmlns, self.namespace): + nsbuffer = """ xmlns="%s\"""" % ixmlns + newoutput.append("<%s" % itag) + newoutput.append(nsbuffer) + for attrib in xml.attrib: + if '{' not in attrib: + newoutput.append(""" %s="%s\"""" % (attrib, self.xmlesc(xml.attrib[attrib]))) + if len(xml) or xml.text or xml.tail: + newoutput.append(">") + if xml.text: + newoutput.append(self.xmlesc(xml.text)) + if len(xml): + for child in xml.getchildren(): + newoutput.append(self.__str__(child, ixmlns)) + newoutput.append("" % (itag, )) + if xml.tail: + newoutput.append(self.xmlesc(xml.tail)) + elif xml.text: + newoutput.append(">%s" % (self.xmlesc(xml.text), itag)) + else: + newoutput.append(" />") + return ''.join(newoutput) + + def xmlesc(self, text): + text = list(text) + cc = 0 + matches = ('&', '<', '"', '>', "'") + for c in text: + if c in matches: + if c == '&': + text[cc] = '&' + elif c == '<': + text[cc] = '<' + elif c == '>': + text[cc] = '>' + elif c == "'": + text[cc] = ''' + else: + text[cc] = '"' + cc += 1 + return ''.join(text) diff --git a/sleekxmpp/xmlstream/tostring26.py b/sleekxmpp/xmlstream/tostring26.py new file mode 100644 index 00000000..9711c300 --- /dev/null +++ b/sleekxmpp/xmlstream/tostring26.py @@ -0,0 +1,65 @@ +import types + +class ToString(object): + def __str__(self, xml=None, xmlns='', stringbuffer=''): + if xml is None: + xml = self.xml + newoutput = [stringbuffer] + #TODO respect ET mapped namespaces + itag = xml.tag.split('}', 1)[-1] + if '}' in xml.tag: + ixmlns = xml.tag.split('}', 1)[0][1:] + else: + ixmlns = '' + nsbuffer = '' + if xmlns != ixmlns and ixmlns != u'' and ixmlns != self.namespace: + if self.stream is not None and ixmlns in self.stream.namespace_map: + if self.stream.namespace_map[ixmlns] != u'': + itag = "%s:%s" % (self.stream.namespace_map[ixmlns], itag) + else: + nsbuffer = """ xmlns="%s\"""" % ixmlns + if ixmlns not in ('', xmlns, self.namespace): + nsbuffer = """ xmlns="%s\"""" % ixmlns + newoutput.append("<%s" % itag) + newoutput.append(nsbuffer) + for attrib in xml.attrib: + if '{' not in attrib: + newoutput.append(""" %s="%s\"""" % (attrib, self.xmlesc(xml.attrib[attrib]))) + if len(xml) or xml.text or xml.tail: + newoutput.append(u">") + if xml.text: + newoutput.append(self.xmlesc(xml.text)) + if len(xml): + for child in xml.getchildren(): + newoutput.append(self.__str__(child, ixmlns)) + newoutput.append(u"" % (itag, )) + if xml.tail: + newoutput.append(self.xmlesc(xml.tail)) + elif xml.text: + newoutput.append(">%s" % (self.xmlesc(xml.text), itag)) + else: + newoutput.append(" />") + return u''.join(newoutput) + + def xmlesc(self, text): + if type(text) != types.UnicodeType: + text = list(unicode(text, 'utf-8', 'ignore')) + else: + text = list(text) + + cc = 0 + matches = (u'&', u'<', u'"', u'>', u"'") + for c in text: + if c in matches: + if c == u'&': + text[cc] = u'&' + elif c == u'<': + text[cc] = u'<' + elif c == u'>': + text[cc] = u'>' + elif c == u"'": + text[cc] = u''' + else: + text[cc] = u'"' + cc += 1 + return ''.join(text) -- cgit v1.2.3