mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-11-30 19:19:55 +00:00
Adjust first level indenting in ElementBase to prepare for cleanup.
This commit is contained in:
parent
b0fb205c16
commit
747001d33c
1 changed files with 250 additions and 250 deletions
|
@ -34,291 +34,291 @@ def registerStanzaPlugin(stanza, plugin):
|
||||||
|
|
||||||
|
|
||||||
class ElementBase(object):
|
class ElementBase(object):
|
||||||
name = 'stanza'
|
name = 'stanza'
|
||||||
plugin_attrib = 'plugin'
|
plugin_attrib = 'plugin'
|
||||||
namespace = 'jabber:client'
|
namespace = 'jabber:client'
|
||||||
interfaces = set(('type', 'to', 'from', 'id', 'payload'))
|
interfaces = set(('type', 'to', 'from', 'id', 'payload'))
|
||||||
types = set(('get', 'set', 'error', None, 'unavailable', 'normal', 'chat'))
|
types = set(('get', 'set', 'error', None, 'unavailable', 'normal', 'chat'))
|
||||||
sub_interfaces = tuple()
|
sub_interfaces = tuple()
|
||||||
plugin_attrib_map = {}
|
plugin_attrib_map = {}
|
||||||
plugin_tag_map = {}
|
plugin_tag_map = {}
|
||||||
subitem = None
|
subitem = None
|
||||||
|
|
||||||
def __init__(self, xml=None, parent=None):
|
def __init__(self, xml=None, parent=None):
|
||||||
if parent is None:
|
if parent is None:
|
||||||
self.parent = None
|
self.parent = None
|
||||||
else:
|
else:
|
||||||
self.parent = weakref.ref(parent)
|
self.parent = weakref.ref(parent)
|
||||||
self.xml = xml
|
self.xml = xml
|
||||||
self.plugins = {}
|
self.plugins = {}
|
||||||
self.iterables = []
|
self.iterables = []
|
||||||
self.idx = 0
|
self.idx = 0
|
||||||
if not self.setup(xml):
|
if not self.setup(xml):
|
||||||
for child in self.xml.getchildren():
|
for child in self.xml.getchildren():
|
||||||
if child.tag in self.plugin_tag_map:
|
if child.tag in self.plugin_tag_map:
|
||||||
self.plugins[self.plugin_tag_map[child.tag].plugin_attrib] = self.plugin_tag_map[child.tag](xml=child, parent=self)
|
self.plugins[self.plugin_tag_map[child.tag].plugin_attrib] = self.plugin_tag_map[child.tag](xml=child, parent=self)
|
||||||
if self.subitem is not None:
|
if self.subitem is not None:
|
||||||
for sub in self.subitem:
|
for sub in self.subitem:
|
||||||
if child.tag == "{%s}%s" % (sub.namespace, sub.name):
|
if child.tag == "{%s}%s" % (sub.namespace, sub.name):
|
||||||
self.iterables.append(sub(xml=child, parent=self))
|
self.iterables.append(sub(xml=child, parent=self))
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def attrib(self): #backwards compatibility
|
def attrib(self): #backwards compatibility
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
self.idx = 0
|
self.idx = 0
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __bool__(self):
|
def __bool__(self):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def __next__(self):
|
def __next__(self):
|
||||||
self.idx += 1
|
self.idx += 1
|
||||||
if self.idx > len(self.iterables):
|
if self.idx > len(self.iterables):
|
||||||
self.idx = 0
|
self.idx = 0
|
||||||
raise StopIteration
|
raise StopIteration
|
||||||
return self.iterables[self.idx - 1]
|
return self.iterables[self.idx - 1]
|
||||||
|
|
||||||
def next(self):
|
def next(self):
|
||||||
return self.__next__()
|
return self.__next__()
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self.iterables)
|
return len(self.iterables)
|
||||||
|
|
||||||
def append(self, item):
|
def append(self, item):
|
||||||
if not isinstance(item, ElementBase):
|
if not isinstance(item, ElementBase):
|
||||||
if type(item) == XML_TYPE:
|
if type(item) == XML_TYPE:
|
||||||
return self.appendxml(item)
|
return self.appendxml(item)
|
||||||
else:
|
else:
|
||||||
raise TypeError
|
raise TypeError
|
||||||
self.xml.append(item.xml)
|
self.xml.append(item.xml)
|
||||||
self.iterables.append(item)
|
self.iterables.append(item)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def pop(self, idx=0):
|
def pop(self, idx=0):
|
||||||
aff = self.iterables.pop(idx)
|
aff = self.iterables.pop(idx)
|
||||||
self.xml.remove(aff.xml)
|
self.xml.remove(aff.xml)
|
||||||
return aff
|
return aff
|
||||||
|
|
||||||
def get(self, key, defaultvalue=None):
|
def get(self, key, defaultvalue=None):
|
||||||
value = self[key]
|
value = self[key]
|
||||||
if value is None or value == '':
|
if value is None or value == '':
|
||||||
return defaultvalue
|
return defaultvalue
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def keys(self):
|
def keys(self):
|
||||||
out = []
|
out = []
|
||||||
out += [x for x in self.interfaces]
|
out += [x for x in self.interfaces]
|
||||||
out += [x for x in self.plugins]
|
out += [x for x in self.plugins]
|
||||||
if self.iterables:
|
if self.iterables:
|
||||||
out.append('substanzas')
|
out.append('substanzas')
|
||||||
return tuple(out)
|
return tuple(out)
|
||||||
|
|
||||||
def match(self, matchstring):
|
def match(self, matchstring):
|
||||||
if isinstance(matchstring, str):
|
if isinstance(matchstring, str):
|
||||||
nodes = matchstring.split('/')
|
nodes = matchstring.split('/')
|
||||||
else:
|
else:
|
||||||
nodes = matchstring
|
nodes = matchstring
|
||||||
tagargs = nodes[0].split('@')
|
tagargs = nodes[0].split('@')
|
||||||
if tagargs[0] not in (self.plugins, self.plugin_attrib): return False
|
if tagargs[0] not in (self.plugins, self.plugin_attrib): return False
|
||||||
founditerable = False
|
founditerable = False
|
||||||
for iterable in self.iterables:
|
for iterable in self.iterables:
|
||||||
if nodes[1:] == []:
|
if nodes[1:] == []:
|
||||||
break
|
break
|
||||||
founditerable = iterable.match(nodes[1:])
|
founditerable = iterable.match(nodes[1:])
|
||||||
if founditerable: break;
|
if founditerable: break;
|
||||||
for evals in tagargs[1:]:
|
for evals in tagargs[1:]:
|
||||||
x,y = evals.split('=')
|
x,y = evals.split('=')
|
||||||
if self[x] != y: return False
|
if self[x] != y: return False
|
||||||
if not founditerable and len(nodes) > 1:
|
if not founditerable and len(nodes) > 1:
|
||||||
next = nodes[1].split('@')[0]
|
next = nodes[1].split('@')[0]
|
||||||
if next in self.plugins:
|
if next in self.plugins:
|
||||||
return self.plugins[next].match(nodes[1:])
|
return self.plugins[next].match(nodes[1:])
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def find(self, xpath): # for backwards compatiblity, expose elementtree interface
|
def find(self, xpath): # for backwards compatiblity, expose elementtree interface
|
||||||
return self.xml.find(xpath)
|
return self.xml.find(xpath)
|
||||||
|
|
||||||
def findall(self, xpath):
|
def findall(self, xpath):
|
||||||
return self.xml.findall(xpath)
|
return self.xml.findall(xpath)
|
||||||
|
|
||||||
def setup(self, xml=None):
|
def setup(self, xml=None):
|
||||||
if self.xml is None:
|
if self.xml is None:
|
||||||
self.xml = xml
|
self.xml = xml
|
||||||
if self.xml is None:
|
if self.xml is None:
|
||||||
for ename in self.name.split('/'):
|
for ename in self.name.split('/'):
|
||||||
new = ET.Element("{%(namespace)s}%(name)s" % {'name': self.name, 'namespace': self.namespace})
|
new = ET.Element("{%(namespace)s}%(name)s" % {'name': self.name, 'namespace': self.namespace})
|
||||||
if self.xml is None:
|
if self.xml is None:
|
||||||
self.xml = new
|
self.xml = new
|
||||||
else:
|
else:
|
||||||
self.xml.append(new)
|
self.xml.append(new)
|
||||||
if self.parent is not None:
|
if self.parent is not None:
|
||||||
self.parent().xml.append(self.xml)
|
self.parent().xml.append(self.xml)
|
||||||
return True #had to generate XML
|
return True #had to generate XML
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def enable(self, attrib):
|
def enable(self, attrib):
|
||||||
self.initPlugin(attrib)
|
self.initPlugin(attrib)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def initPlugin(self, attrib):
|
def initPlugin(self, attrib):
|
||||||
if attrib not in self.plugins:
|
if attrib not in self.plugins:
|
||||||
self.plugins[attrib] = self.plugin_attrib_map[attrib](parent=self)
|
self.plugins[attrib] = self.plugin_attrib_map[attrib](parent=self)
|
||||||
|
|
||||||
def __getitem__(self, attrib):
|
def __getitem__(self, attrib):
|
||||||
if attrib == 'substanzas':
|
if attrib == 'substanzas':
|
||||||
return self.iterables
|
return self.iterables
|
||||||
elif attrib in self.interfaces:
|
elif attrib in self.interfaces:
|
||||||
if hasattr(self, "get%s" % attrib.title()):
|
if hasattr(self, "get%s" % attrib.title()):
|
||||||
return getattr(self, "get%s" % attrib.title())()
|
return getattr(self, "get%s" % attrib.title())()
|
||||||
else:
|
else:
|
||||||
if attrib in self.sub_interfaces:
|
if attrib in self.sub_interfaces:
|
||||||
return self._getSubText(attrib)
|
return self._getSubText(attrib)
|
||||||
else:
|
else:
|
||||||
return self._getAttr(attrib)
|
return self._getAttr(attrib)
|
||||||
elif attrib in self.plugin_attrib_map:
|
elif attrib in self.plugin_attrib_map:
|
||||||
if attrib not in self.plugins: self.initPlugin(attrib)
|
if attrib not in self.plugins: self.initPlugin(attrib)
|
||||||
return self.plugins[attrib]
|
return self.plugins[attrib]
|
||||||
else:
|
else:
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
def __setitem__(self, attrib, value):
|
def __setitem__(self, attrib, value):
|
||||||
if attrib in self.interfaces:
|
if attrib in self.interfaces:
|
||||||
if value is not None:
|
if value is not None:
|
||||||
if hasattr(self, "set%s" % attrib.title()):
|
if hasattr(self, "set%s" % attrib.title()):
|
||||||
getattr(self, "set%s" % attrib.title())(value,)
|
getattr(self, "set%s" % attrib.title())(value,)
|
||||||
else:
|
else:
|
||||||
if attrib in self.sub_interfaces:
|
if attrib in self.sub_interfaces:
|
||||||
return self._setSubText(attrib, text=value)
|
return self._setSubText(attrib, text=value)
|
||||||
else:
|
else:
|
||||||
self._setAttr(attrib, value)
|
self._setAttr(attrib, value)
|
||||||
else:
|
else:
|
||||||
self.__delitem__(attrib)
|
self.__delitem__(attrib)
|
||||||
elif attrib in self.plugin_attrib_map:
|
elif attrib in self.plugin_attrib_map:
|
||||||
if attrib not in self.plugins: self.initPlugin(attrib)
|
if attrib not in self.plugins: self.initPlugin(attrib)
|
||||||
self.initPlugin(attrib)
|
self.initPlugin(attrib)
|
||||||
self.plugins[attrib][attrib] = value
|
self.plugins[attrib][attrib] = value
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __delitem__(self, attrib):
|
def __delitem__(self, attrib):
|
||||||
if attrib.lower() in self.interfaces:
|
if attrib.lower() in self.interfaces:
|
||||||
if hasattr(self, "del%s" % attrib.title()):
|
if hasattr(self, "del%s" % attrib.title()):
|
||||||
getattr(self, "del%s" % attrib.title())()
|
getattr(self, "del%s" % attrib.title())()
|
||||||
else:
|
else:
|
||||||
if attrib in self.sub_interfaces:
|
if attrib in self.sub_interfaces:
|
||||||
return self._delSub(attrib)
|
return self._delSub(attrib)
|
||||||
else:
|
else:
|
||||||
self._delAttr(attrib)
|
self._delAttr(attrib)
|
||||||
elif attrib in self.plugin_attrib_map:
|
elif attrib in self.plugin_attrib_map:
|
||||||
if attrib in self.plugins:
|
if attrib in self.plugins:
|
||||||
del self.plugins[attrib]
|
del self.plugins[attrib]
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if not isinstance(other, ElementBase):
|
if not isinstance(other, ElementBase):
|
||||||
return False
|
return False
|
||||||
values = self.getStanzaValues()
|
values = self.getStanzaValues()
|
||||||
for key in other:
|
for key in other:
|
||||||
if key not in values or values[key] != other[key]:
|
if key not in values or values[key] != other[key]:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _setAttr(self, name, value):
|
def _setAttr(self, name, value):
|
||||||
if value is None or value == '':
|
if value is None or value == '':
|
||||||
self.__delitem__(name)
|
self.__delitem__(name)
|
||||||
else:
|
else:
|
||||||
self.xml.attrib[name] = value
|
self.xml.attrib[name] = value
|
||||||
|
|
||||||
def _delAttr(self, name):
|
def _delAttr(self, name):
|
||||||
if name in self.xml.attrib:
|
if name in self.xml.attrib:
|
||||||
del self.xml.attrib[name]
|
del self.xml.attrib[name]
|
||||||
|
|
||||||
def _getAttr(self, name, default=''):
|
def _getAttr(self, name, default=''):
|
||||||
return self.xml.attrib.get(name, default)
|
return self.xml.attrib.get(name, default)
|
||||||
|
|
||||||
def _getSubText(self, name):
|
def _getSubText(self, name):
|
||||||
if '}' not in name:
|
if '}' not in name:
|
||||||
name = "{%s}%s" % (self.namespace, name)
|
name = "{%s}%s" % (self.namespace, name)
|
||||||
stanza = self.xml.find(name)
|
stanza = self.xml.find(name)
|
||||||
if stanza is None or stanza.text is None:
|
if stanza is None or stanza.text is None:
|
||||||
return ''
|
return ''
|
||||||
else:
|
else:
|
||||||
return stanza.text
|
return stanza.text
|
||||||
|
|
||||||
def _setSubText(self, name, attrib={}, text=None):
|
def _setSubText(self, name, attrib={}, text=None):
|
||||||
if '}' not in name:
|
if '}' not in name:
|
||||||
name = "{%s}%s" % (self.namespace, name)
|
name = "{%s}%s" % (self.namespace, name)
|
||||||
if text is None or text == '':
|
if text is None or text == '':
|
||||||
return self.__delitem__(name)
|
return self.__delitem__(name)
|
||||||
stanza = self.xml.find(name)
|
stanza = self.xml.find(name)
|
||||||
if stanza is None:
|
if stanza is None:
|
||||||
stanza = ET.Element(name)
|
stanza = ET.Element(name)
|
||||||
self.xml.append(stanza)
|
self.xml.append(stanza)
|
||||||
stanza.text = text
|
stanza.text = text
|
||||||
return stanza
|
return stanza
|
||||||
|
|
||||||
def _delSub(self, name):
|
def _delSub(self, name):
|
||||||
if '}' not in name:
|
if '}' not in name:
|
||||||
name = "{%s}%s" % (self.namespace, name)
|
name = "{%s}%s" % (self.namespace, name)
|
||||||
for child in self.xml.getchildren():
|
for child in self.xml.getchildren():
|
||||||
if child.tag == name:
|
if child.tag == name:
|
||||||
self.xml.remove(child)
|
self.xml.remove(child)
|
||||||
|
|
||||||
def getStanzaValues(self):
|
def getStanzaValues(self):
|
||||||
out = {}
|
out = {}
|
||||||
for interface in self.interfaces:
|
for interface in self.interfaces:
|
||||||
out[interface] = self[interface]
|
out[interface] = self[interface]
|
||||||
for pluginkey in self.plugins:
|
for pluginkey in self.plugins:
|
||||||
out[pluginkey] = self.plugins[pluginkey].getStanzaValues()
|
out[pluginkey] = self.plugins[pluginkey].getStanzaValues()
|
||||||
if self.iterables:
|
if self.iterables:
|
||||||
iterables = []
|
iterables = []
|
||||||
for stanza in self.iterables:
|
for stanza in self.iterables:
|
||||||
iterables.append(stanza.getStanzaValues())
|
iterables.append(stanza.getStanzaValues())
|
||||||
iterables[-1].update({'__childtag__': "{%s}%s" % (stanza.namespace, stanza.name)})
|
iterables[-1].update({'__childtag__': "{%s}%s" % (stanza.namespace, stanza.name)})
|
||||||
out['substanzas'] = iterables
|
out['substanzas'] = iterables
|
||||||
return out
|
return out
|
||||||
|
|
||||||
def setStanzaValues(self, attrib):
|
def setStanzaValues(self, attrib):
|
||||||
for interface in attrib:
|
for interface in attrib:
|
||||||
if interface == 'substanzas':
|
if interface == 'substanzas':
|
||||||
for subdict in attrib['substanzas']:
|
for subdict in attrib['substanzas']:
|
||||||
if '__childtag__' in subdict:
|
if '__childtag__' in subdict:
|
||||||
for subclass in self.subitem:
|
for subclass in self.subitem:
|
||||||
if subdict['__childtag__'] == "{%s}%s" % (subclass.namespace, subclass.name):
|
if subdict['__childtag__'] == "{%s}%s" % (subclass.namespace, subclass.name):
|
||||||
sub = subclass(parent=self)
|
sub = subclass(parent=self)
|
||||||
sub.setStanzaValues(subdict)
|
sub.setStanzaValues(subdict)
|
||||||
self.iterables.append(sub)
|
self.iterables.append(sub)
|
||||||
break
|
break
|
||||||
elif interface in self.interfaces:
|
elif interface in self.interfaces:
|
||||||
self[interface] = attrib[interface]
|
self[interface] = attrib[interface]
|
||||||
elif interface in self.plugin_attrib_map and interface not in self.plugins:
|
elif interface in self.plugin_attrib_map and interface not in self.plugins:
|
||||||
self.initPlugin(interface)
|
self.initPlugin(interface)
|
||||||
if interface in self.plugins:
|
if interface in self.plugins:
|
||||||
self.plugins[interface].setStanzaValues(attrib[interface])
|
self.plugins[interface].setStanzaValues(attrib[interface])
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def appendxml(self, xml):
|
def appendxml(self, xml):
|
||||||
self.xml.append(xml)
|
self.xml.append(xml)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __copy__(self):
|
def __copy__(self):
|
||||||
return self.__class__(xml=copy.deepcopy(self.xml), parent=self.parent)
|
return self.__class__(xml=copy.deepcopy(self.xml), parent=self.parent)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return tostring(self.xml, xmlns='', stanza_ns=self.namespace)
|
return tostring(self.xml, xmlns='', stanza_ns=self.namespace)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return self.__str__()
|
return self.__str__()
|
||||||
|
|
||||||
#def __del__(self): #prevents garbage collection of reference cycle
|
#def __del__(self): #prevents garbage collection of reference cycle
|
||||||
# if self.parent is not None:
|
# if self.parent is not None:
|
||||||
# self.parent.xml.remove(self.xml)
|
# self.parent.xml.remove(self.xml)
|
||||||
|
|
||||||
class StanzaBase(ElementBase):
|
class StanzaBase(ElementBase):
|
||||||
name = 'stanza'
|
name = 'stanza'
|
||||||
|
|
Loading…
Reference in a new issue