mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-11-27 19:19:54 +00:00
Added new XEP-0059 plugin.
Contributed by Erik Reuterborg Larsson (who).
This commit is contained in:
parent
13a2f719f4
commit
a8e3657487
5 changed files with 504 additions and 0 deletions
10
sleekxmpp/plugins/xep_0059/__init__.py
Normal file
10
sleekxmpp/plugins/xep_0059/__init__.py
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
"""
|
||||||
|
SleekXMPP: The Sleek XMPP Library
|
||||||
|
Copyright (C) 2010 Nathanael C. Fritz, Erik Reuterborg Larsson
|
||||||
|
This file is part of SleekXMPP.
|
||||||
|
|
||||||
|
See the file LICENSE for copying permission.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from sleekxmpp.plugins.xep_0059.stanza import Set
|
||||||
|
from sleekxmpp.plugins.xep_0059.rsm import ResultIterator, xep_0059
|
118
sleekxmpp/plugins/xep_0059/rsm.py
Normal file
118
sleekxmpp/plugins/xep_0059/rsm.py
Normal file
|
@ -0,0 +1,118 @@
|
||||||
|
"""
|
||||||
|
SleekXMPP: The Sleek XMPP Library
|
||||||
|
Copyright (C) 2010 Nathanael C. Fritz, Erik Reuterborg Larsson
|
||||||
|
This file is part of SleekXMPP.
|
||||||
|
|
||||||
|
See the file LICENSE for copying permission.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
import sleekxmpp
|
||||||
|
from sleekxmpp import Iq
|
||||||
|
from sleekxmpp.plugins.base import base_plugin
|
||||||
|
from sleekxmpp.xmlstream import register_stanza_plugin
|
||||||
|
from sleekxmpp.plugins.xep_0059 import Set
|
||||||
|
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class ResultIterator():
|
||||||
|
|
||||||
|
"""
|
||||||
|
An iterator for Result Set Managment
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, query, interface, amount=10, start=None, reverse=False):
|
||||||
|
"""
|
||||||
|
Arguments:
|
||||||
|
query -- The template query
|
||||||
|
interface -- The substanza of the query, for example disco_items
|
||||||
|
amount -- The max amounts of items to request per iteration
|
||||||
|
start -- From which item id to start
|
||||||
|
reverse -- If True, page backwards through the results
|
||||||
|
|
||||||
|
Example:
|
||||||
|
q = Iq()
|
||||||
|
q['to'] = 'pubsub.example.com'
|
||||||
|
q['disco_items']['node'] = 'blog'
|
||||||
|
for i in ResultIterator(q, 'disco_items', '10'):
|
||||||
|
print i['disco_items']['items']
|
||||||
|
|
||||||
|
"""
|
||||||
|
self.query = query
|
||||||
|
self.amount = amount
|
||||||
|
self.start = start
|
||||||
|
self.interface = interface
|
||||||
|
self.reverse = reverse
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __next__(self):
|
||||||
|
return self.next()
|
||||||
|
|
||||||
|
def next(self):
|
||||||
|
"""
|
||||||
|
Return the next page of results from a query.
|
||||||
|
|
||||||
|
Note: If using backwards paging, then the next page of
|
||||||
|
results will be the items before the current page
|
||||||
|
of items.
|
||||||
|
"""
|
||||||
|
self.query[self.interface]['rsm']['before'] = self.reverse
|
||||||
|
self.query['id'] = self.query.stream.new_id()
|
||||||
|
self.query[self.interface]['rsm']['max'] = str(self.amount)
|
||||||
|
|
||||||
|
if self.start and self.reverse:
|
||||||
|
self.query[self.interface]['rsm']['before'] = self.start
|
||||||
|
elif self.start:
|
||||||
|
self.query[self.interface]['rsm']['after'] = self.start
|
||||||
|
|
||||||
|
r = self.query.send(block=True)
|
||||||
|
|
||||||
|
if not r or not r[self.interface]['rsm']['first'] and \
|
||||||
|
not r[self.interface]['rsm']['last']:
|
||||||
|
raise StopIteration
|
||||||
|
|
||||||
|
if self.reverse:
|
||||||
|
self.start = r[self.interface]['rsm']['first']
|
||||||
|
else:
|
||||||
|
self.start = r[self.interface]['rsm']['last']
|
||||||
|
|
||||||
|
return r
|
||||||
|
|
||||||
|
|
||||||
|
class xep_0059(base_plugin):
|
||||||
|
|
||||||
|
"""
|
||||||
|
XEP-0050: Result Set Management
|
||||||
|
"""
|
||||||
|
|
||||||
|
def plugin_init(self):
|
||||||
|
"""
|
||||||
|
Start the XEP-0059 plugin.
|
||||||
|
"""
|
||||||
|
self.xep = '0059'
|
||||||
|
self.description = 'Result Set Management'
|
||||||
|
self.stanza = sleekxmpp.plugins.xep_0059.stanza
|
||||||
|
|
||||||
|
def post_init(self):
|
||||||
|
"""Handle inter-plugin dependencies."""
|
||||||
|
self.xmpp['xep_0030'].add_feature(Set.namespace)
|
||||||
|
|
||||||
|
def iterate(self, stanza, interface):
|
||||||
|
"""
|
||||||
|
Create a new result set iterator for a given stanza query.
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
stanza -- A stanza object to serve as a template for
|
||||||
|
queries made each iteration. For example, a
|
||||||
|
basic disco#items query.
|
||||||
|
interface -- The name of the substanza to which the
|
||||||
|
result set management stanza should be
|
||||||
|
appended. For example, for disco#items queries
|
||||||
|
the interface 'disco_items' should be used.
|
||||||
|
"""
|
||||||
|
return ResultIterator(stanza, interface)
|
108
sleekxmpp/plugins/xep_0059/stanza.py
Normal file
108
sleekxmpp/plugins/xep_0059/stanza.py
Normal file
|
@ -0,0 +1,108 @@
|
||||||
|
"""
|
||||||
|
SleekXMPP: The Sleek XMPP Library
|
||||||
|
Copyright (C) 2010 Nathanael C. Fritz, Erik Reuterborg Larsson
|
||||||
|
This file is part of SleekXMPP.
|
||||||
|
|
||||||
|
See the file LICENSE for copying permission.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from sleekxmpp.xmlstream import ElementBase, ET
|
||||||
|
from sleekxmpp.plugins.xep_0030.stanza.items import DiscoItems
|
||||||
|
|
||||||
|
|
||||||
|
class Set(ElementBase):
|
||||||
|
|
||||||
|
"""
|
||||||
|
XEP-0059 (Result Set Managment) can be used to manage the
|
||||||
|
results of queries. For example, limiting the number of items
|
||||||
|
per response or starting at certain positions.
|
||||||
|
|
||||||
|
Example set stanzas:
|
||||||
|
<iq type="get">
|
||||||
|
<query xmlns="http://jabber.org/protocol/disco#items">
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<max>2</max>
|
||||||
|
</set>
|
||||||
|
</query>
|
||||||
|
</iq>
|
||||||
|
|
||||||
|
<iq type="result">
|
||||||
|
<query xmlns="http://jabber.org/protocol/disco#items">
|
||||||
|
<item jid="conference.example.com" />
|
||||||
|
<item jid="pubsub.example.com" />
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<first>conference.example.com</first>
|
||||||
|
<last>pubsub.example.com</last>
|
||||||
|
</set>
|
||||||
|
</query>
|
||||||
|
</iq>
|
||||||
|
|
||||||
|
Stanza Interface:
|
||||||
|
first_index -- The index attribute of <first>
|
||||||
|
after -- The id defining from which item to start
|
||||||
|
before -- The id defining from which item to
|
||||||
|
start when browsing backwards
|
||||||
|
max -- Max amount per response
|
||||||
|
first -- Id for the first item in the response
|
||||||
|
last -- Id for the last item in the response
|
||||||
|
index -- Used to set an index to start from
|
||||||
|
count -- The number of remote items available
|
||||||
|
|
||||||
|
Methods:
|
||||||
|
set_first_index -- Sets the index attribute for <first> and
|
||||||
|
creates the element if it doesn't exist
|
||||||
|
get_first_index -- Returns the value of the index
|
||||||
|
attribute for <first>
|
||||||
|
del_first_index -- Removes the index attribute for <first>
|
||||||
|
but keeps the element
|
||||||
|
set_before -- Sets the value of <before>, if the value is True
|
||||||
|
then the element will be created without a value
|
||||||
|
get_before -- Returns the value of <before>, if it is
|
||||||
|
empty it will return True
|
||||||
|
|
||||||
|
"""
|
||||||
|
namespace = 'http://jabber.org/protocol/rsm'
|
||||||
|
name = 'set'
|
||||||
|
plugin_attrib = 'rsm'
|
||||||
|
sub_interfaces = set(('first', 'after', 'before', 'count',
|
||||||
|
'index', 'last', 'max'))
|
||||||
|
interfaces = set(('first_index', 'first', 'after', 'before',
|
||||||
|
'count', 'index', 'last', 'max'))
|
||||||
|
|
||||||
|
def set_first_index(self, val):
|
||||||
|
fi = self.find("{%s}first" % (self.namespace))
|
||||||
|
if fi is not None:
|
||||||
|
if val:
|
||||||
|
fi.attrib['index'] = val
|
||||||
|
else:
|
||||||
|
del fi.attrib['index']
|
||||||
|
elif val:
|
||||||
|
fi = ET.Element("{%s}first" % (self.namespace))
|
||||||
|
fi.attrib['index'] = val
|
||||||
|
self.xml.append(fi)
|
||||||
|
|
||||||
|
def get_first_index(self):
|
||||||
|
fi = self.find("{%s}first" % (self.namespace))
|
||||||
|
if fi is not None:
|
||||||
|
return fi.attrib.get('index', '')
|
||||||
|
|
||||||
|
def del_first_index(self):
|
||||||
|
fi = self.xml.find("{%s}first" % (self.namespace))
|
||||||
|
if fi is not None:
|
||||||
|
del fi.attrib['index']
|
||||||
|
|
||||||
|
def set_before(self, val):
|
||||||
|
b = self.xml.find("{%s}before" % (self.namespace))
|
||||||
|
if b is None and val == True:
|
||||||
|
self._set_sub_text('{%s}before' % self.namespace, '', True)
|
||||||
|
else:
|
||||||
|
self._set_sub_text('{%s}before' % self.namespace, val)
|
||||||
|
|
||||||
|
def get_before(self):
|
||||||
|
b = self.xml.find("{%s}before" % (self.namespace))
|
||||||
|
if b is not None and not b.text:
|
||||||
|
return True
|
||||||
|
elif b is not None:
|
||||||
|
return b.text
|
||||||
|
else:
|
||||||
|
return None
|
106
tests/test_stanza_xep_0059.py
Normal file
106
tests/test_stanza_xep_0059.py
Normal file
|
@ -0,0 +1,106 @@
|
||||||
|
from sleekxmpp.test import *
|
||||||
|
from sleekxmpp.plugins.xep_0059 import Set
|
||||||
|
|
||||||
|
|
||||||
|
class TestSetStanzas(SleekTest):
|
||||||
|
|
||||||
|
def testSetFirstIndex(self):
|
||||||
|
s = Set()
|
||||||
|
s['first'] = 'id'
|
||||||
|
s.set_first_index('10')
|
||||||
|
self.check(s, """
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<first index="10">id</first>
|
||||||
|
</set>
|
||||||
|
""")
|
||||||
|
|
||||||
|
def testGetFirstIndex(self):
|
||||||
|
xml_string = """
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<first index="10">id</first>
|
||||||
|
</set>
|
||||||
|
"""
|
||||||
|
s = Set(ET.fromstring(xml_string))
|
||||||
|
expected = '10'
|
||||||
|
self.failUnless(s['first_index'] == expected)
|
||||||
|
|
||||||
|
def testDelFirstIndex(self):
|
||||||
|
xml_string = """
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<first index="10">id</first>
|
||||||
|
</set>
|
||||||
|
"""
|
||||||
|
s = Set(ET.fromstring(xml_string))
|
||||||
|
del s['first_index']
|
||||||
|
self.check(s, """
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<first>id</first>
|
||||||
|
</set>
|
||||||
|
""")
|
||||||
|
|
||||||
|
def testSetBefore(self):
|
||||||
|
s = Set()
|
||||||
|
s['before'] = True
|
||||||
|
self.check(s, """
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<before />
|
||||||
|
</set>
|
||||||
|
""")
|
||||||
|
|
||||||
|
def testGetBefore(self):
|
||||||
|
xml_string = """
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<before />
|
||||||
|
</set>
|
||||||
|
"""
|
||||||
|
s = Set(ET.fromstring(xml_string))
|
||||||
|
expected = True
|
||||||
|
self.failUnless(s['before'] == expected)
|
||||||
|
|
||||||
|
def testGetBefore(self):
|
||||||
|
xml_string = """
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<before />
|
||||||
|
</set>
|
||||||
|
"""
|
||||||
|
s = Set(ET.fromstring(xml_string))
|
||||||
|
del s['before']
|
||||||
|
self.check(s, """
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
</set>
|
||||||
|
""")
|
||||||
|
|
||||||
|
def testSetBeforeVal(self):
|
||||||
|
s = Set()
|
||||||
|
s['before'] = 'id'
|
||||||
|
self.check(s, """
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<before>id</before>
|
||||||
|
</set>
|
||||||
|
""")
|
||||||
|
|
||||||
|
def testGetBeforeVal(self):
|
||||||
|
xml_string = """
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<before>id</before>
|
||||||
|
</set>
|
||||||
|
"""
|
||||||
|
s = Set(ET.fromstring(xml_string))
|
||||||
|
expected = 'id'
|
||||||
|
self.failUnless(s['before'] == expected)
|
||||||
|
|
||||||
|
def testGetBeforeVal(self):
|
||||||
|
xml_string = """
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<before>id</before>
|
||||||
|
</set>
|
||||||
|
"""
|
||||||
|
s = Set(ET.fromstring(xml_string))
|
||||||
|
del s['before']
|
||||||
|
self.check(s, """
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
</set>
|
||||||
|
""")
|
||||||
|
|
||||||
|
|
||||||
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestSetStanzas)
|
162
tests/test_stream_xep_0059.py
Normal file
162
tests/test_stream_xep_0059.py
Normal file
|
@ -0,0 +1,162 @@
|
||||||
|
import threading
|
||||||
|
|
||||||
|
from sleekxmpp.test import *
|
||||||
|
from sleekxmpp.xmlstream import register_stanza_plugin
|
||||||
|
from sleekxmpp.plugins.xep_0030 import DiscoItems
|
||||||
|
from sleekxmpp.plugins.xep_0059 import ResultIterator, Set
|
||||||
|
|
||||||
|
|
||||||
|
class TestStreamSet(SleekTest):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
register_stanza_plugin(DiscoItems, Set)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.stream_close()
|
||||||
|
|
||||||
|
def iter(self, rev=False):
|
||||||
|
q = self.xmpp.Iq()
|
||||||
|
q['type'] = 'get'
|
||||||
|
it = ResultIterator(q, 'disco_items', '1', reverse=rev)
|
||||||
|
for i in it:
|
||||||
|
for j in i['disco_items']['items']:
|
||||||
|
self.items.append(j[0])
|
||||||
|
|
||||||
|
def testResultIterator(self):
|
||||||
|
self.items = []
|
||||||
|
self.stream_start(mode='client')
|
||||||
|
t = threading.Thread(target=self.iter)
|
||||||
|
t.start()
|
||||||
|
self.send("""
|
||||||
|
<iq type="get" id="2">
|
||||||
|
<query xmlns="http://jabber.org/protocol/disco#items">
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<max>1</max>
|
||||||
|
</set>
|
||||||
|
</query>
|
||||||
|
</iq>
|
||||||
|
""")
|
||||||
|
self.recv("""
|
||||||
|
<iq type="result" id="2">
|
||||||
|
<query xmlns="http://jabber.org/protocol/disco#items">
|
||||||
|
<item jid="item1" />
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<last>item1</last>
|
||||||
|
</set>
|
||||||
|
</query>
|
||||||
|
</iq>
|
||||||
|
""")
|
||||||
|
self.send("""
|
||||||
|
<iq type="get" id="3">
|
||||||
|
<query xmlns="http://jabber.org/protocol/disco#items">
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<max>1</max>
|
||||||
|
<after>item1</after>
|
||||||
|
</set>
|
||||||
|
</query>
|
||||||
|
</iq>
|
||||||
|
""")
|
||||||
|
self.recv("""
|
||||||
|
<iq type="result" id="3">
|
||||||
|
<query xmlns="http://jabber.org/protocol/disco#items">
|
||||||
|
<item jid="item2" />
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<last>item2</last>
|
||||||
|
</set>
|
||||||
|
</query>
|
||||||
|
</iq>
|
||||||
|
""")
|
||||||
|
self.send("""
|
||||||
|
<iq type="get" id="4">
|
||||||
|
<query xmlns="http://jabber.org/protocol/disco#items">
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<max>1</max>
|
||||||
|
<after>item2</after>
|
||||||
|
</set>
|
||||||
|
</query>
|
||||||
|
</iq>
|
||||||
|
""")
|
||||||
|
self.recv("""
|
||||||
|
<iq type="result" id="4">
|
||||||
|
<query xmlns="http://jabber.org/protocol/disco#items">
|
||||||
|
<item jid="item2" />
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
</set>
|
||||||
|
</query>
|
||||||
|
</iq>
|
||||||
|
""")
|
||||||
|
t.join()
|
||||||
|
self.failUnless(self.items == ['item1', 'item2'])
|
||||||
|
|
||||||
|
def testResultIteratorReverse(self):
|
||||||
|
self.items = []
|
||||||
|
self.stream_start(mode='client')
|
||||||
|
|
||||||
|
t = threading.Thread(target=self.iter, args=(True,))
|
||||||
|
t.start()
|
||||||
|
|
||||||
|
self.send("""
|
||||||
|
<iq type="get" id="2">
|
||||||
|
<query xmlns="http://jabber.org/protocol/disco#items">
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<max>1</max>
|
||||||
|
<before />
|
||||||
|
</set>
|
||||||
|
</query>
|
||||||
|
</iq>
|
||||||
|
""")
|
||||||
|
self.recv("""
|
||||||
|
<iq type="result" id="2">
|
||||||
|
<query xmlns="http://jabber.org/protocol/disco#items">
|
||||||
|
<item jid="item2" />
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<first>item2</first>
|
||||||
|
</set>
|
||||||
|
</query>
|
||||||
|
</iq>
|
||||||
|
""")
|
||||||
|
self.send("""
|
||||||
|
<iq type="get" id="3">
|
||||||
|
<query xmlns="http://jabber.org/protocol/disco#items">
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<max>1</max>
|
||||||
|
<before>item2</before>
|
||||||
|
</set>
|
||||||
|
</query>
|
||||||
|
</iq>
|
||||||
|
""")
|
||||||
|
self.recv("""
|
||||||
|
<iq type="result" id="3">
|
||||||
|
<query xmlns="http://jabber.org/protocol/disco#items">
|
||||||
|
<item jid="item1" />
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<first>item1</first>
|
||||||
|
</set>
|
||||||
|
</query>
|
||||||
|
</iq>
|
||||||
|
""")
|
||||||
|
self.send("""
|
||||||
|
<iq type="get" id="4">
|
||||||
|
<query xmlns="http://jabber.org/protocol/disco#items">
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
<max>1</max>
|
||||||
|
<before>item1</before>
|
||||||
|
</set>
|
||||||
|
</query>
|
||||||
|
</iq>
|
||||||
|
""")
|
||||||
|
self.recv("""
|
||||||
|
<iq type="result" id="4">
|
||||||
|
<query xmlns="http://jabber.org/protocol/disco#items">
|
||||||
|
<item jid="item1" />
|
||||||
|
<set xmlns="http://jabber.org/protocol/rsm">
|
||||||
|
</set>
|
||||||
|
</query>
|
||||||
|
</iq>
|
||||||
|
""")
|
||||||
|
|
||||||
|
t.join()
|
||||||
|
self.failUnless(self.items == ['item2', 'item1'])
|
||||||
|
|
||||||
|
|
||||||
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestStreamSet)
|
Loading…
Reference in a new issue