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
This commit is contained in:
Goffi 2015-11-18 11:06:24 +01:00
commit ad973c6a2c
4 changed files with 34 additions and 16 deletions

View file

@ -33,16 +33,16 @@ class ProfileNotSetError(Exception):
pass
class ConnectedProfileError(Exception):
class ProfileConnected(Exception):
"""This error is raised when trying to delete a connected profile."""
pass
class NotConnectedProfileError(Exception):
class ProfileNotConnected(Exception):
pass
class ProfileKeyUnknownError(Exception):
class ProfileKeyUnknown(Exception):
pass

View file

@ -455,11 +455,19 @@ class SAT(service.Service):
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 @@ class SAT(service.Service):
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 @@ class SAT(service.Service):
- 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 @@ class SAT(service.Service):
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):

View file

@ -183,7 +183,7 @@ class Params(object):
self.host.disconnect(profile)
else:
log.info(_("Trying to delete a connected profile"))
return defer.fail(Failure(exceptions.ConnectedProfileError))
return defer.fail(Failure(exceptions.ProfileConnected))
return self.storage.deleteProfile(profile)
def getProfileName(self, profile_key, return_profile_keys=False):
@ -482,7 +482,7 @@ class Params(object):
if profile not in self.params:
log.error(_('Requesting synchronous param for not connected profile'))
raise exceptions.NotConnectedProfileError(profile)
raise exceptions.ProfileNotConnected(profile)
if attr == "value":
value = self._getParam(category, name, profile=profile)

View file

@ -124,7 +124,7 @@ class FakeSAT(object):
@return: client or None if it doesn't exist"""
profile = self.memory.getProfileName(profile_key)
if not profile:
raise exceptions.ProfileKeyUnknownError
raise exceptions.ProfileKeyUnknown
if profile not in self.profiles:
self.profiles[profile] = FakeClient(self, profile)
return self.profiles[profile]