Replaced the ToString class with a tostring function.
The sleekxmpp.xmlstream.tostring and sleekxmpp.xmlstream.tostring26 packages have been merged to sleekxmpp.xmlstream.tostring. The __init__.py file will import the appropriate tostring function depending on the Python version. The setup.py file has been updated with the package changes. ElementBase is now a direct descendent of object and does not subclass ToString. Stanza objects now return their XML contents for __repr__.
This commit is contained in:
@@ -12,10 +12,7 @@ import weakref
|
||||
import copy
|
||||
from . jid import JID
|
||||
|
||||
if sys.version_info < (3,0):
|
||||
from . import tostring26 as tostring
|
||||
else:
|
||||
from . import tostring
|
||||
from sleekxmpp.xmlstream.tostring import tostring
|
||||
|
||||
xmltester = type(ET.Element('xml'))
|
||||
|
||||
@@ -29,7 +26,7 @@ def registerStanzaPlugin(stanza, plugin):
|
||||
stanza.plugin_tag_map[tag] = plugin
|
||||
|
||||
|
||||
class ElementBase(tostring.ToString):
|
||||
class ElementBase(object):
|
||||
name = 'stanza'
|
||||
plugin_attrib = 'plugin'
|
||||
namespace = 'jabber:client'
|
||||
@@ -70,20 +67,20 @@ class ElementBase(tostring.ToString):
|
||||
|
||||
def __bool__(self):
|
||||
return True
|
||||
|
||||
|
||||
def __next__(self):
|
||||
self.idx += 1
|
||||
if self.idx > len(self.iterables):
|
||||
self.idx = 0
|
||||
raise StopIteration
|
||||
return self.iterables[self.idx - 1]
|
||||
|
||||
|
||||
def next(self):
|
||||
return self.__next__()
|
||||
|
||||
def __len__(self):
|
||||
return len(self.iterables)
|
||||
|
||||
|
||||
def append(self, item):
|
||||
if not isinstance(item, ElementBase):
|
||||
if type(item) == xmltester:
|
||||
@@ -93,18 +90,18 @@ class ElementBase(tostring.ToString):
|
||||
self.xml.append(item.xml)
|
||||
self.iterables.append(item)
|
||||
return self
|
||||
|
||||
|
||||
def pop(self, idx=0):
|
||||
aff = self.iterables.pop(idx)
|
||||
self.xml.remove(aff.xml)
|
||||
return aff
|
||||
|
||||
|
||||
def get(self, key, defaultvalue=None):
|
||||
value = self[key]
|
||||
if value is None or value == '':
|
||||
return defaultvalue
|
||||
return value
|
||||
|
||||
|
||||
def keys(self):
|
||||
out = []
|
||||
out += [x for x in self.interfaces]
|
||||
@@ -112,7 +109,7 @@ class ElementBase(tostring.ToString):
|
||||
if self.iterables:
|
||||
out.append('substanzas')
|
||||
return tuple(out)
|
||||
|
||||
|
||||
def match(self, matchstring):
|
||||
if isinstance(matchstring, str):
|
||||
nodes = matchstring.split('/')
|
||||
@@ -136,13 +133,13 @@ class ElementBase(tostring.ToString):
|
||||
else:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def find(self, xpath): # for backwards compatiblity, expose elementtree interface
|
||||
return self.xml.find(xpath)
|
||||
|
||||
def findall(self, xpath):
|
||||
return self.xml.findall(xpath)
|
||||
|
||||
|
||||
def setup(self, xml=None):
|
||||
if self.xml is None:
|
||||
self.xml = xml
|
||||
@@ -162,11 +159,11 @@ class ElementBase(tostring.ToString):
|
||||
def enable(self, attrib):
|
||||
self.initPlugin(attrib)
|
||||
return self
|
||||
|
||||
|
||||
def initPlugin(self, attrib):
|
||||
if attrib not in self.plugins:
|
||||
self.plugins[attrib] = self.plugin_attrib_map[attrib](parent=self)
|
||||
|
||||
|
||||
def __getitem__(self, attrib):
|
||||
if attrib == 'substanzas':
|
||||
return self.iterables
|
||||
@@ -183,7 +180,7 @@ class ElementBase(tostring.ToString):
|
||||
return self.plugins[attrib]
|
||||
else:
|
||||
return ''
|
||||
|
||||
|
||||
def __setitem__(self, attrib, value):
|
||||
if attrib in self.interfaces:
|
||||
if value is not None:
|
||||
@@ -201,7 +198,7 @@ class ElementBase(tostring.ToString):
|
||||
self.initPlugin(attrib)
|
||||
self.plugins[attrib][attrib] = value
|
||||
return self
|
||||
|
||||
|
||||
def __delitem__(self, attrib):
|
||||
if attrib.lower() in self.interfaces:
|
||||
if hasattr(self, "del%s" % attrib.title()):
|
||||
@@ -215,7 +212,7 @@ class ElementBase(tostring.ToString):
|
||||
if attrib in self.plugins:
|
||||
del self.plugins[attrib]
|
||||
return self
|
||||
|
||||
|
||||
def __eq__(self, other):
|
||||
if not isinstance(other, ElementBase):
|
||||
return False
|
||||
@@ -224,20 +221,20 @@ class ElementBase(tostring.ToString):
|
||||
if key not in values or values[key] != other[key]:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _setAttr(self, name, value):
|
||||
if value is None or value == '':
|
||||
self.__delitem__(name)
|
||||
else:
|
||||
self.xml.attrib[name] = value
|
||||
|
||||
|
||||
def _delAttr(self, name):
|
||||
if name in self.xml.attrib:
|
||||
del self.xml.attrib[name]
|
||||
|
||||
|
||||
def _getAttr(self, name, default=''):
|
||||
return self.xml.attrib.get(name, default)
|
||||
|
||||
|
||||
def _getSubText(self, name):
|
||||
if '}' not in name:
|
||||
name = "{%s}%s" % (self.namespace, name)
|
||||
@@ -246,7 +243,7 @@ class ElementBase(tostring.ToString):
|
||||
return ''
|
||||
else:
|
||||
return stanza.text
|
||||
|
||||
|
||||
def _setSubText(self, name, attrib={}, text=None):
|
||||
if '}' not in name:
|
||||
name = "{%s}%s" % (self.namespace, name)
|
||||
@@ -258,14 +255,14 @@ class ElementBase(tostring.ToString):
|
||||
self.xml.append(stanza)
|
||||
stanza.text = text
|
||||
return stanza
|
||||
|
||||
|
||||
def _delSub(self, name):
|
||||
if '}' not in name:
|
||||
name = "{%s}%s" % (self.namespace, name)
|
||||
for child in self.xml.getchildren():
|
||||
if child.tag == name:
|
||||
self.xml.remove(child)
|
||||
|
||||
|
||||
def getStanzaValues(self):
|
||||
out = {}
|
||||
for interface in self.interfaces:
|
||||
@@ -279,7 +276,7 @@ class ElementBase(tostring.ToString):
|
||||
iterables[-1].update({'__childtag__': "{%s}%s" % (stanza.namespace, stanza.name)})
|
||||
out['substanzas'] = iterables
|
||||
return out
|
||||
|
||||
|
||||
def setStanzaValues(self, attrib):
|
||||
for interface in attrib:
|
||||
if interface == 'substanzas':
|
||||
@@ -298,14 +295,20 @@ class ElementBase(tostring.ToString):
|
||||
if interface in self.plugins:
|
||||
self.plugins[interface].setStanzaValues(attrib[interface])
|
||||
return self
|
||||
|
||||
|
||||
def appendxml(self, xml):
|
||||
self.xml.append(xml)
|
||||
return self
|
||||
|
||||
def __copy__(self):
|
||||
return self.__class__(xml=copy.deepcopy(self.xml), parent=self.parent)
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return tostring(self.xml, xmlns='', stanza_ns=self.namespace)
|
||||
|
||||
def __repr__(self):
|
||||
return self.__str__()
|
||||
|
||||
#def __del__(self): #prevents garbage collection of reference cycle
|
||||
# if self.parent is not None:
|
||||
# self.parent.xml.remove(self.xml)
|
||||
@@ -329,7 +332,7 @@ class StanzaBase(ElementBase):
|
||||
if sfrom is not None:
|
||||
self['from'] = sfrom
|
||||
self.tag = "{%s}%s" % (self.namespace, self.name)
|
||||
|
||||
|
||||
def setType(self, value):
|
||||
if value in self.types:
|
||||
self.xml.attrib['type'] = value
|
||||
@@ -337,22 +340,22 @@ class StanzaBase(ElementBase):
|
||||
|
||||
def getPayload(self):
|
||||
return self.xml.getchildren()
|
||||
|
||||
|
||||
def setPayload(self, value):
|
||||
self.xml.append(value)
|
||||
return self
|
||||
|
||||
|
||||
def delPayload(self):
|
||||
self.clear()
|
||||
return self
|
||||
|
||||
|
||||
def clear(self):
|
||||
for child in self.xml.getchildren():
|
||||
self.xml.remove(child)
|
||||
for plugin in list(self.plugins.keys()):
|
||||
del self.plugins[plugin]
|
||||
return self
|
||||
|
||||
|
||||
def reply(self):
|
||||
# if it's a component, use from
|
||||
if self.stream and hasattr(self.stream, "is_component") and self.stream.is_component:
|
||||
@@ -362,32 +365,34 @@ class StanzaBase(ElementBase):
|
||||
del self['from']
|
||||
self.clear()
|
||||
return self
|
||||
|
||||
|
||||
def error(self):
|
||||
self['type'] = 'error'
|
||||
return self
|
||||
|
||||
|
||||
def getTo(self):
|
||||
return JID(self._getAttr('to'))
|
||||
|
||||
|
||||
def setTo(self, value):
|
||||
return self._setAttr('to', str(value))
|
||||
|
||||
|
||||
def getFrom(self):
|
||||
return JID(self._getAttr('from'))
|
||||
|
||||
|
||||
def setFrom(self, value):
|
||||
return self._setAttr('from', str(value))
|
||||
|
||||
|
||||
def unhandled(self):
|
||||
pass
|
||||
|
||||
|
||||
def exception(self, e):
|
||||
logging.exception('Error handling {%s}%s stanza' % (self.namespace, self.name))
|
||||
|
||||
|
||||
def send(self):
|
||||
self.stream.sendRaw(self.__str__())
|
||||
|
||||
def __copy__(self):
|
||||
return self.__class__(xml=copy.deepcopy(self.xml), stream=self.stream)
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return tostring(self.xml, xmlns='', stanza_ns=self.namespace, stream=self.stream)
|
||||
|
||||
Reference in New Issue
Block a user