mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-12-28 11:14:19 +00:00
* added first stanza tests
* added stanza.keys() * stanza.getValues() now return substanzas and plugins * stanza.setValues() now can read substanzas and plugins * stanzas can now be iterable if stanza.subitem is set to a class
This commit is contained in:
parent
b54221f2a9
commit
8e3168e145
4 changed files with 161 additions and 66 deletions
|
@ -5,63 +5,52 @@ from .. xmlstream.xmlstream import XMLStream
|
|||
from . import xep_0004
|
||||
|
||||
def stanzaPlugin(stanza, plugin):
|
||||
stanza.plugin_attrib_map[plugin.plugin_attrib] = plugin
|
||||
stanza.plugin_tag_map["{%s}%s" % (plugin.namespace, plugin.name)] = plugin
|
||||
stanza.plugin_attrib_map[plugin.plugin_attrib] = plugin
|
||||
stanza.plugin_tag_map["{%s}%s" % (plugin.namespace, plugin.name)] = plugin
|
||||
|
||||
class Pubsub(ElementBase):
|
||||
namespace = 'http://jabber.org/protocol/pubsub'
|
||||
name = 'pubsub'
|
||||
plugin_attrib = 'pubsub'
|
||||
interfaces = set(tuple())
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
stanzaPlugin(Iq, Pubsub)
|
||||
|
||||
class PubsubOwner(ElementBase):
|
||||
namespace = 'http://jabber.org/protocol/pubsub#owner'
|
||||
name = 'pubsub_owner'
|
||||
plugin_attrib = 'pubsub'
|
||||
name = 'pubsub'
|
||||
plugin_attrib = 'pubsub_owner'
|
||||
interfaces = set(tuple())
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
stanzaPlugin(Iq, PubsubOwner)
|
||||
|
||||
class Affiliation(ElementBase):
|
||||
namespace = 'http://jabber.org/protocol/pubsub'
|
||||
name = 'affiliation'
|
||||
plugin_attrib = name
|
||||
interfaces = set(('node', 'affiliation'))
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
class Affiliations(ElementBase):
|
||||
namespace = 'http://jabber.org/protocol/pubsub'
|
||||
name = 'affiliations'
|
||||
plugin_attrib = 'affiliations'
|
||||
interfaces = set(tuple())
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
subitem = Affiliation
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
ElementBase.__init__(self, *args, **kwargs)
|
||||
self.affiliations = []
|
||||
self.idx = 0
|
||||
|
||||
def __iter__(self):
|
||||
self.idx = 0
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
self.idx += 1
|
||||
if self.idx + 1 > len(self.affiliations):
|
||||
self.idx = 0
|
||||
raise StopIteration
|
||||
return self.affiliations[self.idx]
|
||||
|
||||
def __len__(self):
|
||||
return len(self.affiliations)
|
||||
|
||||
def append(self, affiliation):
|
||||
if not isinstance(affiliation, Affiliation):
|
||||
raise TypeError
|
||||
self.xml.append(affiliation.xml)
|
||||
return self.affiliations.append(affiliation)
|
||||
|
||||
def pop(self, idx=0):
|
||||
aff = self.affiliations.pop(idx)
|
||||
self.xml.remove(aff.xml)
|
||||
return aff
|
||||
|
||||
def find(self, affiliation):
|
||||
return self.affiliations.find(affiliation)
|
||||
return self.iterables.append(affiliation)
|
||||
|
||||
|
||||
stanzaPlugin(Pubsub, Affiliations)
|
||||
|
||||
|
@ -70,6 +59,8 @@ class Subscriptions(ElementBase):
|
|||
name = 'subscriptions'
|
||||
plugin_attrib = 'subscriptions'
|
||||
interfaces = set(tuple())
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
ElementBase.__init__(self, *args, **kwargs)
|
||||
|
@ -106,17 +97,14 @@ class Subscriptions(ElementBase):
|
|||
|
||||
stanzaPlugin(Pubsub, Subscriptions)
|
||||
|
||||
class Affiliation(ElementBase):
|
||||
namespace = 'http://jabber.org/protocol/pubsub'
|
||||
name = 'affiliation'
|
||||
plugin_attrib = name
|
||||
interfaces = set(('node', 'affiliation'))
|
||||
|
||||
class Subscription(ElementBase):
|
||||
namespace = 'http://jabber.org/protocol/pubsub'
|
||||
name = 'subscription'
|
||||
plugin_attrib = name
|
||||
interfaces = set(('jid', 'node', 'subid', 'subscription'))
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
def setJid(self, value):
|
||||
self._setAttr('jid', str(value))
|
||||
|
@ -153,6 +141,8 @@ class SubscribeOptions(ElementBase, OptionalSetting):
|
|||
namespace = 'http://jabber.org/protocol/pubsub'
|
||||
name = 'subscribe-options'
|
||||
plugin_attrib = 'options'
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
stanzaPlugin(Subscription, SubscribeOptions)
|
||||
|
||||
|
@ -161,6 +151,8 @@ class Items(ElementBase):
|
|||
name = 'items'
|
||||
plugin_attrib = 'items'
|
||||
interfaces = set(tuple())
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
ElementBase.__init__(self, *args, **kwargs)
|
||||
|
@ -202,6 +194,8 @@ class Item(ElementBase):
|
|||
name = 'item'
|
||||
plugin_attrib = name
|
||||
interfaces = set(('id', 'payload'))
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
def setPayload(self, value):
|
||||
self.xml.append(value)
|
||||
|
@ -220,6 +214,8 @@ class Create(ElementBase):
|
|||
name = 'create'
|
||||
plugin_attrib = name
|
||||
interfaces = set(('node'))
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
stanzaPlugin(Pubsub, Create)
|
||||
|
||||
|
@ -228,6 +224,8 @@ class Default(ElementBase):
|
|||
name = 'default'
|
||||
plugin_attrib = name
|
||||
interfaces = set(('node', 'type'))
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
def getType(self):
|
||||
t = self._getAttr('type')
|
||||
|
@ -241,6 +239,8 @@ class Publish(Items):
|
|||
name = 'publish'
|
||||
plugin_attrib = name
|
||||
interfaces = set(('node'))
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
stanzaPlugin(Pubsub, Publish)
|
||||
|
||||
|
@ -249,6 +249,8 @@ class Retract(Items):
|
|||
name = 'retract'
|
||||
plugin_attrib = name
|
||||
interfaces = set(('node', 'notify'))
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
stanzaPlugin(Pubsub, Retract)
|
||||
|
||||
|
@ -257,6 +259,8 @@ class Unsubscribe(ElementBase):
|
|||
name = 'unsubscribe'
|
||||
plugin_attrib = name
|
||||
interfaces = set(('node', 'jid'))
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
def setJid(self, value):
|
||||
self._setAttr('jid', str(value))
|
||||
|
@ -269,6 +273,8 @@ class Subscribe(ElementBase):
|
|||
name = 'subscribe'
|
||||
plugin_attrib = name
|
||||
interfaces = set(('node', 'jid'))
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
def setJid(self, value):
|
||||
self._setAttr('jid', str(value))
|
||||
|
@ -283,6 +289,8 @@ class Configure(ElementBase):
|
|||
name = 'configure'
|
||||
plugin_attrib = name
|
||||
interfaces = set(('node', 'type', 'config'))
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
def getType(self):
|
||||
t = self._getAttr('type')
|
||||
|
@ -311,6 +319,8 @@ class DefaultConfig(ElementBase):
|
|||
name = 'default'
|
||||
plugin_attrib = 'defaultconfig'
|
||||
interfaces = set(('node', 'type', 'config'))
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
ElementBase.__init__(self, *args, **kwargs)
|
||||
|
@ -337,6 +347,8 @@ class Options(ElementBase):
|
|||
name = 'options'
|
||||
plugin_attrib = 'options'
|
||||
interfaces = set(('jid', 'node', 'options'))
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
ElementBase.__init__(self, *args, **kwargs)
|
||||
|
@ -364,34 +376,24 @@ class Options(ElementBase):
|
|||
|
||||
stanzaPlugin(Pubsub, Options)
|
||||
|
||||
iq = Iq()
|
||||
aff1 = Affiliation()
|
||||
aff1['node'] = 'testnode'
|
||||
aff1['affiliation'] = 'owner'
|
||||
aff2 = Affiliation()
|
||||
aff2['node'] = 'testnode2'
|
||||
aff2['affiliation'] = 'publisher'
|
||||
iq['pubsub']['affiliations'].append(aff1)
|
||||
iq['pubsub']['affiliations'].append(aff2)
|
||||
print(iq)
|
||||
iq['pubsub']['affiliations'].pop(0)
|
||||
print(iq)
|
||||
|
||||
iq = Iq()
|
||||
iq['pubsub']['defaultconfig']
|
||||
print(iq)
|
||||
#iq = Iq()
|
||||
#iq['pubsub']['defaultconfig']
|
||||
#print(iq)
|
||||
|
||||
from xml.etree import cElementTree as ET
|
||||
iq = Iq()
|
||||
item = Item()
|
||||
item['payload'] = ET.Element("{http://netflint.net/p/crap}stupidshit")
|
||||
item['id'] = 'aa11bbcc'
|
||||
iq['pubsub']['items'].append(item)
|
||||
print(iq)
|
||||
#from xml.etree import cElementTree as ET
|
||||
#iq = Iq()
|
||||
#item = Item()
|
||||
#item['payload'] = ET.Element("{http://netflint.net/p/crap}stupidshit")
|
||||
#item['id'] = 'aa11bbcc'
|
||||
#iq['pubsub']['items'].append(item)
|
||||
#print(iq)
|
||||
|
||||
class OwnerAffiliations(Affiliations):
|
||||
namespace = 'http://jabber.org/protocol/pubsub#owner'
|
||||
interfaces = set(('node'))
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
def append(self, affiliation):
|
||||
if not isinstance(affiliation, OwnerAffiliation):
|
||||
|
@ -404,16 +406,22 @@ stanzaPlugin(PubsubOwner, OwnerAffiliations)
|
|||
class OwnerAffiliation(Affiliation):
|
||||
namespace = 'http://jabber.org/protocol/pubsub#owner'
|
||||
interfaces = set(('affiliation', 'jid'))
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
class OwnerConfigure(Configure):
|
||||
namespace = 'http://jabber.org/protocol/pubsub#owner'
|
||||
interfaces = set(('node', 'config'))
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
stanzaPlugin(PubsubOwner, OwnerConfigure)
|
||||
|
||||
class OwnerDefault(OwnerConfigure):
|
||||
namespace = 'http://jabber.org/protocol/pubsub#owner'
|
||||
interfaces = set(('node', 'config'))
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
stanzaPlugin(PubsubOwner, OwnerDefault)
|
||||
|
||||
|
@ -421,6 +429,8 @@ class OwnerDelete(ElementBase, OptionalSetting):
|
|||
namespace = 'http://jabber.org/protocol/pubsub#owner'
|
||||
name = 'delete'
|
||||
plugin_attrib = 'delete'
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
stanzaPlugin(PubsubOwner, OwnerDelete)
|
||||
|
||||
|
@ -428,6 +438,8 @@ class OwnerPurge(ElementBase, OptionalSetting):
|
|||
namespace = 'http://jabber.org/protocol/pubsub#owner'
|
||||
name = 'purge'
|
||||
plugin_attrib = name
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
stanzaPlugin(PubsubOwner, OwnerPurge)
|
||||
|
||||
|
@ -436,6 +448,8 @@ class OwnerRedirect(ElementBase):
|
|||
name = 'redirect'
|
||||
plugin_attrib = name
|
||||
interfaces = set(('node', 'jid'))
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
def setJid(self, value):
|
||||
self._setAttr('jid', str(value))
|
||||
|
@ -448,6 +462,8 @@ stanzaPlugin(OwnerDelete, OwnerRedirect)
|
|||
class OwnerSubscriptions(Subscriptions):
|
||||
namespace = 'http://jabber.org/protocol/pubsub#owner'
|
||||
interfaces = set(('node',))
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
def append(self, subscription):
|
||||
if not isinstance(subscription, OwnerSubscription):
|
||||
|
@ -462,6 +478,8 @@ class OwnerSubscription(ElementBase):
|
|||
name = 'subscription'
|
||||
plugin_attrib = name
|
||||
interfaces = set(('jid', 'subscription'))
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
|
||||
def setJid(self, value):
|
||||
self._setAttr('jid', str(value))
|
||||
|
|
|
@ -48,14 +48,14 @@ class Iq(RootStanza):
|
|||
|
||||
def setQuery(self, value):
|
||||
query = self.xml.find("{%s}query" % value)
|
||||
if query is None:
|
||||
if query is None and value:
|
||||
self.clear()
|
||||
query = ET.Element("{%s}query" % value)
|
||||
self.xml.append(query)
|
||||
return self
|
||||
|
||||
def getQuery(self):
|
||||
for child in self.getchildren():
|
||||
for child in self.xml.getchildren():
|
||||
if child.tag.endswith('query'):
|
||||
ns =child.tag.split('}')[0]
|
||||
if '{' in ns:
|
||||
|
|
|
@ -30,16 +30,56 @@ class ElementBase(object):
|
|||
sub_interfaces = tuple()
|
||||
plugin_attrib_map = {}
|
||||
plugin_tag_map = {}
|
||||
subitem = None
|
||||
|
||||
def __init__(self, xml=None, parent=None):
|
||||
self.attrib = self # backwards compatibility hack
|
||||
self.parent = parent
|
||||
self.xml = xml
|
||||
self.plugins = {}
|
||||
if not self.setup(xml) and len(self.plugin_attrib_map):
|
||||
self.iterables = []
|
||||
self.idx = 0
|
||||
if not self.setup(xml):
|
||||
for child in self.xml.getchildren():
|
||||
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)
|
||||
if self.subitem is not None and child.tag == "{%s}%s" % (self.subitem.namespace, self.subitem.name):
|
||||
self.iterables.append(self.subitem(xml=child, parent=self))
|
||||
|
||||
def __iter__(self):
|
||||
self.idx = 0
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
self.idx += 1
|
||||
if self.idx + 1 > len(self.iterables):
|
||||
self.idx = 0
|
||||
raise StopIteration
|
||||
return self.affiliations[self.idx]
|
||||
|
||||
def __len__(self):
|
||||
return len(self.iterables)
|
||||
|
||||
def append(self, item):
|
||||
if not isinstance(item, ElementBase):
|
||||
raise TypeError
|
||||
self.xml.append(item.xml)
|
||||
return self.iterables.append(item)
|
||||
|
||||
def pop(self, idx=0):
|
||||
aff = self.iterables.pop(idx)
|
||||
self.xml.remove(aff.xml)
|
||||
return aff
|
||||
|
||||
def keys(self):
|
||||
out = []
|
||||
out += [x for x in self.interfaces]
|
||||
out += [x for x in self.plugins]
|
||||
if self.iterables:
|
||||
out.append('substanzas')
|
||||
|
||||
def find(self, item):
|
||||
return self.iterables.find(item)
|
||||
|
||||
def match(self, xml):
|
||||
return xml.tag == self.tag
|
||||
|
@ -72,7 +112,9 @@ class ElementBase(object):
|
|||
self.plugins[attrib] = self.plugin_attrib_map[attrib](parent=self)
|
||||
|
||||
def __getitem__(self, attrib):
|
||||
if attrib in self.interfaces:
|
||||
if attrib == 'substanzas':
|
||||
return self.iterables
|
||||
elif attrib in self.interfaces:
|
||||
if hasattr(self, "get%s" % attrib.title()):
|
||||
return getattr(self, "get%s" % attrib.title())()
|
||||
else:
|
||||
|
@ -165,12 +207,26 @@ class ElementBase(object):
|
|||
out = {}
|
||||
for interface in self.interfaces:
|
||||
out[interface] = self[interface]
|
||||
for pluginkey in self.plugins:
|
||||
out[pluginkey] = self.plugins[pluginkey].getValues()
|
||||
if self.iterables:
|
||||
iterables = [x.getValues() for x in self.iterables]
|
||||
out['substanzas'] = iterables
|
||||
return out
|
||||
|
||||
def setValues(self, attrib):
|
||||
for interface in attrib:
|
||||
if interface in self.interfaces:
|
||||
if interface == 'substanzas':
|
||||
for subdict in attrib['substanzas']:
|
||||
sub = self.subitem(parent=self)
|
||||
sub.setValues(subdict)
|
||||
self.iterables.append(sub)
|
||||
elif interface in self.interfaces:
|
||||
self[interface] = attrib[interface]
|
||||
elif interface in self.plugin_attrib_map and interface not in self.plugins:
|
||||
self.initPlugin(interface)
|
||||
if interface in self.plugins:
|
||||
self.plugins[interface].setValues(attrib[interface])
|
||||
return self
|
||||
|
||||
def append(self, xml):
|
||||
|
@ -204,8 +260,6 @@ class StanzaBase(ElementBase):
|
|||
def setType(self, value):
|
||||
if value in self.types:
|
||||
self.xml.attrib['type'] = value
|
||||
else:
|
||||
raise ValueError
|
||||
return self
|
||||
|
||||
def getPayload(self):
|
||||
|
|
23
tests/pubsub_stanzas.py
Normal file
23
tests/pubsub_stanzas.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
from sleekxmpp.plugins.stanza_pubsub import *
|
||||
|
||||
def testAffiliations():
|
||||
iq = Iq()
|
||||
aff1 = Affiliation()
|
||||
aff1['node'] = 'testnode'
|
||||
aff1['affiliation'] = 'owner'
|
||||
aff2 = Affiliation()
|
||||
aff2['node'] = 'testnode2'
|
||||
aff2['affiliation'] = 'publisher'
|
||||
iq['pubsub']['affiliations'].append(aff1)
|
||||
iq['pubsub']['affiliations'].append(aff2)
|
||||
print(iq)
|
||||
iq2 = Iq(None, ET.fromstring("""<iq id="0"><pubsub xmlns="http://jabber.org/protocol/pubsub"><affiliations><affiliation node="testnode" affiliation="owner" /><affiliation node="testnode2" affiliation="publisher" /></affiliations></pubsub></iq>"""))
|
||||
iq3 = Iq()
|
||||
values = iq2.getValues()
|
||||
print(values)
|
||||
iq3.setValues(values)
|
||||
print(iq3)
|
||||
print(str(iq) == str(iq2) == str(iq3))
|
||||
|
||||
|
||||
testAffiliations()
|
Loading…
Reference in a new issue