diff src/core/sat_main.py @ 1624:7e749e8eefd0

core: fixed launchAction: - getClient now raise a NotFound error if client is not available - errors ConnectedProfileError, NotConnectedProfileError anf ProfileKeyUnknownError renamed for consistency (all profile error must prefixed by Profile) - launchAction fixed, the case where client is not available is handled - kept actions are actuall1y removed on launchAction
author Goffi <goffi@goffi.org>
date Wed, 18 Nov 2015 11:06:24 +0100
parents 5b24d6bf5d15
children 63cef4dbf2a4
line wrap: on
line diff
--- a/src/core/sat_main.py	Tue Nov 17 21:29:03 2015 +0100
+++ b/src/core/sat_main.py	Wed Nov 18 11:06:24 2015 +0100
@@ -455,11 +455,19 @@
 
     def getClient(self, profile_key):
         """Convenient method to get client from profile key
-        @return: client or None if it doesn't exist"""
+
+        @return: client or None if it doesn't exist
+        @raise exceptions.ProfileKeyUnknown: the profile or profile key doesn't exist
+        @raise exceptions.NotFound: client is not available
+            This happen if profile has not been use yet
+        """
         profile = self.memory.getProfileName(profile_key)
         if not profile:
-            raise exceptions.ProfileKeyUnknownError
-        return self.profiles[profile]
+            raise exceptions.ProfileKeyUnknown
+        try:
+            return self.profiles[profile]
+        except KeyError:
+            raise exceptions.NotFound
 
     def getClients(self, profile_key):
         """Convenient method to get list of clients from profile key (manage list through profile_key like C.PROF_KEY_ALL)
@@ -474,7 +482,7 @@
         if profile == C.PROF_KEY_ALL:
             return self.profiles.values()
         elif profile.count('@') > 1:
-            raise exceptions.ProfileKeyUnknownError
+            raise exceptions.ProfileKeyUnknown
         return [self.profiles[profile]]
 
     def _getConfig(self, section, name):
@@ -925,13 +933,23 @@
                 - C.BOOL_TRUE
                 - C.BOOL_FALSE
         """
-        client = self.getClient(profile_key)
         try:
-            action_tuple = client.actions[callback_id]
-        except KeyError:
-            pass
+            client = self.getClient(profile_key)
+        except exceptions.NotFound:
+            # client is not available yet
+            profile = self.memory.getProfileName(profile_key)
+            if not profile:
+                raise exceptions.ProfileUnknownError(_('trying to launch action with a non-existant profile'))
         else:
-            action_tuple[-1].cancel() # the last item is the action timer
+            profile = client.profile
+            # we check if the action is kept, and remove it
+            try:
+                action_tuple = client.actions[callback_id]
+            except KeyError:
+                pass
+            else:
+                action_tuple[-1].cancel() # the last item is the action timer
+                del client.actions[callback_id]
 
         try:
             callback, args, kwargs = self._cb_map[callback_id]
@@ -943,7 +961,7 @@
                 raise exceptions.DataError("Required data for this callback is missing")
             args,kwargs=list(args)[:],kwargs.copy() # we don't want to modify the original (kw)args
             args.insert(0, data)
-            kwargs["profile"] = client.profile
+            kwargs["profile"] = profile
             del kwargs["with_data"]
 
         if kwargs.pop('one_shot', False):