From 5ab77c745270d7d5c016c1dc7ef2a82533a4b16e Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Thu, 17 Jul 2014 14:19:04 +0200 Subject: Rename to slixmpp --- slixmpp/plugins/xep_0059/__init__.py | 18 +++++ slixmpp/plugins/xep_0059/rsm.py | 145 +++++++++++++++++++++++++++++++++++ slixmpp/plugins/xep_0059/stanza.py | 108 ++++++++++++++++++++++++++ 3 files changed, 271 insertions(+) create mode 100644 slixmpp/plugins/xep_0059/__init__.py create mode 100644 slixmpp/plugins/xep_0059/rsm.py create mode 100644 slixmpp/plugins/xep_0059/stanza.py (limited to 'slixmpp/plugins/xep_0059') diff --git a/slixmpp/plugins/xep_0059/__init__.py b/slixmpp/plugins/xep_0059/__init__.py new file mode 100644 index 00000000..21a0e5c9 --- /dev/null +++ b/slixmpp/plugins/xep_0059/__init__.py @@ -0,0 +1,18 @@ +""" + Slixmpp: The Slick XMPP Library + Copyright (C) 2010 Nathanael C. Fritz, Erik Reuterborg Larsson + This file is part of Slixmpp. + + See the file LICENSE for copying permission. +""" + +from slixmpp.plugins.base import register_plugin + +from slixmpp.plugins.xep_0059.stanza import Set +from slixmpp.plugins.xep_0059.rsm import ResultIterator, XEP_0059 + + +register_plugin(XEP_0059) + +# Retain some backwards compatibility +xep_0059 = XEP_0059 diff --git a/slixmpp/plugins/xep_0059/rsm.py b/slixmpp/plugins/xep_0059/rsm.py new file mode 100644 index 00000000..5876a9aa --- /dev/null +++ b/slixmpp/plugins/xep_0059/rsm.py @@ -0,0 +1,145 @@ +""" + Slixmpp: The Slick XMPP Library + Copyright (C) 2010 Nathanael C. Fritz, Erik Reuterborg Larsson + This file is part of Slixmpp. + + See the file LICENSE for copying permission. +""" + +import logging + +import slixmpp +from slixmpp import Iq +from slixmpp.plugins import BasePlugin, register_plugin +from slixmpp.xmlstream import register_stanza_plugin +from slixmpp.plugins.xep_0059 import stanza, Set +from slixmpp.exceptions import XMPPError + + +log = logging.getLogger(__name__) + + +class ResultIterator(): + + """ + An iterator for Result Set Managment + """ + + def __init__(self, query, interface, results='substanzas', amount=10, + start=None, reverse=False): + """ + Arguments: + query -- The template query + interface -- The substanza of the query, for example disco_items + results -- The query stanza's interface which provides a + countable list of query results. + 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.results = results + self.reverse = reverse + self._stop = False + + 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. + """ + if self._stop: + raise StopIteration + 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 + + try: + r = self.query.send(block=True) + + if not r[self.interface]['rsm']['first'] and \ + not r[self.interface]['rsm']['last']: + 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][self.results]) + if first + num_items == count: + self._stop = True + + if self.reverse: + self.start = r[self.interface]['rsm']['first'] + else: + self.start = r[self.interface]['rsm']['last'] + + return r + except XMPPError: + raise StopIteration + + +class XEP_0059(BasePlugin): + + """ + XEP-0050: Result Set Management + """ + + name = 'xep_0059' + description = 'XEP-0059: Result Set Management' + dependencies = set(['xep_0030']) + stanza = stanza + + def plugin_init(self): + """ + Start the XEP-0059 plugin. + """ + register_stanza_plugin(self.xmpp['xep_0030'].stanza.DiscoItems, + self.stanza.Set) + + def plugin_end(self): + self.xmpp['xep_0030'].del_feature(feature=Set.namespace) + + def session_bind(self, jid): + self.xmpp['xep_0030'].add_feature(Set.namespace) + + def iterate(self, stanza, interface, results='substanzas'): + """ + 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. + results -- The name of the interface containing the + query results (typically just 'substanzas'). + """ + return ResultIterator(stanza, interface, results) diff --git a/slixmpp/plugins/xep_0059/stanza.py b/slixmpp/plugins/xep_0059/stanza.py new file mode 100644 index 00000000..e2701af4 --- /dev/null +++ b/slixmpp/plugins/xep_0059/stanza.py @@ -0,0 +1,108 @@ +""" + Slixmpp: The Slick XMPP Library + Copyright (C) 2010 Nathanael C. Fritz, Erik Reuterborg Larsson + This file is part of Slixmpp. + + See the file LICENSE for copying permission. +""" + +from slixmpp.xmlstream import ElementBase, ET +from slixmpp.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: + + + + 2 + + + + + + + + + + conference.example.com + pubsub.example.com + + + + + Stanza Interface: + first_index -- The index attribute of + 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 and + creates the element if it doesn't exist + get_first_index -- Returns the value of the index + attribute for + del_first_index -- Removes the index attribute for + but keeps the element + set_before -- Sets the value of , if the value is True + then the element will be created without a value + get_before -- Returns the value of , 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 + elif 'index' in fi.attrib: + 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 is 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 -- cgit v1.2.3