changeset 1423:882e5fabf68c

plugin groupblog, tmp (mam, rsm): some style improvments/fixes: - renamed variables nammed after reserved word/module - removed class variables which are not needed - removed external parenthesis from asserts - in mam.MAMPrefs, removed default value for default argument, as None can't be used - (groupblog) extended docstring for DeferredItems and DeferredItemsFromMany
author Goffi <goffi@goffi.org>
date Thu, 23 Apr 2015 13:35:21 +0200
parents be1fccf4854d
children 2d8fccec84e8
files src/plugins/plugin_misc_groupblog.py src/tmp/wokkel/mam.py src/tmp/wokkel/rsm.py
diffstat 3 files changed, 49 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_misc_groupblog.py	Thu Apr 23 10:57:40 2015 +0200
+++ b/src/plugins/plugin_misc_groupblog.py	Thu Apr 23 13:35:21 2015 +0200
@@ -494,33 +494,33 @@
 
     ## get ##
 
-    def _getOrCountComments(self, items, max=0, profile_key=C.PROF_KEY_NONE):
+    def _getOrCountComments(self, items, max_=0, profile_key=C.PROF_KEY_NONE):
         """Get and/or count the comments of the given items.
 
         @param items (list): items to consider.
-        @param max (int): maximum number of comments to get, if 0 only count
+        @param max_ (int): maximum number of comments to get, if 0 only count
             them. The count is set to the item data of key "comments_count".
         @param profile_key (str): %(doc_profile_key)s
         @return: a deferred list of:
-            - if max == 0: microblog data
+            - if max_ == 0: microblog data
             - else: couple (dict, (list[dict], dict)) containing:
                 - microblog data (main item)
                 - couple (comments data, RSM response data for the comments)
         """
         def comments_cb(comments_data, entry):
             entry['comments_count'] = comments_data[1]['count']
-            return (entry, comments_data) if max > 0 else entry
+            return (entry, comments_data) if max_ > 0 else entry
 
-        assert(max >= 0)
+        assert max_ >= 0
         d_list = []
         for entry in items:
             if entry.get('comments', False):
-                comments_rsm = {'max': max}
+                comments_rsm = {'max': max_}
                 d = self.getGroupBlogComments(entry['comments_service'], entry['comments_node'], rsm=comments_rsm, profile_key=profile_key)
                 d.addCallback(comments_cb, entry)
                 d_list.append(d)
             else:
-                if max > 0:
+                if max_ > 0:
                     d_list.append(defer.succeed((entry, ([], {}))))
                 else:
                     d_list.append(defer.succeed(entry))
@@ -588,7 +588,7 @@
         """
         if max_comments is None:
             max_comments = MAX_COMMENTS
-        assert(max_comments > 0)  # otherwise the return signature is not the same
+        assert max_comments > 0  # otherwise the return signature is not the same
         return self._getGroupBlogs(pub_jid_s, item_ids=item_ids, rsm=rsm, max_comments=max_comments, profile_key=profile_key)
 
     def getGroupBlogsAtom(self, pub_jid_s, rsm=None, profile_key=C.PROF_KEY_NONE):
@@ -900,7 +900,7 @@
         self.profile_key = profile_key
 
     def get(self, node, item_ids=None, sub_id=None, rsm=None):
-        """
+        """Retrieve and process a page of pubsub items
 
         @param node (str): node identifier.
         @param item_ids (list[str]): list of items identifiers.
@@ -917,7 +917,7 @@
             profile, client = result
             rsm_ = wokkel_rsm.RSMRequest(**rsm)
             d = self.parent.host.plugins["XEP-0060"].getItems(client.item_access_pubsub,
-                                                              node, rsm_.max_,
+                                                              node, rsm_.max,
                                                               item_ids, sub_id, rsm_,
                                                               profile_key=profile)
 
@@ -948,7 +948,8 @@
         return {publisher: self.parent.getNodeName(publisher) for publisher in jids}
 
     def get(self, publishers_type, publishers, sub_id=None, rsm=None):
-        """
+        """Retrieve and process a page of pubsub items
+
         @param publishers_type (str): type of the list of publishers (one of "GROUP" or "JID" or "ALL")
         @param publishers (list): list of publishers, according to publishers_type (list of groups or list of jids)
         @param sub_id (str): optional subscription identifier.
--- a/src/tmp/wokkel/mam.py	Thu Apr 23 10:57:40 2015 +0200
+++ b/src/tmp/wokkel/mam.py	Thu Apr 23 13:35:21 2015 +0200
@@ -85,15 +85,11 @@
     @itype form: C{unicode}
     """
 
-    form = None
-    rsm = None
-    node = None
-
-    def __init__(self, form=None, rsm=None, node=None):
+    def __init__(self, form=None, rsm_=None, node=None):
         if form is not None:
-            assert(form.formType == 'submit')
+            assert form.formType == 'submit'
         self.form = form
-        self.rsm = rsm
+        self.rsm = rsm_
         self.node = node
 
     @classmethod
@@ -141,7 +137,7 @@
         @return: MAM request element.
         @rtype: L{Element<twisted.words.xish.domish.Element>}
         """
-        assert(parent.name == 'iq')
+        assert parent.name == 'iq'
         mam_elt = self.toElement()
         parent.addChild(mam_elt)
         return mam_elt
@@ -152,7 +148,7 @@
     A Message Archive Management <prefs/> request.
 
     @param default: A value in ('always', 'never', 'roster').
-    @type : C{unicode}
+    @type : C{unicode} or C{None}
 
     @param always (list): A list of JID instances.
     @type always: C{list}
@@ -161,24 +157,19 @@
     @type never: C{list}
     """
 
-    default = None
-    always = None
-    never = None
-
-    def __init__(self, default=None, always=None, never=None):
-        if default:
-            assert(default in ('always', 'never', 'roster'))
-            self.default = default
-        if always:
-            assert(isinstance(always, list))
-            self.always = always
+    def __init__(self, default, always=None, never=None):
+        assert default in ('always', 'never', 'roster')
+        self.default = default
+        if always is not None:
+            assert isinstance(always, list)
         else:
-            self.always = []
-        if never:
-            assert(isinstance(never, list))
-            self.never = never
+            always = []
+        self.always = always
+        if never is not None:
+            assert isinstance(never, list)
         else:
-            self.never = []
+            never = []
+        self.never = never
 
     @classmethod
     def parse(cls, element):
@@ -229,7 +220,7 @@
         @return: MAM request element.
         @rtype: L{Element<twisted.words.xish.domish.Element>}
         """
-        assert(parent.name == 'iq')
+        assert parent.name == 'iq'
         mam_elt = self.toElement()
         parent.addChild(mam_elt)
         return mam_elt
@@ -329,7 +320,7 @@
         @rtype: L{Deferred<twisted.internet.defer.Deferred>}
         """
         # http://xmpp.org/extensions/xep-0313.html#prefs
-        assert(default is not None)
+        assert default is not None
         iq = IQ(self.xmlstream, 'set')
         MAMPrefs(default, always, never).render(iq)
         if sender is not None:
@@ -408,14 +399,13 @@
                                },
                       }
 
-    extra_filters = {}
-
     def __init__(self, resource):
         """
         @param resource: instance implementing IMAMResource
         @type resource: L{object}
         """
         self.resource = resource
+        self.extra_filters = {}
 
     def connectionInitialized(self):
         """
--- a/src/tmp/wokkel/rsm.py	Thu Apr 23 10:57:40 2015 +0200
+++ b/src/tmp/wokkel/rsm.py	Thu Apr 23 13:35:21 2015 +0200
@@ -53,7 +53,7 @@
     @itype max_: C{int} or C{unicode}
 
     @ivar index: starting index of the requested page.
-    @itype index: C{int} or C{unicode}
+    @itype index: C{int} or C{unicode} or C{None}
 
     @ivar after: ID of the element immediately preceding the page.
     @itype after: C{unicode}
@@ -62,31 +62,25 @@
     @itype before: C{unicode}
     """
 
-    max_ = 10
-    index = None
-    after = None
-    before = None
-
-    def __init__(self, max_=None, index=None, after=None, before=None):
-        if max_ is not None:
-            max_ = int(max_)
-            assert max_ >= 0
-            self.max_ = max_
+    def __init__(self, max_=10, index=None, after=None, before=None):
+        max_ = int(max_)
+        assert max_ >= 0
+        self.max = max_
 
         if index is not None:
             assert after is None and before is None
             index = int(index)
             assert index >= 0
-            self.index = index
+        self.index = index
 
         if after is not None:
             assert before is None
             assert isinstance(after, unicode)
-            self.after = after
+        self.after = after
 
         if before is not None:
             assert isinstance(before, unicode)
-            self.before = before
+        self.before = before
 
     @classmethod
     def parse(cls, element):
@@ -112,8 +106,8 @@
             elif elt.name in ('max', 'index'):
                 setattr(request, elt.name, int(''.join(elt.children)))
 
-        if request.max_ is None:
-            raise RSMError("RSM request is missing its 'max_' element")
+        if request.max is None:
+            raise RSMError("RSM request is missing its 'max' element")
 
         return request
 
@@ -124,7 +118,7 @@
         @rtype: L{domish.Element}
         """
         set_elt = domish.Element((NS_RSM, 'set'))
-        set_elt.addElement('max').addContent(unicode(self.max_))
+        set_elt.addElement('max').addContent(unicode(self.max))
 
         if self.index is not None:
             set_elt.addElement('index').addContent(unicode(self.index))
@@ -150,7 +144,7 @@
         @rtype: L{domish.Element}
         """
         if element.name == 'pubsub' and hasattr(element, 'items'):
-            element.items.attributes['max_items'] = unicode(self.max_)
+            element.items.attributes['max_items'] = unicode(self.max)
 
         set_elt = self.toElement()
         element.addChild(set_elt)
@@ -175,25 +169,18 @@
     @itype last: C{unicode}
     """
 
-    count = 0
-    index = None
-    first = None
-    last = None
-
-    def __init__(self, count=None, index=None, first=None, last=None):
-        if count is not None:
-            assert isinstance(count, int) and count >= 0
-            self.count = count
-
+    def __init__(self, count=0, index=None, first=None, last=None):
+        assert isinstance(count, int) and count >= 0
+        self.count = count
         if index is not None:
             assert isinstance(index, int) and index >= 0
-            self.index = index
             assert isinstance(first, unicode)
-            self.first = first
             assert isinstance(last, unicode)
-            self.last = last
         else:
             assert first is None and last is None
+        self.index = index
+        self.first = first
+        self.last = last
 
     @classmethod
     def parse(cls, element):