* 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:
Nathan Fritz 2010-01-08 01:45:11 +00:00
parent b54221f2a9
commit 8e3168e145
4 changed files with 161 additions and 66 deletions

View file

@ -5,63 +5,52 @@ from .. xmlstream.xmlstream import XMLStream
from . import xep_0004 from . import xep_0004
def stanzaPlugin(stanza, plugin): def stanzaPlugin(stanza, plugin):
stanza.plugin_attrib_map[plugin.plugin_attrib] = plugin stanza.plugin_attrib_map[plugin.plugin_attrib] = plugin
stanza.plugin_tag_map["{%s}%s" % (plugin.namespace, plugin.name)] = plugin stanza.plugin_tag_map["{%s}%s" % (plugin.namespace, plugin.name)] = plugin
class Pubsub(ElementBase): class Pubsub(ElementBase):
namespace = 'http://jabber.org/protocol/pubsub' namespace = 'http://jabber.org/protocol/pubsub'
name = 'pubsub' name = 'pubsub'
plugin_attrib = 'pubsub' plugin_attrib = 'pubsub'
interfaces = set(tuple()) interfaces = set(tuple())
plugin_attrib_map = {}
plugin_tag_map = {}
stanzaPlugin(Iq, Pubsub) stanzaPlugin(Iq, Pubsub)
class PubsubOwner(ElementBase): class PubsubOwner(ElementBase):
namespace = 'http://jabber.org/protocol/pubsub#owner' namespace = 'http://jabber.org/protocol/pubsub#owner'
name = 'pubsub_owner' name = 'pubsub'
plugin_attrib = 'pubsub' plugin_attrib = 'pubsub_owner'
interfaces = set(tuple()) interfaces = set(tuple())
plugin_attrib_map = {}
plugin_tag_map = {}
stanzaPlugin(Iq, PubsubOwner) 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): class Affiliations(ElementBase):
namespace = 'http://jabber.org/protocol/pubsub' namespace = 'http://jabber.org/protocol/pubsub'
name = 'affiliations' name = 'affiliations'
plugin_attrib = 'affiliations' plugin_attrib = 'affiliations'
interfaces = set(tuple()) interfaces = set(tuple())
plugin_attrib_map = {}
def __init__(self, *args, **kwargs): plugin_tag_map = {}
ElementBase.__init__(self, *args, **kwargs) subitem = Affiliation
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): def append(self, affiliation):
if not isinstance(affiliation, Affiliation): if not isinstance(affiliation, Affiliation):
raise TypeError raise TypeError
self.xml.append(affiliation.xml) self.xml.append(affiliation.xml)
return self.affiliations.append(affiliation) return self.iterables.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)
stanzaPlugin(Pubsub, Affiliations) stanzaPlugin(Pubsub, Affiliations)
@ -70,6 +59,8 @@ class Subscriptions(ElementBase):
name = 'subscriptions' name = 'subscriptions'
plugin_attrib = 'subscriptions' plugin_attrib = 'subscriptions'
interfaces = set(tuple()) interfaces = set(tuple())
plugin_attrib_map = {}
plugin_tag_map = {}
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
ElementBase.__init__(self, *args, **kwargs) ElementBase.__init__(self, *args, **kwargs)
@ -106,17 +97,14 @@ class Subscriptions(ElementBase):
stanzaPlugin(Pubsub, Subscriptions) stanzaPlugin(Pubsub, Subscriptions)
class Affiliation(ElementBase):
namespace = 'http://jabber.org/protocol/pubsub'
name = 'affiliation'
plugin_attrib = name
interfaces = set(('node', 'affiliation'))
class Subscription(ElementBase): class Subscription(ElementBase):
namespace = 'http://jabber.org/protocol/pubsub' namespace = 'http://jabber.org/protocol/pubsub'
name = 'subscription' name = 'subscription'
plugin_attrib = name plugin_attrib = name
interfaces = set(('jid', 'node', 'subid', 'subscription')) interfaces = set(('jid', 'node', 'subid', 'subscription'))
plugin_attrib_map = {}
plugin_tag_map = {}
def setJid(self, value): def setJid(self, value):
self._setAttr('jid', str(value)) self._setAttr('jid', str(value))
@ -153,6 +141,8 @@ class SubscribeOptions(ElementBase, OptionalSetting):
namespace = 'http://jabber.org/protocol/pubsub' namespace = 'http://jabber.org/protocol/pubsub'
name = 'subscribe-options' name = 'subscribe-options'
plugin_attrib = 'options' plugin_attrib = 'options'
plugin_attrib_map = {}
plugin_tag_map = {}
stanzaPlugin(Subscription, SubscribeOptions) stanzaPlugin(Subscription, SubscribeOptions)
@ -161,6 +151,8 @@ class Items(ElementBase):
name = 'items' name = 'items'
plugin_attrib = 'items' plugin_attrib = 'items'
interfaces = set(tuple()) interfaces = set(tuple())
plugin_attrib_map = {}
plugin_tag_map = {}
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
ElementBase.__init__(self, *args, **kwargs) ElementBase.__init__(self, *args, **kwargs)
@ -202,6 +194,8 @@ class Item(ElementBase):
name = 'item' name = 'item'
plugin_attrib = name plugin_attrib = name
interfaces = set(('id', 'payload')) interfaces = set(('id', 'payload'))
plugin_attrib_map = {}
plugin_tag_map = {}
def setPayload(self, value): def setPayload(self, value):
self.xml.append(value) self.xml.append(value)
@ -220,6 +214,8 @@ class Create(ElementBase):
name = 'create' name = 'create'
plugin_attrib = name plugin_attrib = name
interfaces = set(('node')) interfaces = set(('node'))
plugin_attrib_map = {}
plugin_tag_map = {}
stanzaPlugin(Pubsub, Create) stanzaPlugin(Pubsub, Create)
@ -228,6 +224,8 @@ class Default(ElementBase):
name = 'default' name = 'default'
plugin_attrib = name plugin_attrib = name
interfaces = set(('node', 'type')) interfaces = set(('node', 'type'))
plugin_attrib_map = {}
plugin_tag_map = {}
def getType(self): def getType(self):
t = self._getAttr('type') t = self._getAttr('type')
@ -241,6 +239,8 @@ class Publish(Items):
name = 'publish' name = 'publish'
plugin_attrib = name plugin_attrib = name
interfaces = set(('node')) interfaces = set(('node'))
plugin_attrib_map = {}
plugin_tag_map = {}
stanzaPlugin(Pubsub, Publish) stanzaPlugin(Pubsub, Publish)
@ -249,6 +249,8 @@ class Retract(Items):
name = 'retract' name = 'retract'
plugin_attrib = name plugin_attrib = name
interfaces = set(('node', 'notify')) interfaces = set(('node', 'notify'))
plugin_attrib_map = {}
plugin_tag_map = {}
stanzaPlugin(Pubsub, Retract) stanzaPlugin(Pubsub, Retract)
@ -257,6 +259,8 @@ class Unsubscribe(ElementBase):
name = 'unsubscribe' name = 'unsubscribe'
plugin_attrib = name plugin_attrib = name
interfaces = set(('node', 'jid')) interfaces = set(('node', 'jid'))
plugin_attrib_map = {}
plugin_tag_map = {}
def setJid(self, value): def setJid(self, value):
self._setAttr('jid', str(value)) self._setAttr('jid', str(value))
@ -269,6 +273,8 @@ class Subscribe(ElementBase):
name = 'subscribe' name = 'subscribe'
plugin_attrib = name plugin_attrib = name
interfaces = set(('node', 'jid')) interfaces = set(('node', 'jid'))
plugin_attrib_map = {}
plugin_tag_map = {}
def setJid(self, value): def setJid(self, value):
self._setAttr('jid', str(value)) self._setAttr('jid', str(value))
@ -283,6 +289,8 @@ class Configure(ElementBase):
name = 'configure' name = 'configure'
plugin_attrib = name plugin_attrib = name
interfaces = set(('node', 'type', 'config')) interfaces = set(('node', 'type', 'config'))
plugin_attrib_map = {}
plugin_tag_map = {}
def getType(self): def getType(self):
t = self._getAttr('type') t = self._getAttr('type')
@ -311,6 +319,8 @@ class DefaultConfig(ElementBase):
name = 'default' name = 'default'
plugin_attrib = 'defaultconfig' plugin_attrib = 'defaultconfig'
interfaces = set(('node', 'type', 'config')) interfaces = set(('node', 'type', 'config'))
plugin_attrib_map = {}
plugin_tag_map = {}
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
ElementBase.__init__(self, *args, **kwargs) ElementBase.__init__(self, *args, **kwargs)
@ -337,6 +347,8 @@ class Options(ElementBase):
name = 'options' name = 'options'
plugin_attrib = 'options' plugin_attrib = 'options'
interfaces = set(('jid', 'node', 'options')) interfaces = set(('jid', 'node', 'options'))
plugin_attrib_map = {}
plugin_tag_map = {}
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
ElementBase.__init__(self, *args, **kwargs) ElementBase.__init__(self, *args, **kwargs)
@ -364,34 +376,24 @@ class Options(ElementBase):
stanzaPlugin(Pubsub, Options) 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 = Iq()
iq['pubsub']['defaultconfig'] #iq['pubsub']['defaultconfig']
print(iq) #print(iq)
from xml.etree import cElementTree as ET #from xml.etree import cElementTree as ET
iq = Iq() #iq = Iq()
item = Item() #item = Item()
item['payload'] = ET.Element("{http://netflint.net/p/crap}stupidshit") #item['payload'] = ET.Element("{http://netflint.net/p/crap}stupidshit")
item['id'] = 'aa11bbcc' #item['id'] = 'aa11bbcc'
iq['pubsub']['items'].append(item) #iq['pubsub']['items'].append(item)
print(iq) #print(iq)
class OwnerAffiliations(Affiliations): class OwnerAffiliations(Affiliations):
namespace = 'http://jabber.org/protocol/pubsub#owner' namespace = 'http://jabber.org/protocol/pubsub#owner'
interfaces = set(('node')) interfaces = set(('node'))
plugin_attrib_map = {}
plugin_tag_map = {}
def append(self, affiliation): def append(self, affiliation):
if not isinstance(affiliation, OwnerAffiliation): if not isinstance(affiliation, OwnerAffiliation):
@ -404,16 +406,22 @@ stanzaPlugin(PubsubOwner, OwnerAffiliations)
class OwnerAffiliation(Affiliation): class OwnerAffiliation(Affiliation):
namespace = 'http://jabber.org/protocol/pubsub#owner' namespace = 'http://jabber.org/protocol/pubsub#owner'
interfaces = set(('affiliation', 'jid')) interfaces = set(('affiliation', 'jid'))
plugin_attrib_map = {}
plugin_tag_map = {}
class OwnerConfigure(Configure): class OwnerConfigure(Configure):
namespace = 'http://jabber.org/protocol/pubsub#owner' namespace = 'http://jabber.org/protocol/pubsub#owner'
interfaces = set(('node', 'config')) interfaces = set(('node', 'config'))
plugin_attrib_map = {}
plugin_tag_map = {}
stanzaPlugin(PubsubOwner, OwnerConfigure) stanzaPlugin(PubsubOwner, OwnerConfigure)
class OwnerDefault(OwnerConfigure): class OwnerDefault(OwnerConfigure):
namespace = 'http://jabber.org/protocol/pubsub#owner' namespace = 'http://jabber.org/protocol/pubsub#owner'
interfaces = set(('node', 'config')) interfaces = set(('node', 'config'))
plugin_attrib_map = {}
plugin_tag_map = {}
stanzaPlugin(PubsubOwner, OwnerDefault) stanzaPlugin(PubsubOwner, OwnerDefault)
@ -421,6 +429,8 @@ class OwnerDelete(ElementBase, OptionalSetting):
namespace = 'http://jabber.org/protocol/pubsub#owner' namespace = 'http://jabber.org/protocol/pubsub#owner'
name = 'delete' name = 'delete'
plugin_attrib = 'delete' plugin_attrib = 'delete'
plugin_attrib_map = {}
plugin_tag_map = {}
stanzaPlugin(PubsubOwner, OwnerDelete) stanzaPlugin(PubsubOwner, OwnerDelete)
@ -428,6 +438,8 @@ class OwnerPurge(ElementBase, OptionalSetting):
namespace = 'http://jabber.org/protocol/pubsub#owner' namespace = 'http://jabber.org/protocol/pubsub#owner'
name = 'purge' name = 'purge'
plugin_attrib = name plugin_attrib = name
plugin_attrib_map = {}
plugin_tag_map = {}
stanzaPlugin(PubsubOwner, OwnerPurge) stanzaPlugin(PubsubOwner, OwnerPurge)
@ -436,6 +448,8 @@ class OwnerRedirect(ElementBase):
name = 'redirect' name = 'redirect'
plugin_attrib = name plugin_attrib = name
interfaces = set(('node', 'jid')) interfaces = set(('node', 'jid'))
plugin_attrib_map = {}
plugin_tag_map = {}
def setJid(self, value): def setJid(self, value):
self._setAttr('jid', str(value)) self._setAttr('jid', str(value))
@ -448,6 +462,8 @@ stanzaPlugin(OwnerDelete, OwnerRedirect)
class OwnerSubscriptions(Subscriptions): class OwnerSubscriptions(Subscriptions):
namespace = 'http://jabber.org/protocol/pubsub#owner' namespace = 'http://jabber.org/protocol/pubsub#owner'
interfaces = set(('node',)) interfaces = set(('node',))
plugin_attrib_map = {}
plugin_tag_map = {}
def append(self, subscription): def append(self, subscription):
if not isinstance(subscription, OwnerSubscription): if not isinstance(subscription, OwnerSubscription):
@ -462,6 +478,8 @@ class OwnerSubscription(ElementBase):
name = 'subscription' name = 'subscription'
plugin_attrib = name plugin_attrib = name
interfaces = set(('jid', 'subscription')) interfaces = set(('jid', 'subscription'))
plugin_attrib_map = {}
plugin_tag_map = {}
def setJid(self, value): def setJid(self, value):
self._setAttr('jid', str(value)) self._setAttr('jid', str(value))

View file

@ -48,14 +48,14 @@ class Iq(RootStanza):
def setQuery(self, value): def setQuery(self, value):
query = self.xml.find("{%s}query" % value) query = self.xml.find("{%s}query" % value)
if query is None: if query is None and value:
self.clear() self.clear()
query = ET.Element("{%s}query" % value) query = ET.Element("{%s}query" % value)
self.xml.append(query) self.xml.append(query)
return self return self
def getQuery(self): def getQuery(self):
for child in self.getchildren(): for child in self.xml.getchildren():
if child.tag.endswith('query'): if child.tag.endswith('query'):
ns =child.tag.split('}')[0] ns =child.tag.split('}')[0]
if '{' in ns: if '{' in ns:

View file

@ -30,16 +30,56 @@ class ElementBase(object):
sub_interfaces = tuple() sub_interfaces = tuple()
plugin_attrib_map = {} plugin_attrib_map = {}
plugin_tag_map = {} plugin_tag_map = {}
subitem = None
def __init__(self, xml=None, parent=None): def __init__(self, xml=None, parent=None):
self.attrib = self # backwards compatibility hack self.attrib = self # backwards compatibility hack
self.parent = parent self.parent = parent
self.xml = xml self.xml = xml
self.plugins = {} 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(): 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 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): def match(self, xml):
return xml.tag == self.tag return xml.tag == self.tag
@ -72,7 +112,9 @@ class ElementBase(object):
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 in self.interfaces: if attrib == 'substanzas':
return self.iterables
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:
@ -165,12 +207,26 @@ class ElementBase(object):
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:
out[pluginkey] = self.plugins[pluginkey].getValues()
if self.iterables:
iterables = [x.getValues() for x in self.iterables]
out['substanzas'] = iterables
return out return out
def setValues(self, attrib): def setValues(self, attrib):
for interface in 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] 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 return self
def append(self, xml): def append(self, xml):
@ -204,8 +260,6 @@ class StanzaBase(ElementBase):
def setType(self, value): def setType(self, value):
if value in self.types: if value in self.types:
self.xml.attrib['type'] = value self.xml.attrib['type'] = value
else:
raise ValueError
return self return self
def getPayload(self): def getPayload(self):

23
tests/pubsub_stanzas.py Normal file
View 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()