mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-11-24 03:00:15 +00:00
Merge branch 'develop'
This commit is contained in:
commit
4d87098590
6 changed files with 152 additions and 61 deletions
0
LICENSE
Normal file
0
LICENSE
Normal file
|
@ -1,6 +1,12 @@
|
||||||
from xml.etree import cElementTree as ET
|
from xml.etree import cElementTree as ET
|
||||||
import logging
|
import logging
|
||||||
import traceback
|
import traceback
|
||||||
|
import sys
|
||||||
|
|
||||||
|
if sys.version_info < (3,0):
|
||||||
|
from . import tostring26 as tostring
|
||||||
|
else:
|
||||||
|
from . import tostring
|
||||||
|
|
||||||
xmltester = type(ET.Element('xml'))
|
xmltester = type(ET.Element('xml'))
|
||||||
|
|
||||||
|
@ -23,7 +29,7 @@ class JID(object):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.jid
|
return self.jid
|
||||||
|
|
||||||
class ElementBase(object):
|
class ElementBase(tostring.ToString):
|
||||||
name = 'stanza'
|
name = 'stanza'
|
||||||
plugin_attrib = 'plugin'
|
plugin_attrib = 'plugin'
|
||||||
namespace = 'jabber:client'
|
namespace = 'jabber:client'
|
||||||
|
@ -333,63 +339,5 @@ class StanzaBase(ElementBase):
|
||||||
logging.error(traceback.format_tb(e))
|
logging.error(traceback.format_tb(e))
|
||||||
|
|
||||||
def send(self):
|
def send(self):
|
||||||
self.stream.sendRaw(str(self))
|
self.stream.sendRaw(self.__str__())
|
||||||
|
|
||||||
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("</%s>" % (itag, ))
|
|
||||||
if xml.tail:
|
|
||||||
newoutput.append(self.xmlesc(xml.tail))
|
|
||||||
elif xml.text:
|
|
||||||
newoutput.append(">%s</%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)
|
|
||||||
|
|
60
sleekxmpp/xmlstream/tostring.py
Normal file
60
sleekxmpp/xmlstream/tostring.py
Normal file
|
@ -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("</%s>" % (itag, ))
|
||||||
|
if xml.tail:
|
||||||
|
newoutput.append(self.xmlesc(xml.tail))
|
||||||
|
elif xml.text:
|
||||||
|
newoutput.append(">%s</%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)
|
65
sleekxmpp/xmlstream/tostring26.py
Normal file
65
sleekxmpp/xmlstream/tostring26.py
Normal file
|
@ -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"</%s>" % (itag, ))
|
||||||
|
if xml.tail:
|
||||||
|
newoutput.append(self.xmlesc(xml.tail))
|
||||||
|
elif xml.text:
|
||||||
|
newoutput.append(">%s</%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)
|
|
@ -10,7 +10,10 @@ class testoverall(unittest.TestCase):
|
||||||
"""Testing all modules by compiling them"""
|
"""Testing all modules by compiling them"""
|
||||||
import compileall
|
import compileall
|
||||||
import re
|
import re
|
||||||
|
if sys.version_info < (3,0):
|
||||||
self.failUnless(compileall.compile_dir('.' + os.sep + 'sleekxmpp', rx=re.compile('/[.]svn'), quiet=True))
|
self.failUnless(compileall.compile_dir('.' + os.sep + 'sleekxmpp', rx=re.compile('/[.]svn'), quiet=True))
|
||||||
|
else:
|
||||||
|
self.failUnless(compileall.compile_dir('.' + os.sep + 'sleekxmpp', rx=re.compile('/[.]svn|26.py'), quiet=True))
|
||||||
|
|
||||||
def testTabNanny(self):
|
def testTabNanny(self):
|
||||||
"""Invoking the tabnanny"""
|
"""Invoking the tabnanny"""
|
||||||
|
|
15
tests/test_presencestanzas.py
Normal file
15
tests/test_presencestanzas.py
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
class testpresencestanzas(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
import sleekxmpp.stanza.presence as p
|
||||||
|
self.p = p
|
||||||
|
|
||||||
|
def testPresenceShowRegression(self):
|
||||||
|
"Regression check presence['type'] = 'dnd' show value working"
|
||||||
|
p = self.p.Presence()
|
||||||
|
p['type'] = 'dnd'
|
||||||
|
self.failUnless(str(p) == "<presence><show>dnd</show></presence>")
|
||||||
|
|
||||||
|
suite = unittest.TestLoader().loadTestsFromTestCase(testpresencestanzas)
|
Loading…
Reference in a new issue