diff wokkel/rsm.py @ 1:9d35f88168a1

tmp: update tmp.wokkel.rsm, add tmp.wokkel.mam
author souliane <souliane@mailoo.org>
date Fri, 09 Jan 2015 10:50:11 +0100
parents 09e7c32a6a00
children 4c3e159abf0b
line wrap: on
line diff
--- a/wokkel/rsm.py	Mon Dec 15 12:46:58 2014 +0100
+++ b/wokkel/rsm.py	Fri Jan 09 10:50:11 2015 +0100
@@ -10,7 +10,6 @@
 U{XEP-0059<http://xmpp.org/extensions/xep-0059.html>}.
 """
 
-from twisted.python import log
 from twisted.words.xish import domish
 
 import pubsub
@@ -21,6 +20,18 @@
 NS_RSM = 'http://jabber.org/protocol/rsm'
 
 
+class RSMError(Exception):
+    """
+    RSM error.
+    """
+
+
+class RSMNotFoundError(Exception):
+    """
+    An expected RSM element has not been found.
+    """
+
+
 class RSMRequest():
     """
     A Result Set Management request.
@@ -79,7 +90,7 @@
                                                     name="set",
                                                     uri=NS_RSM).next()
         except StopIteration:
-            return None
+            raise RSMNotFoundError()
 
         request = RSMRequest()
         for elt in list(set_elt.elements()):
@@ -89,22 +100,16 @@
                 setattr(request, elt.name, int(''.join(elt.children)))
 
         if request.max is None:
-            log.err("RSM request is missing its 'max' element!")
+            raise RSMError("RSM request is missing its 'max' element")
 
         return request
 
-    def render(self, element=None):
-        """Render a RSM page request, eventually embed it in the given element.
+    def toElement(self):
+        """
+        Return the DOM representation of this RSM request.
 
-        @param element: request element.
-        @type element: L{domish.Element}
-
-        @return: RSM request element.
         @rtype: L{domish.Element}
         """
-        if element and element.name == 'pubsub' and hasattr(element, 'items'):
-            element.items.attributes['max_items'] = unicode(self.max)
-
         set_elt = domish.Element((NS_RSM, 'set'))
         set_elt.addElement('max').addContent(unicode(self.max))
 
@@ -120,8 +125,22 @@
         if self.after is not None:
             set_elt.addElement('after').addContent(self.after)
 
-        if element:
-            element.addChild(set_elt)
+        return set_elt
+
+    def render(self, element):
+        """Embed the DOM representation of this RSM request in the given element.
+
+        @param element: Element to contain the RSM request.
+        @type element: L{domish.Element}
+
+        @return: RSM request element.
+        @rtype: L{domish.Element}
+        """
+        if element.name == 'pubsub' and hasattr(element, 'items'):
+            element.items.attributes['max_items'] = unicode(self.max)
+
+        set_elt = self.toElement()
+        element.addChild(set_elt)
 
         return set_elt
 
@@ -178,7 +197,7 @@
                                                     name="set",
                                                     uri=NS_RSM).next()
         except StopIteration:
-            return None
+            return RSMNotFoundError()
 
         response = RSMResponse()
         for elt in list(set_elt.elements()):
@@ -190,17 +209,14 @@
                 response.count = int(''.join(elt.children))
 
         if response.count is None:
-            log.err("RSM response is missing its 'count' element!")
+            raise RSMError("RSM response is missing its 'count' element")
 
         return response
 
-    def render(self, parent=None):
-        """Render a RSM page response, eventually embed it in the given element.
+    def toElement(self):
+        """
+        Return the DOM representation of this RSM request.
 
-        @param element: response element.
-        @type element:  L{domish.Element}
-
-        @return: RSM request element.
         @rtype: L{domish.Element}
         """
         set_elt = domish.Element((NS_RSM, 'set'))
@@ -213,9 +229,19 @@
 
             set_elt.addElement('last').addContent(self.last)
 
-        if parent:
-            parent.addChild(set_elt)
+        return set_elt
+
+    def render(self, element):
+        """Embed the DOM representation of this RSM response in the given element.
 
+        @param element: Element to contain the RSM response.
+        @type element:  L{domish.Element}
+
+        @return: RSM request element.
+        @rtype: L{domish.Element}
+        """
+        set_elt = self.toElement()
+        element.addChild(set_elt)
         return set_elt
 
     def toDict(self):
@@ -247,7 +273,10 @@
         self._parameters['items'].append('rsm')
 
     def _parse_rsm(self, verbElement):
-        self.rsm = RSMRequest.parse(verbElement.parent)
+        try:
+            self.rsm = RSMRequest.parse(verbElement.parent)
+        except RSMNotFoundError:
+            self.rsm = None
 
     def _render_rsm(self, verbElement):
         if self.rsm: