Fix detecting end of result set paging.

This commit is contained in:
Lance Stout 2012-01-18 19:57:49 -08:00
parent bb3080e829
commit b25668b5b7
2 changed files with 51 additions and 25 deletions

View file

@ -6,7 +6,7 @@
See the file LICENSE for copying permission. See the file LICENSE for copying permission.
""" """
from sleekxmpp.xmlstream import ElementBase, ET from sleekxmpp.xmlstream import ElementBase, ET, register_stanza_plugin
class DiscoItems(ElementBase): class DiscoItems(ElementBase):
@ -78,13 +78,11 @@ class DiscoItems(ElementBase):
""" """
if (jid, node) not in self._items: if (jid, node) not in self._items:
self._items.add((jid, node)) self._items.add((jid, node))
item_xml = ET.Element('{%s}item' % self.namespace) item = DiscoItem(parent=self)
item_xml.attrib['jid'] = jid item['jid'] = jid
if name: item['node'] = node
item_xml.attrib['name'] = name item['name'] = name
if node: self.iterables.append(item)
item_xml.attrib['node'] = node
self.xml.append(item_xml)
return True return True
return False return False
@ -108,11 +106,9 @@ class DiscoItems(ElementBase):
def get_items(self): def get_items(self):
"""Return all items.""" """Return all items."""
items = set() items = set()
for item_xml in self.findall('{%s}item' % self.namespace): for item in self['substanzas']:
item = (item_xml.attrib['jid'], if isinstance(item, DiscoItem):
item_xml.attrib.get('node'), items.add((item['jid'], item['node'], item['name']))
item_xml.attrib.get('name'))
items.add(item)
return items return items
def set_items(self, items): def set_items(self, items):
@ -132,5 +128,23 @@ class DiscoItems(ElementBase):
def del_items(self): def del_items(self):
"""Remove all items.""" """Remove all items."""
self._items = set() self._items = set()
for item_xml in self.findall('{%s}item' % self.namespace): for item in self['substanzas']:
self.xml.remove(item_xml) if isinstance(item, DiscoItem):
self.xml.remove(item.xml)
class DiscoItem(ElementBase):
name = 'item'
namespace = 'http://jabber.org/protocol/disco#items'
plugin_attrib = name
interfaces = set(('jid', 'node', 'name'))
def get_node(self):
return self._get_attr('node', None)
def get_name(self):
return self._get_attr('name', None)
register_stanza_plugin(DiscoItems, DiscoItem, iterable=True)

View file

@ -13,6 +13,7 @@ from sleekxmpp import Iq
from sleekxmpp.plugins.base import base_plugin from sleekxmpp.plugins.base import base_plugin
from sleekxmpp.xmlstream import register_stanza_plugin from sleekxmpp.xmlstream import register_stanza_plugin
from sleekxmpp.plugins.xep_0059 import Set from sleekxmpp.plugins.xep_0059 import Set
from sleekxmpp.exceptions import XMPPError
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -70,18 +71,29 @@ class ResultIterator():
elif self.start: elif self.start:
self.query[self.interface]['rsm']['after'] = self.start self.query[self.interface]['rsm']['after'] = self.start
try:
r = self.query.send(block=True) r = self.query.send(block=True)
if not r or not r[self.interface]['rsm']['first'] and \ if not r[self.interface]['rsm']['first'] and \
not r[self.interface]['rsm']['last']: not r[self.interface]['rsm']['last']:
raise StopIteration raise StopIteration
if r[self.interface]['rsm']['count'] and \
r[self.interface]['rsm']['first_index']:
count = int(r[self.interface]['rsm']['count'])
first = int(r[self.interface]['rsm']['first_index'])
num_items = len(r[self.interface]['substanzas'])
if first + num_items == count:
raise StopIteration
if self.reverse: if self.reverse:
self.start = r[self.interface]['rsm']['first'] self.start = r[self.interface]['rsm']['first']
else: else:
self.start = r[self.interface]['rsm']['last'] self.start = r[self.interface]['rsm']['last']
return r return r
except XMPPError:
raise StopIteration
class xep_0059(base_plugin): class xep_0059(base_plugin):