changeset 471:6cd04adddaea

core: exceptions moved to core plugin group blog: group blog now manage public microblogs
author Goffi <goffi@goffi.org>
date Wed, 04 Apr 2012 00:06:44 +0200 (2012-04-03)
parents 5c916b99d0f6
children 813a869315dc
files frontends/src/bridge/DBus.py src/bridge/bridge_constructor/dbus_frontend_template.py src/core/exceptions.py src/core/sat_main.py src/core/xmpp.py src/exceptions.py src/plugins/plugin_misc_groupblog.py src/plugins/plugin_misc_maildir.py
diffstat 8 files changed, 80 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/bridge/DBus.py	Sun Apr 01 19:48:31 2012 +0200
+++ b/frontends/src/bridge/DBus.py	Wed Apr 04 00:06:44 2012 +0200
@@ -243,8 +243,8 @@
     def getLastMicroblogs(self, jid, max_items, profile_key, callback=None, errback=None):
         return self.db_plugin_iface.getLastMicroblogs(jid, max_items, profile_key, reply_handler=callback, error_handler=errback)
 
-    def sendGroupBlog(self, groups, message, profile_key):
-        return self.db_plugin_iface.sendGroupBlog(groups, message, profile_key)
+    def sendGroupBlog(self, access_type, access_list, message, profile_key='@DEFAULT@'):
+        return self.db_plugin_iface.sendGroupBlog(access_type, access_list, message, profile_key)
     
     def getLastGroupBlogs(self, jid, max_items, profile_key, callback=None, errback=None):
         return self.db_plugin_iface.getLastGroupBlogs(jid, max_items, profile_key, reply_handler=callback, error_handler=errback)
--- a/src/bridge/bridge_constructor/dbus_frontend_template.py	Sun Apr 01 19:48:31 2012 +0200
+++ b/src/bridge/bridge_constructor/dbus_frontend_template.py	Wed Apr 04 00:06:44 2012 +0200
@@ -136,8 +136,8 @@
     def getLastMicroblogs(self, jid, max_items, profile_key, callback=None, errback=None):
         return self.db_plugin_iface.getLastMicroblogs(jid, max_items, profile_key, reply_handler=callback, error_handler=errback)
 
-    def sendGroupBlog(self, groups, message, profile_key):
-        return self.db_plugin_iface.sendGroupBlog(groups, message, profile_key)
+    def sendGroupBlog(self, access_type, access_list, message, profile_key='@DEFAULT@'):
+        return self.db_plugin_iface.sendGroupBlog(access_type, access_list, message, profile_key)
     
     def getLastGroupBlogs(self, jid, max_items, profile_key, callback=None, errback=None):
         return self.db_plugin_iface.getLastGroupBlogs(jid, max_items, profile_key, reply_handler=callback, error_handler=errback)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/core/exceptions.py	Wed Apr 04 00:06:44 2012 +0200
@@ -0,0 +1,23 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+"""
+SàT Exceptions
+Copyright (C) 2011  Jérôme Poisson (goffi@goffi.org)
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+"""
+
+class ProfileUnknownError(Exception):
+    pass
--- a/src/core/sat_main.py	Sun Apr 01 19:48:31 2012 +0200
+++ b/src/core/sat_main.py	Wed Apr 04 00:06:44 2012 +0200
@@ -43,6 +43,7 @@
 import os.path
 
 from sat.core import xmpp
+from sat.core.exceptions import ProfileUnknownError
 from sat.memory.memory import Memory
 from sat.tools.xml_tools import tupleList2dataForm
 from sat.tools.misc import TriggerManager
@@ -213,7 +214,7 @@
         profile = self.memory.getProfileName(profile_key)
         if not profile:
             error (_('Trying to connect a non-exsitant profile'))
-            return defer.fail()
+            raise ProfileUnknownError(profile_key)
         
         if (self.isConnected(profile)):
             info(_("already connected !"))
--- a/src/core/xmpp.py	Sun Apr 01 19:48:31 2012 +0200
+++ b/src/core/xmpp.py	Wed Apr 04 00:06:44 2012 +0200
@@ -187,7 +187,7 @@
         bare_jid = item.jid.userhost()
         self._jids[bare_jid] = item
         for group in item.groups:
-            self._groups.get(group,set()).add(bare_jid)
+            self._groups.setdefault(group,set()).add(bare_jid)
         self.host.bridge.newContact(item.jid.full(), self.getAttributes(item), item.groups, self.parent.profile)
     
     def onRosterRemove(self, entity):
--- a/src/exceptions.py	Sun Apr 01 19:48:31 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,23 +0,0 @@
-#!/usr/bin/python
-# -*- coding: utf-8 -*-
-
-"""
-SàT Exceptions
-Copyright (C) 2011  Jérôme Poisson (goffi@goffi.org)
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
-
-class ProfileUnknownError(Exception):
-    pass
--- a/src/plugins/plugin_misc_groupblog.py	Sun Apr 01 19:48:31 2012 +0200
+++ b/src/plugins/plugin_misc_groupblog.py	Wed Apr 04 00:06:44 2012 +0200
@@ -62,6 +62,12 @@
 class NoCompatiblePubSubServerFound(Exception):
     pass
 
+class BadAccessTypeError(Exception):
+    pass
+
+class BadAccessListError(Exception):
+    pass
+
 class GroupBlog():
     """This class use a SàT PubSub Service to manage access on microblog"""
 
@@ -69,24 +75,13 @@
         info(_("Group blog plugin initialization"))
         self.host = host
 
-        host.bridge.addMethod("sendGroupBlog", ".plugin", in_sign='asss', out_sign='',
-                               method=self.sendGroupBlog,
-                               doc = { 'summary':"Send a microblog to a list of groups",
-                                       'param_0':'list of groups which can read the microblog',
-                                       'param_1':'text to send',
-                                       'param_2':'%(doc_profile)s'
-                                     })
+        host.bridge.addMethod("sendGroupBlog", ".plugin", in_sign='sasss', out_sign='',
+                               method=self.sendGroupBlog)
        
         host.bridge.addMethod("getLastGroupBlogs", ".plugin",
                               in_sign='sis', out_sign='aa{ss}',
                               method=self.getLastGroupBlogs,
-                              async = True,
-                              doc = { 'summary':'retrieve items',
-                                      'param_0':'jid: publisher of wanted microblog',
-                                      'param_1':'max_items: see XEP-0060 #6.5.7',
-                                      'param_2':'%(doc_profile)s',
-                                      'return':'list of microblog data (dict)'
-                                    })
+                              async = True)
 
         host.bridge.addMethod("getMassiveLastGroupBlogs", ".plugin",
                               in_sign='sasis', out_sign='a{saa{ss}}',
@@ -129,40 +124,61 @@
         defer.returnValue((profile, client))
 
 
-    def _publishMblog(self, service, groups, message, client):
+    def _publishMblog(self, service, client, access_type, access_list, message):
         """Actually publish the message on the group blog
         @param service: jid of the item-access pubsub service
-        @param groups: set of groups allowed to see the item
+        @param client: SatXMPPClient of the published
+        @param access_type: one of "PUBLIC", "GROUP", "JID"
+        @param access_list: set of entities (empty list for all, groups or jids) allowed to see the item
         @param message: message to publish
-        @param client: SatXMPPClient of the published"""
+        """
         mblog_item = self.host.plugins["XEP-0277"].data2entry({'content':message}, client.profile)
         form = data_form.Form('submit', formNamespace=NS_PUBSUB_ITEM_CONFIG)
-        field = data_form.Field('list-multi', OPT_ROSTER_GROUPS_ALLOWED, values=groups)
-        form.addField(field)
-        mblog_item.addChild(form.toElement())
+        if access_type == "PUBLIC":
+            if access_list:
+                raise BadAccessListError("access_list must be empty for PUBLIC access")
+        elif access_type == "GROUP":
+            field = data_form.Field('list-multi', OPT_ROSTER_GROUPS_ALLOWED, values=access_list)
+            form.addField(field)
+            mblog_item.addChild(form.toElement())
+        elif access_type == "JID":
+            raise NotImplementedError
+        else:
+            error(_("Unknown access_type"))
+            raise BadAccessTypeError
         defer_blog = self.host.plugins["XEP-0060"].publish(service, client.jid.userhost(), items=[mblog_item], profile_key=client.profile)
         defer_blog.addErrback(self._mblogPublicationFailed)
 
     def _mblogPublicationFailed(self, failure):
         #TODO
-        pass
+        return failure
 
-    def sendGroupBlog(self, groups, message, profile_key='@DEFAULT@'):
-        """Publish a microblog to the node associated to the groups
-        If the node doesn't exist, it is created, then the message is posted
-        @param groups: list of groups allowed to retrieve the microblog
+    def sendGroupBlog(self, access_type, access_list, message, profile_key='@DEFAULT@'):
+        """Publish a microblog with given item access
+        @param access_type: one of "PUBLIC", "GROUP", "JID"
+        @param access_list: list of authorized entity (empty list for PUBLIC ACCESS,
+                            list of groups or list of jids) for this item
         @param message: microblog
         @profile_key: %(doc_profile)s
         """
         print "sendGroupBlog"
-
         def initialised(result):
             profile, client = result
-            _groups = set(groups).intersection(client.roster.getGroups()) #We only keep group which actually exist
-            #TODO: send an error signal if user want to post to non existant groups
+            if access_type == "PUBLIC":
+                if access_list:
+                    raise Exception("Publishers list must be empty when getting microblogs for all contacts")
+                self._publishMblog(client.item_access_pubsub, client, "PUBLIC", [], message)
+            elif access_type == "GROUP":
+                _groups = set(access_list).intersection(client.roster.getGroups()) #We only keep group which actually exist
+                if not _groups:
+                    raise BadAccessListError("No valid group")
+                self._publishMblog(client.item_access_pubsub, client, "GROUP", _groups, message)
+            elif access_type == "JID":
+                raise NotImplementedError
+            else:
+                error(_("Unknown access type"))
+                raise BadAccessTypeError 
             
-            self._publishMblog(client.item_access_pubsub, _groups, message, client)
-
         self.initialise(profile_key).addCallback(initialised)
 
 
@@ -170,10 +186,9 @@
     def getLastGroupBlogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@'):
         """Get the last published microblogs
         @param pub_jid: jid of the publisher
-        @param max_items: how many microblogs we want to get
+        @param max_items: how many microblogs we want to get (see XEP-0060 #6.5.7)
         @param profile_key: profile key
-        @param callback: used for the async answer
-        @param errback: used for the async answer
+        @return: list of microblog data (dict)
         """
         
         def initialised(result):
--- a/src/plugins/plugin_misc_maildir.py	Sun Apr 01 19:48:31 2012 +0200
+++ b/src/plugins/plugin_misc_maildir.py	Wed Apr 04 00:06:44 2012 +0200
@@ -33,7 +33,7 @@
 from cStringIO import StringIO
 from twisted.internet import reactor
 import pdb
-from sat.exceptions import *
+from sat.core.exceptions import ProfileUnknownError
 from sat.memory.persistent import PersistentBinaryDict
 
 
@@ -107,7 +107,7 @@
         @param observer: method to call when a NewMessage arrive"""
         profile = self.host.memory.getProfileName(profile_key)
         if not profile:
-            raise ProfileUnknownError
+            raise ProfileUnknownError(profile_key)
         if not self.__mailboxes[profile].has_key(boxname):
             self.__mailboxes[profile][boxname]=MailboxUser(self, boxname, observer, profile=profile)
         else: