[pkg-fso-commits] [SCM] FSO frameworkd Debian packaging branch, master, updated. milestone4-368-g700ab82

Michael 'Mickey' Lauer mickey at vanille-media.de
Mon Feb 2 18:52:06 UTC 2009


The following commit has been merged in the master branch:
commit ae581fa712cbf5821628f51217178a0f99179026
Author: Michael 'Mickey' Lauer <mickey at vanille-media.de>
Date:   Wed Jan 28 15:55:44 2009 +0100

    opimd: raise proper dbus errors, remove some redundancy (more to come), styling fixes

diff --git a/framework/subsystems/opimd/__init__.py b/framework/subsystems/opimd/__init__.py
index 5530e00..f3d6b27 100755
--- a/framework/subsystems/opimd/__init__.py
+++ b/framework/subsystems/opimd/__init__.py
@@ -5,6 +5,8 @@ Open PIM Daemon
 (C) 2008 by Soeren Apel <abraxa at dar-clan.de>
 (C) 2008 Openmoko, Inc.
 GPLv2 or later
+
+Framework plugin factory
 """
 
 MODULE_NAME = "opimd"
diff --git a/framework/subsystems/opimd/backend_manager.py b/framework/subsystems/opimd/backend_manager.py
index a2c2384..3baa5a3 100644
--- a/framework/subsystems/opimd/backend_manager.py
+++ b/framework/subsystems/opimd/backend_manager.py
@@ -1,28 +1,13 @@
-#
-#   Openmoko PIM Daemon
-#   Backend Plugin Manager
-#
-#   http://openmoko.org/
-#   http://pyneo.org/
-#
-#   Copyright (C) 2008 by Soeren Apel (abraxa at dar-clan.de)
-#
-#   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 2 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, write to the Free Software
-#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-#
-
-"""pypimd Backend Plugin Manager"""
+#!/usr/bin/env python
+"""
+Open PIM Daemon
+
+(C) 2008 by Soeren Apel <abraxa at dar-clan.de>
+(C) 2008 Openmoko, Inc.
+GPLv2 or later
+
+Backend plugin manager
+"""
 
 from domain_manager import DomainManager
 from helpers import *
@@ -50,26 +35,18 @@ PIMB_STATUS_CONNECTED = 1
 
 #----------------------------------------------------------------------------#
 
-# D-Bus constant initialization, *must* be done before any D-Bus method decorators are declared
-try:
-    import framework.config
-
-    _DBUS_PATH_SOURCES = DBUS_PATH_BASE_FSO + '/Sources'
-    _DIN_SOURCES = DIN_BASE_FSO + '.Sources'
-    _DIN_SOURCE = DIN_BASE_FSO + '.Source'
-    ENV_MODE = 'FSO'
-
-except ImportError:
-    _DBUS_PATH_SOURCES = DBUS_PATH_BASE_PYNEO + '/Sources'
-    _DIN_SOURCES = DIN_BASE_PYNEO + '.Sources'
-    _DIN_SOURCE = DIN_BASE_PYNEO + '.Source'
-    ENV_MODE = 'pyneo'
+_DBUS_PATH_SOURCES = DBUS_PATH_BASE_FSO + '/Sources'
+_DIN_SOURCES = DIN_BASE_FSO + '.Sources'
+_DIN_SOURCE = DIN_BASE_FSO + '.Source'
+ENV_MODE = 'FSO'
 
-
-# We use a meta class to automaticaly register all the backend subclasses
 #----------------------------------------------------------------------------#
 class BackendMetaClass(type):
 #----------------------------------------------------------------------------#
+    """
+    Meta class to automaticaly register all the backend subclasses.
+    """
+
     def __init__(cls, name, bases, dict):
         super(BackendMetaClass, cls).__init__(name, bases, dict)
         if object in bases:
@@ -170,7 +147,7 @@ class BackendManager(DBusFBObject):
         if (num_id < len(self._backends)):
             backend = self._backends[num_id]
         else:
-            raise InvalidBackendIDError()
+            raise error.InvalidBackendID( "Maximum backend ID is %d" % len(self._backends)-1 )
 
         return backend.name
 
@@ -183,7 +160,7 @@ class BackendManager(DBusFBObject):
         if (num_id < len(self._backends)):
             backend = self._backends[num_id]
         else:
-            raise InvalidBackendIDError()
+            raise error.InvalidBackendID( "Maximum backend ID is %d" % len(self._backends)-1 )
 
         return backend.get_supported_domains()
 
@@ -196,7 +173,6 @@ class BackendManager(DBusFBObject):
         if (num_id < len(self._backends)):
             backend = self._backends[num_id]
         else:
-            raise InvalidBackendIDError()
+            raise error.InvalidBackendID( "Maximum backend ID is %d" % len(self._backends)-1 )
 
         return backend.status
-
diff --git a/framework/subsystems/opimd/helpers.py b/framework/subsystems/opimd/helpers.py
index 6005380..9cd591a 100644
--- a/framework/subsystems/opimd/helpers.py
+++ b/framework/subsystems/opimd/helpers.py
@@ -1,72 +1,91 @@
-#
-#   Openmoko PIM Daemon
-#   Various globally used helpers
-#
-#   http://openmoko.org/
-#   http://pyneo.org/
-#
-#   Copyright (C) 2008 by Soeren Apel (abraxa at dar-clan.de)
-#
-#   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 2 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, write to the Free Software
-#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-#
-
-"""pypimd Helpers"""
+#!/usr/bin/env python
+"""
+Open PIM Daemon
 
+(C) 2008 by Soeren Apel <abraxa at dar-clan.de>
+(C) 2009 Michael 'Mickey' Lauer <mlauer at vanille-media.de>
+(C) 2008-2009 Openmoko, Inc.
+GPLv2 or later
 
+Helpers
+"""
+
+from dbus import DBusException
+
+#----------------------------------------------------------------------------#
 def phone_number_to_tel_uri(phone_num):
+#----------------------------------------------------------------------------#
     """Transforms a regular phone number into a tel URI"""
-    
+
     uri = "tel:"
-    
+
     uri += phone_num
     return uri
 
 
+#----------------------------------------------------------------------------#
 def get_compare_for_tel(tel_value):
+#----------------------------------------------------------------------------#
     """Determines and returns a representation of a tel URI that is comparable to human input"""
-    
+
     # Remove tel:
     res = tel_value[4:]
-    
+
     # Remove +, - and /
     res = res.translate({ord(u'-'):None, ord(u'/'):None})
-    
+
     return res
 
+#----------------------------------------------------------------------------#
+class InvalidBackend( DBusException ):
+#----------------------------------------------------------------------------#
+    """Raised when a backend is either invalid or unsuited for a certain function call"""
+    _dbus_error_name = "org.freesmartphone.PIM.InvalidBackend"
+
+#----------------------------------------------------------------------------#
+class InvalidBackendID( DBusException ):
+#----------------------------------------------------------------------------#
+    """Raised when a submitted backend ID is invalid / out of range"""
+    _dbus_error_name = "org.freesmartphone.PIM.InvalidBackendID"
+
+#----------------------------------------------------------------------------#
+class InvalidContactID( DBusException ):
+#----------------------------------------------------------------------------#
+    """Raised when a submitted contact ID is invalid / out of range"""
+    _dbus_error_name = "org.freesmartphone.PIM.InvalidContactID"
 
 #----------------------------------------------------------------------------#
-class PIMException(Exception):
+class NoMoreContacts( DBusException ):
 #----------------------------------------------------------------------------#
-    """Base class for PIM-specific exceptions"""
-    pass
+    """Raised when there are no more contacts to be listed"""
+    _dbus_error_name = "org.freesmartphone.PIM.NoMoreContacts"
 
 #----------------------------------------------------------------------------#
-class InvalidQueryIDError(PIMException):
+class InvalidQueryID( DBusException ):
 #----------------------------------------------------------------------------#
     """Raised when a submitted query ID is invalid / out of range"""
-    pass
+    _dbus_error_name = "org.freesmartphone.PIM.InvalidQueryID"
 
 #----------------------------------------------------------------------------#
-class InvalidBackendIDError(PIMException):
+class InvalidMessageID( DBusException ):
 #----------------------------------------------------------------------------#
-    """Raised when a submitted backend ID is invalid / out of range"""
-    pass
+    """Raised when a submitted message ID is invalid / out of range"""
+    _dbus_error_name = "org.freesmartphone.PIM.InvalidMessageID"
 
 #----------------------------------------------------------------------------#
-class InvalidBackendError(PIMException):
+class NoMoreMessages( DBusException ):
 #----------------------------------------------------------------------------#
-    """Raised when a backend is either invalid or unsuited for a certain function call"""
-    pass
+    """Raised when there are no more messages to be listed"""
+    _dbus_error_name = "org.freesmartphone.PIM.NoMoreMessages"
 
+#----------------------------------------------------------------------------#
+class UnknownFolder( DBusException ):
+#----------------------------------------------------------------------------#
+    """Raised when a given folder name is unknown"""
+    _dbus_error_name = "org.freesmartphone.PIM.UnknownFolder"
+
+#----------------------------------------------------------------------------#
+class AmbiguousKey( DBusException ):
+#----------------------------------------------------------------------------#
+    """Raised when a given message field name is present more than once and it's unclear which to modify"""
+    _dbus_error_name = "org.freesmartphone.PIM.AmbiguousKey"
diff --git a/framework/subsystems/opimd/pimd_contacts.py b/framework/subsystems/opimd/pimd_contacts.py
index ef30838..1d3eff9 100644
--- a/framework/subsystems/opimd/pimd_contacts.py
+++ b/framework/subsystems/opimd/pimd_contacts.py
@@ -1,29 +1,15 @@
-#
-#   Openmoko PIM Daemon
-#   Contacts Domain Plugin
-#
-#   http://openmoko.org/
-#   http://pyneo.org/
-#
-#   Copyright (C) 2008 by Soeren Apel (abraxa at dar-clan.de)
-#
-#   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 2 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, write to the Free Software
-#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-#
-
-"""pypimd Contacts Domain Plugin
-Establishes the 'contact' PIM domain and handles all related requests"""
+#!/usr/bin/env python
+"""
+Open PIM Daemon
+
+(C) 2008 by Soeren Apel <abraxa at dar-clan.de>
+(C) 2008 Openmoko, Inc.
+GPLv2 or later
+
+Contacts Domain Plugin
+
+Establishes the 'contact' PIM domain and handles all related requests
+"""
 
 from dbus.service import FallbackObject as DBusFBObject
 from dbus.service import signal as dbus_signal
@@ -43,24 +29,15 @@ from opimd import *
 
 from framework.config import busmap
 
+#----------------------------------------------------------------------------#
+
 _DOMAIN_NAME = "Contacts"
 
 _MIN_MATCH_TRESHOLD = 0.75
 
-
-# D-Bus constant initialization, *must* be done before any D-Bus method decorators are declared
-try:
-    import framework.config
-
-    _DBUS_PATH_CONTACTS = DBUS_PATH_BASE_FSO + '/' + _DOMAIN_NAME
-    _DIN_CONTACTS_BASE = DIN_BASE_FSO
-    ENV_MODE = 'FSO'
-
-except ImportError:
-    _DBUS_PATH_CONTACTS = DBUS_PATH_BASE_PYNEO + '/' + _DOMAIN_NAME
-    _DIN_CONTACTS_BASE = DIN_BASE_PYNEO
-    ENV_MODE = 'pyneo'
-
+_DBUS_PATH_CONTACTS = DBUS_PATH_BASE_FSO + '/' + _DOMAIN_NAME
+_DIN_CONTACTS_BASE = DIN_BASE_FSO
+ENV_MODE = 'FSO'
 
 _DBUS_PATH_QUERIES = _DBUS_PATH_CONTACTS + '/Queries'
 
@@ -68,29 +45,10 @@ _DIN_CONTACTS = _DIN_CONTACTS_BASE + '.' + 'Contacts'
 _DIN_ENTRY = _DIN_CONTACTS_BASE + '.' + 'Contact'
 _DIN_QUERY = _DIN_CONTACTS_BASE + '.' + 'ContactQuery'
 
-
-
-#----------------------------------------------------------------------------#
-class InvalidContactIDError(PIMException):
-#----------------------------------------------------------------------------#
-    """Raised when a submitted contact ID is invalid / out of range"""
-    pass
-
-
-
-#----------------------------------------------------------------------------#
-class NoMoreContactsError(PIMException):
-#----------------------------------------------------------------------------#
-    """Raised when there are no more contacts to be listed"""
-    pass
-
-
-
 #----------------------------------------------------------------------------#
 class ContactQueryMatcher(object):
 #----------------------------------------------------------------------------#
     query_obj = None
-#----------------------------------------------------------------------------#
 
     def __init__(self, query):
         """Evaluates a query
@@ -153,7 +111,6 @@ class Contact():
     _fields = None
     _field_idx = None
     _used_backends = None
-#----------------------------------------------------------------------------#
 
     def __init__(self, path):
         """Creates a new Contact instance
@@ -391,7 +348,6 @@ class SingleQueryHandler(object):
     query = None      # The query this handler is processing
     entries = None
     cursors = None    # The next entry we'll serve, depending on the client calling us
-#----------------------------------------------------------------------------#
 
     def __init__(self, query, contacts, dbus_sender):
         """Creates a new SingleQueryHandler instance
@@ -479,7 +435,7 @@ class SingleQueryHandler(object):
         try:
             result = self.entries[self.cursors[dbus_sender]]
         except IndexError:
-            raise NoMoreContactsError()
+            raise NoMoreContacts( "All results have been submitted" )
 
         contact_id = self.entries[self.cursors[dbus_sender]]
         contact = self._contacts[contact_id]
@@ -501,7 +457,7 @@ class SingleQueryHandler(object):
         try:
             result = self.entries[self.cursors[dbus_sender]]
         except IndexError:
-            raise NoMoreContactsError()
+            raise NoMoreContacts( "All results have been submitted" )
 
         contact_id = self.entries[self.cursors[dbus_sender]]
         contact = self._contacts[contact_id]
@@ -531,7 +487,8 @@ class SingleQueryHandler(object):
             try:
                 entry = self.get_result(dbus_sender)
                 result.append(entry)
-            except NoMoreContactsError:
+            except NoMoreContacts:
+                """Don't want to raise an error in that case"""
                 break
 
         return result
@@ -572,8 +529,7 @@ class QueryManager(DBusFBObject):
     _contacts = None
     _next_query_id = None
 
-# Note: _queries must be a dict so we can remove queries without messing up query IDs
-#----------------------------------------------------------------------------#
+    # Note: _queries must be a dict so we can remove queries without messing up query IDs
 
     def __init__(self, contacts):
         """Creates a new QueryManager instance
@@ -621,13 +577,17 @@ class QueryManager(DBusFBObject):
                 # TODO Figure out how relative signals really work
                 # self.ContactAdded(query_id, contact_path)
 
+    def check_query_id_ok( self, num_id ):
+        """
+        Checks whether a query ID is existing. Raises InvalidQueryID, if not.
+        """
+        if not num_id in self._queries:
+            raise InvalidQueryID( "Existing query IDs: %s" % self._queries.keys() )
 
     @dbus_method(_DIN_QUERY, "", "i", rel_path_keyword="rel_path")
     def GetResultCount(self, rel_path):
         num_id = int(rel_path[1:])
-
-        # Make sure the requested query exists
-        if not self._queries.has_key(num_id): raise InvalidQueryIDError()
+        self.check_query_id_ok( num_id )
 
         return self._queries[num_id].get_result_count()
 
@@ -635,9 +595,7 @@ class QueryManager(DBusFBObject):
     @dbus_method(_DIN_QUERY, "", "", rel_path_keyword="rel_path", sender_keyword="sender")
     def Rewind(self, rel_path, sender):
         num_id = int(rel_path[1:])
-
-        # Make sure the requested query exists
-        if not self._queries.has_key(num_id): raise InvalidQueryIDError()
+        self.check_query_id_ok( num_id )
 
         self._queries[num_id].rewind(sender)
 
@@ -645,9 +603,7 @@ class QueryManager(DBusFBObject):
     @dbus_method(_DIN_QUERY, "i", "", rel_path_keyword="rel_path", sender_keyword="sender")
     def Skip(self, num_entries, rel_path, sender):
         num_id = int(rel_path[1:])
-
-        # Make sure the requested query exists
-        if not self._queries.has_key(num_id): raise InvalidQueryIDError()
+        self.check_query_id_ok( num_id )
 
         self._queries[num_id].skip(sender, num_entries)
 
@@ -655,9 +611,7 @@ class QueryManager(DBusFBObject):
     @dbus_method(_DIN_QUERY, "", "s", rel_path_keyword="rel_path", sender_keyword="sender")
     def GetContactPath(self, rel_path, sender):
         num_id = int(rel_path[1:])
-
-        # Make sure the requested query exists
-        if not self._queries.has_key(num_id): raise InvalidQueryIDError()
+        self.check_query_id_ok( num_id )
 
         return self._queries[num_id].get_contact_path(sender)
 
@@ -665,9 +619,7 @@ class QueryManager(DBusFBObject):
     @dbus_method(_DIN_QUERY, "", "a{sv}", rel_path_keyword="rel_path", sender_keyword="sender")
     def GetResult(self, rel_path, sender):
         num_id = int(rel_path[1:])
-
-        # Make sure the requested query exists
-        if not self._queries.has_key(num_id): raise InvalidQueryIDError()
+        self.check_query_id_ok( num_id )
 
         return self._queries[num_id].get_result(sender)
 
@@ -675,9 +627,7 @@ class QueryManager(DBusFBObject):
     @dbus_method(_DIN_QUERY, "i", "aa{sv}", rel_path_keyword="rel_path", sender_keyword="sender")
     def GetMultipleResults(self, num_entries, rel_path, sender):
         num_id = int(rel_path[1:])
-
-        # Make sure the requested query exists
-        if not self._queries.has_key(num_id): raise InvalidQueryIDError()
+        self.check_query_id_ok( num_id )
 
         return self._queries[num_id].get_multiple_results(sender, num_entries)
 
@@ -685,9 +635,7 @@ class QueryManager(DBusFBObject):
     @dbus_method(_DIN_QUERY, "", "", rel_path_keyword="rel_path")
     def Dispose(self, rel_path):
         num_id = int(rel_path[1:])
-
-        # Make sure the requested query exists
-        if not self._queries.has_key(num_id): raise InvalidQueryIDError()
+        self.check_query_id_ok( num_id )
 
         # Make sure no one else references the query handler before we remove our reference to it
         # Otherwise, garbage collection won't actually free its memory
@@ -702,7 +650,6 @@ class ContactDomain(Domain):
     _backends = None
     _contacts = None
     query_manager = None
-#----------------------------------------------------------------------------#
 
     def __init__(self):
         """Creates a new ContactDomain instance"""
@@ -791,12 +738,12 @@ class ContactDomain(Domain):
         result = ""
 
         if not PIMB_CAN_ADD_ENTRY in backend.properties:
-            raise InvalidBackendError()
+            raise InvalidBackend( "Backend properties not including PIMB_CAN_ADD_ENTRY" )
 
         try:
             contact_id = backend.add_contact(contact_data)
         except AttributeError:
-            raise InvalidBackendError()
+            raise InvalidBackend( "Backend does not feature add_contact" )
 
         contact = self._contacts[contact_id]
         result = contact['Path']
@@ -807,7 +754,6 @@ class ContactDomain(Domain):
 
         return result
 
-
     @dbus_method(_DIN_CONTACTS, "a{sv}s", "s")
     def GetSingleContactSingleField(self, query, field_name):
         """Returns the first contact found for a query, making it real easy to query simple things
@@ -851,7 +797,8 @@ class ContactDomain(Domain):
         num_id = int(rel_path[1:])
 
         # Make sure the requested contact exists
-        if num_id >= len(self._contacts): raise InvalidContactIDError()
+        if num_id >= len(self._contacts):
+            raise InvalidContactID()
 
         return self._contacts[num_id].get_content()
 
@@ -861,7 +808,8 @@ class ContactDomain(Domain):
         num_id = int(rel_path[1:])
 
         # Make sure the requested contact exists
-        if num_id >= len(self._contacts): raise InvalidContactIDError()
+        if num_id >= len(self._contacts):
+            raise InvalidContactID()
 
         # Break the string up into a list
         fields = field_list.split(',')
diff --git a/framework/subsystems/opimd/pimd_messages.py b/framework/subsystems/opimd/pimd_messages.py
index e529099..6d54f04 100644
--- a/framework/subsystems/opimd/pimd_messages.py
+++ b/framework/subsystems/opimd/pimd_messages.py
@@ -1,31 +1,16 @@
-#
-#   Openmoko PIM Daemon
-#   Messages Domain Plugin
-#
-#   http://openmoko.org/
-#   http://pyneo.org/
-#
-#   Copyright (C) 2008 by Soeren Apel (abraxa at dar-clan.de)
-#
-#   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 2 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, write to the Free Software
-#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-#
-
-"""pypimd Messages Domain Plugin
-Establishes the 'messages' PIM domain and handles all related requests"""
-
-from dbus import SystemBus
+#!/usr/bin/env python
+"""
+Open PIM Daemon
+
+(C) 2008 by Soeren Apel <abraxa at dar-clan.de>
+(C) 2008 Openmoko, Inc.
+GPLv2 or later
+
+Contacts Domain Plugin
+
+Establishes the 'messages' PIM domain and handles all related requests
+"""
+
 from dbus.service import FallbackObject as DBusFBObject
 from dbus.service import signal as dbus_signal
 from dbus.service import method as dbus_method
@@ -41,24 +26,15 @@ from opimd import *
 
 from framework.config import config, busmap
 
+#----------------------------------------------------------------------------#
+
 _DOMAIN_NAME = "Messages"
 
 _MIN_MATCH_TRESHOLD = 0.75
 
-
-# D-Bus constant initialization, *must* be done before any D-Bus method decorators are declared
-try:
-    import framework.config
-
-    _DBUS_PATH_MESSAGES = DBUS_PATH_BASE_FSO + '/' + _DOMAIN_NAME
-    _DIN_MESSAGES_BASE = DIN_BASE_FSO
-    ENV_MODE = 'FSO'
-
-except ImportError:
-    _DBUS_PATH_MESSAGES = DBUS_PATH_BASE_PYNEO + '/' + _DOMAIN_NAME
-    _DIN_MESSAGES_BASE = DIN_BASE_PYNEO
-    ENV_MODE = 'pyneo'
-
+_DBUS_PATH_MESSAGES = DBUS_PATH_BASE_FSO + '/' + _DOMAIN_NAME
+_DIN_MESSAGES_BASE = DIN_BASE_FSO
+ENV_MODE = 'FSO'
 
 _DBUS_PATH_QUERIES = _DBUS_PATH_MESSAGES + '/Queries'
 _DBUS_PATH_FOLDERS = _DBUS_PATH_MESSAGES + '/Folders'
@@ -68,45 +44,10 @@ _DIN_ENTRY = _DIN_MESSAGES_BASE + '.' + 'Message'
 _DIN_QUERY = _DIN_MESSAGES_BASE + '.' + 'MessageQuery'
 _DIN_FOLDER = _DIN_MESSAGES_BASE + '.' + 'MessageFolder'
 
-
-
-#----------------------------------------------------------------------------#
-class InvalidMessageIDError(PIMException):
-#----------------------------------------------------------------------------#
-    """Raised when a submitted contact ID is invalid / out of range"""
-    pass
-
-
-
-#----------------------------------------------------------------------------#
-class NoMoreMessagesError(PIMException):
-#----------------------------------------------------------------------------#
-    """Raised when there are no more messages to be listed"""
-    pass
-
-
-
-#----------------------------------------------------------------------------#
-class UnknownFolderNameError(PIMException):
-#----------------------------------------------------------------------------#
-    """Raised when a given folder name is unknown"""
-    pass
-
-
-
-#----------------------------------------------------------------------------#
-class AmbiguousKeyError(PIMException):
-#----------------------------------------------------------------------------#
-    """Raised when a given message field name is present more than once and it's unclear which to modify"""
-    pass
-
-
-
 #----------------------------------------------------------------------------#
 class MessageQueryMatcher(object):
 #----------------------------------------------------------------------------#
     query_obj = None
-#----------------------------------------------------------------------------#
 
     def __init__(self, query):
         """Evaluates a query
@@ -152,8 +93,6 @@ class MessageQueryMatcher(object):
 
         return results
 
-
-
 #----------------------------------------------------------------------------#
 class Message():
 #----------------------------------------------------------------------------#
@@ -169,7 +108,6 @@ class Message():
     _fields = None
     _field_idx = None
     _used_backends = None
-#----------------------------------------------------------------------------#
 
     def __init__(self, uri):
         """Creates a new Message instance
@@ -221,7 +159,7 @@ class Message():
             return
 
         if len(field_ids) > 1:
-            raise AmbiguousKeyError()
+            raise AmbiguousKey( "More than one potential field: %s" % field_ids )
 
         field = self._fields[field_ids[0]]
         field[1] = value
@@ -412,7 +350,6 @@ class SingleQueryHandler(object):
     query = None      # The query this handler is processing
     entries = None
     cursors = None    # The next entry we'll serve, depending on the client calling us
-#----------------------------------------------------------------------------#
 
     def __init__(self, query, messages, dbus_sender):
         """Creates a new SingleQueryHandler instance
@@ -500,7 +437,7 @@ class SingleQueryHandler(object):
         try:
             result = self.entries[self.cursors[dbus_sender]]
         except IndexError:
-            raise NoMoreMessagesError()
+            raise NoMoreMessages( "All Messages have been delivered" )
 
         message_id = self.entries[self.cursors[dbus_sender]]
         message = self._messages[message_id]
@@ -522,7 +459,7 @@ class SingleQueryHandler(object):
         try:
             result = self.entries[self.cursors[dbus_sender]]
         except IndexError:
-            raise NoMoreMessagesError()
+            raise NoMoreMessages( "All Messages have been delivered" )
 
         message_id = self.entries[self.cursors[dbus_sender]]
         message = self._messages[message_id]
@@ -552,7 +489,8 @@ class SingleQueryHandler(object):
             try:
                 entry = self.get_result(dbus_sender)
                 result[i] = entry
-            except NoMoreMessagesError:
+            except NoMoreMessages:
+                # we don't want to raise a dbus error here
                 break
 
         return result
@@ -593,8 +531,7 @@ class QueryManager(DBusFBObject):
     _messages = None
     _next_query_id = None
 
-# Note: _queries must be a dict so we can remove queries without messing up query IDs
-#----------------------------------------------------------------------------#
+    # Note: _queries must be a dict so we can remove queries without messing up query IDs
 
     def __init__(self, messages):
         """Creates a new QueryManager instance
@@ -642,13 +579,17 @@ class QueryManager(DBusFBObject):
                 # TODO Figure out how relative signals really work
                 # self.MessageAdded(query_id, message_uri)
 
+    def check_query_id_ok( self, query_id ):
+        """
+        Checks whether a query ID is existing. Raises InvalidQueryID, if not.
+        """
+        if not num_id in self._queries:
+            raise InvalidQueryID( "Existing query IDs: %s" % self._queries.keys() )
 
     @dbus_method(_DIN_QUERY, "", "i", rel_path_keyword="rel_path")
     def GetResultCount(self, rel_path):
         num_id = int(rel_path[1:])
-
-        # Make sure the requested query exists
-        if not self._queries.has_key(num_id): raise InvalidQueryIDError()
+        self.check_query_id_ok( num_id )
 
         return self._queries[num_id].get_result_count()
 
@@ -656,9 +597,7 @@ class QueryManager(DBusFBObject):
     @dbus_method(_DIN_QUERY, "", "", rel_path_keyword="rel_path", sender_keyword="sender")
     def Rewind(self, rel_path, sender):
         num_id = int(rel_path[1:])
-
-        # Make sure the requested query exists
-        if not self._queries.has_key(num_id): raise InvalidQueryIDError()
+        self.check_query_id_ok( num_id )
 
         self._queries[num_id].rewind(sender)
 
@@ -666,9 +605,7 @@ class QueryManager(DBusFBObject):
     @dbus_method(_DIN_QUERY, "i", "", rel_path_keyword="rel_path", sender_keyword="sender")
     def Skip(self, num_entries, rel_path, sender):
         num_id = int(rel_path[1:])
-
-        # Make sure the requested query exists
-        if not self._queries.has_key(num_id): raise InvalidQueryIDError()
+        self.check_query_id_ok( num_id )
 
         self._queries[num_id].skip(sender, num_entries)
 
@@ -676,9 +613,7 @@ class QueryManager(DBusFBObject):
     @dbus_method(_DIN_QUERY, "", "s", rel_path_keyword="rel_path", sender_keyword="sender")
     def GetMessageURI(self, rel_path, sender):
         num_id = int(rel_path[1:])
-
-        # Make sure the requested query exists
-        if not self._queries.has_key(num_id): raise InvalidQueryIDError()
+        self.check_query_id_ok( num_id )
 
         return self._queries[num_id].get_message_uri(sender)
 
@@ -686,9 +621,7 @@ class QueryManager(DBusFBObject):
     @dbus_method(_DIN_QUERY, "", "a{sv}", rel_path_keyword="rel_path", sender_keyword="sender")
     def GetResult(self, rel_path, sender):
         num_id = int(rel_path[1:])
-
-        # Make sure the requested query exists
-        if not self._queries.has_key(num_id): raise InvalidQueryIDError()
+        self.check_query_id_ok( num_id )
 
         return self._queries[num_id].get_result(sender)
 
@@ -696,9 +629,7 @@ class QueryManager(DBusFBObject):
     @dbus_method(_DIN_QUERY, "i", "a{ia{sv}}", rel_path_keyword="rel_path", sender_keyword="sender")
     def GetMultipleResults(self, num_entries, rel_path, sender):
         num_id = int(rel_path[1:])
-
-        # Make sure the requested query exists
-        if not self._queries.has_key(num_id): raise InvalidQueryIDError()
+        self.check_query_id_ok( num_id )
 
         return self._queries[num_id].get_multiple_results(sender, num_entries)
 
@@ -706,9 +637,7 @@ class QueryManager(DBusFBObject):
     @dbus_method(_DIN_QUERY, "", "", rel_path_keyword="rel_path")
     def Dispose(self, rel_path):
         num_id = int(rel_path[1:])
-
-        # Make sure the requested query exists
-        if not self._queries.has_key(num_id): raise InvalidQueryIDError()
+        self.check_query_id_ok( num_id )
 
         # Make sure no one else references the query handler before we remove our reference to it
         # Otherwise, garbage collection won't actually free its memory
@@ -723,7 +652,6 @@ class MessageFolder(DBusFBObject):
     _messages = None   # List of all messages registered with the messages domain
     _entries = None    # List of all messages within this folder
     name = None
-#----------------------------------------------------------------------------#
 
     def __init__(self, messages, folder_id, folder_name):
         self._messages = messages
@@ -793,7 +721,6 @@ class MessageDomain(Domain):
     _messages = None
     _folders = None
     query_manager = None
-#----------------------------------------------------------------------------#
 
     def __init__(self):
         """Creates a new MessageDomain instance"""
@@ -835,7 +762,7 @@ class MessageDomain(Domain):
         for (folder_id, folder) in enumerate(self._folders):
             if folder.name == folder_name: return folder_id
 
-        raise UnknownFolderNameError()
+        raise UnknownFolder( "Valid folders are %s" % list(self._folders) )
 
 
     def register_backend(self, backend):
@@ -871,7 +798,7 @@ class MessageDomain(Domain):
             folder_id = self.get_folder_id_from_name(folder_name)
             folder = self._folders[folder_id]
 
-        except UnknownFolderNameError:
+        except UnknownFolder:
             folder_id = len(self._folders)
             folder = MessageFolder(self._messages, folder_id, folder_name)
             self._folders.append(folder)
@@ -907,12 +834,12 @@ class MessageDomain(Domain):
         result = ""
 
         if not PIMB_CAN_ADD_ENTRY in backend.properties:
-            raise InvalidBackendError()
+            raise InvalidBackend( "This backend does not feature PIMB_CAN_ADD_ENTRY" )
 
         try:
             message_id = backend.add_message(message_data)
         except AttributeError:
-            raise InvalidBackendError()
+            raise InvalidBackend( "This backend does not feature add_message" )
 
         message = self._messages[message_id]
         result = message['URI']
@@ -988,13 +915,17 @@ class MessageDomain(Domain):
     def NewMessage(self, message_URI):
         pass
 
+    def check_message_id_ok( self, num_id ):
+        """
+        Checks whether the given message id is valid. Raises InvalidMessageID, if not.
+        """
+        if num_id >= len(self._messages):
+            raise InvalidMessageID()
 
     @dbus_method(_DIN_ENTRY, "", "a{sv}", rel_path_keyword="rel_path")
     def GetContent(self, rel_path):
         num_id = int(rel_path[1:])
-
-        # Make sure the requested message exists
-        if num_id >= len(self._messages): raise InvalidMessageIDError()
+        self.check_message_id_ok()
 
         return self._messages[num_id].get_content()
 
@@ -1002,9 +933,7 @@ class MessageDomain(Domain):
     @dbus_method(_DIN_ENTRY, "s", "a{sv}", rel_path_keyword="rel_path")
     def GetMultipleFields(self, field_list, rel_path):
         num_id = int(rel_path[1:])
-
-        # Make sure the requested message exists
-        if num_id >= len(self._messages): raise InvalidMessageIDError()
+        self.check_message_id_ok()
 
         # Break the string up into a list
         fields = field_list.split(',')
@@ -1025,9 +954,7 @@ class MessageDomain(Domain):
         @param new_folder_name Name of new folder
         @param rel_path Relative part of D-Bus object path, e.g. '/4'"""
         num_id = int(rel_path[1:])
-
-        # Make sure the requested message exists
-        if num_id >= len(self._messages): raise InvalidMessageIDError()
+        self.check_message_id_ok()
 
         message = self._messages[num_id]
 

-- 
FSO frameworkd Debian packaging



More information about the pkg-fso-commits mailing list