changeset 1419:be2df1ddea8e

plugins (groupblog, xep-0277) + tmp(rsm): improved style: - removed external parenthesis in assertions - added blank line after first line of docstrings - replaced "__" prefixes by "_" - renamed variabled named after the reserverd word "max" to "max_"
author Goffi <goffi@goffi.org>
date Wed, 22 Apr 2015 18:30:28 +0200
parents 6adf1b0be609
children 7c0acb966fd6
files src/plugins/plugin_misc_groupblog.py src/plugins/plugin_xep_0277.py src/tmp/wokkel/rsm.py
diffstat 3 files changed, 47 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_misc_groupblog.py	Mon Apr 20 16:46:17 2015 +0200
+++ b/src/plugins/plugin_misc_groupblog.py	Wed Apr 22 18:30:28 2015 +0200
@@ -214,6 +214,7 @@
 
     def _handleCommentsItems(self, items, service, node_identifier):
         """ Convert comments items to groupblog data, and send them as signals
+
         @param items: comments items
         @param service: jid of the PubSub service used
         @param node_identifier: comments node
@@ -261,12 +262,15 @@
 
     def getNodeName(self, publisher):
         """Retrieve the name of publisher's node
+
         @param publisher: publisher's jid
-        @return: node's name (string)"""
+        @return: node's name (string)
+        """
         return NS_NODE_PREFIX + publisher.userhost()
 
     def _publishMblog(self, service, client, access_type, access_list, message, extra):
         """Actually publish the message on the group blog
+
         @param service: jid of the item-access pubsub service
         @param client: SatXMPPClient of the publisher
         @param access_type: one of "PUBLIC", "GROUP", "JID"
@@ -745,7 +749,7 @@
         jids = self._getPublishersJIDs(publishers_type, publishers, client)
         node_ids = [self.getNodeName(publisher) for publisher in jids]
         d_list = yield self.host.plugins["XEP-0060"].subscribeToMany(client.item_access_pubsub, node_ids, profile_key=profile_key)
-        result = yield defer.DeferredList(d_list, consumeErrors=False)
+        yield defer.DeferredList(d_list, consumeErrors=False)
         defer.returnValue(None)
 
     def deleteAllGroupBlogsAndComments(self, profile_key=C.PROF_KEY_NONE):
--- a/src/plugins/plugin_xep_0277.py	Mon Apr 20 16:46:17 2015 +0200
+++ b/src/plugins/plugin_xep_0277.py	Wed Apr 22 18:30:28 2015 +0200
@@ -95,9 +95,9 @@
 
         return (service, node)
 
-    def __removeXHTMLMarkups(self, xhtml):
-        """
-        Remove XHTML markups from the given string.
+    def _removeXHTMLMarkups(self, xhtml):
+        """Remove XHTML markups from the given string.
+
         @param xhtml: the XHTML string to be cleaned
         @return: a Deferred instance for the cleaned string
         """
@@ -142,17 +142,17 @@
 
         for key in ['title', 'content']:  # process the textual elements
             for attr_elt in xpath(entry_elt, key):
-                attr_content = self.__getLXMLInnerContent(attr_elt)
+                attr_content = self._getLXMLInnerContent(attr_elt)
                 if not attr_content.strip():
                     continue  # element with empty value
                 content_type = attr_elt.get('type', 'text').lower()
                 if content_type == 'xhtml':
-                    text = self.__decapsulateExtraNS(attr_content)
+                    text = self._decapsulateExtraNS(attr_content)
                     microblog_data['%s_xhtml' % key] = yield self.host.plugins["TEXT-SYNTAXES"].clean_xhtml(text)
                 else:
                     microblog_data[key] = attr_content
             if key not in microblog_data and ('%s_xhtml' % key) in microblog_data:
-                microblog_data[key] = yield self.__removeXHTMLMarkups(microblog_data['%s_xhtml' % key])
+                microblog_data[key] = yield self._removeXHTMLMarkups(microblog_data['%s_xhtml' % key])
 
         try:  # check for mandatory elements
             microblog_data['id'] = xpath(entry_elt, 'id')[0].text
@@ -198,14 +198,14 @@
 
         defer.returnValue(microblog_data)
 
-    def __getLXMLInnerContent(self, elt):
+    def _getLXMLInnerContent(self, elt):
         """Return the inner content of a lxml.etree.Element. It is not
         trivial because the lxml tostring method would return the full
         content including elt's tag and attributes, and elt.getchildren()
         would skip a text value which is not within an element..."""
-        return self.__getDomishInnerContent(ElementParser()(etree.tostring(elt)))
+        return self._getDomishInnerContent(ElementParser()(etree.tostring(elt)))
 
-    def __getDomishInnerContent(self, elt):
+    def _getDomishInnerContent(self, elt):
         """Return the inner content of a domish.Element."""
         result = ''
         for child in elt.children:
@@ -215,7 +215,7 @@
                 result += child  # child is unicode
         return result
 
-    def __decapsulateExtraNS(self, text):
+    def _decapsulateExtraNS(self, text):
         """Check for XHTML namespace and decapsulate the content so the user
         who wants to modify an entry will see the text that he entered. Also
         this avoids successive encapsulation with a new <div>...</div> at
@@ -223,7 +223,7 @@
         elt = ElementParser()(text)
         if elt.uri != NS_XHTML:
             raise exceptions.DataError(_('Content of type XHTML must declare its namespace!'))
-        return self.__getDomishInnerContent(elt)
+        return self._getDomishInnerContent(elt)
 
     def microblogCB(self, itemsEvent, profile):
         """Callback to "MICROBLOG" PEP event."""
@@ -236,8 +236,10 @@
     @defer.inlineCallbacks
     def data2entry(self, data, profile):
         """Convert a data dict to en entry usable to create an item
+
         @param data: data dict as given by bridge method.
-        @return: deferred which fire domish.Element"""
+        @return: deferred which fire domish.Element
+        """
         _uuid = unicode(uuid.uuid1())
         _entry = atom.Entry()
         _entry.title = ''  # reset the default value which is not empty
@@ -266,7 +268,7 @@
                         setattr(_entry, key, elem)
             if not getattr(_entry, key).text:
                 if hasattr(_entry, '%s_xhtml' % key):
-                    text = yield self.__removeXHTMLMarkups(getattr(_entry, '%s_xhtml' % key).text)
+                    text = yield self._removeXHTMLMarkups(getattr(_entry, '%s_xhtml' % key).text)
                     setattr(_entry, key, text)
         if not _entry.title.text:  # eventually move the data from content to title
             _entry.title = _entry.content.text
@@ -298,6 +300,7 @@
     @defer.inlineCallbacks
     def sendMicroblog(self, data, profile):
         """Send XEP-0277's microblog data
+
         @param data: must include content
         @param profile: profile which send the mood"""
         if 'content' not in data:
@@ -313,6 +316,7 @@
 
     def getLastMicroblogs(self, pub_jid, max_items=10, profile_key=C.PROF_KEY_NONE):
         """Get the last published microblogs
+
         @param pub_jid: jid of the publisher
         @param max_items: how many microblogs we want to get
         @param profile_key: profile key
@@ -326,6 +330,7 @@
 
     def setMicroblogAccess(self, access="presence", profile_key=C.PROF_KEY_NONE):
         """Create a microblog node on PEP with given access
+
         If the node already exists, it change options
         @param access: Node access model, according to xep-0060 #4.5
         @param profile_key: profile key"""
--- a/src/tmp/wokkel/rsm.py	Mon Apr 20 16:46:17 2015 +0200
+++ b/src/tmp/wokkel/rsm.py	Wed Apr 22 18:30:28 2015 +0200
@@ -36,8 +36,8 @@
     """
     A Result Set Management request.
 
-    @ivar max: limit on the number of retrieved items.
-    @itype max: C{int} or C{unicode}
+    @ivar max_: limit on the number of retrieved items.
+    @itype max_: C{int} or C{unicode}
 
     @ivar index: starting index of the requested page.
     @itype index: C{int} or C{unicode}
@@ -49,30 +49,30 @@
     @itype before: C{unicode}
     """
 
-    max = 10
+    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_=None, index=None, after=None, before=None):
+        if max_ is not None:
+            max_ = int(max_)
+            assert max_ >= 0
+            self.max_ = max_
 
         if index is not None:
-            assert(after is None and before is None)
+            assert after is None and before is None
             index = int(index)
-            assert(index >= 0)
+            assert index >= 0
             self.index = index
 
         if after is not None:
-            assert(before is None)
-            assert(isinstance(after, unicode))
+            assert before is None
+            assert isinstance(after, unicode)
             self.after = after
 
         if before is not None:
-            assert(isinstance(before, unicode))
+            assert isinstance(before, unicode)
             self.before = before
 
     @classmethod
@@ -99,8 +99,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
 
@@ -111,7 +111,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))
@@ -137,7 +137,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)
@@ -169,18 +169,18 @@
 
     def __init__(self, count=None, index=None, first=None, last=None):
         if count is not None:
-            assert(isinstance(count, int) and count >= 0)
+            assert isinstance(count, int) and count >= 0
             self.count = count
 
         if index is not None:
-            assert(isinstance(index, int) and index >= 0)
+            assert isinstance(index, int) and index >= 0
             self.index = index
-            assert(isinstance(first, unicode))
+            assert isinstance(first, unicode)
             self.first = first
-            assert(isinstance(last, unicode))
+            assert isinstance(last, unicode)
             self.last = last
         else:
-            assert(first is None and last is None)
+            assert first is None and last is None
 
     @classmethod
     def parse(cls, element):
@@ -372,7 +372,7 @@
                                                           resource, request)
         set_elts = [elt for elt in result if elt.name == 'set']
         if set_elts:
-            assert(len(set_elts) == 1)
+            assert len(set_elts) == 1
             response.addChild(set_elts[0])
 
         return response