# HG changeset patch # User Goffi # Date 1391534667 -3600 # Node ID 5d6c45d6ee1bb838589eee3b083763ec462f6496 # Parent 7c05c39156a2f6885991b91911faafc1f3fda4bd core: added "one_shot" option to registered callback: when this keyword is used, the callback is automatically removed once it has been called. diff -r 7c05c39156a2 -r 5d6c45d6ee1b src/core/sat_main.py --- a/src/core/sat_main.py Tue Feb 04 18:21:51 2014 +0100 +++ b/src/core/sat_main.py Tue Feb 04 18:24:27 2014 +0100 @@ -847,6 +847,7 @@ """ Register a callback. Use with_data=True in kwargs if the callback use the optional data dict use force_id=id to avoid generated id. Can lead to name conflict, avoid if possible + use one_shot=True to delete callback once it have been called @param callback: any callable @return: id of the registered callback """ @@ -857,11 +858,21 @@ if callback_id in self._cb_map: raise exceptions.ConflictError(_(u"id already registered")) self._cb_map[callback_id] = (callback, args, kwargs) + + if "one_shot" in kwargs: # One Shot callback are removed after 30 min + def purgeCallback(): + try: + self.removeCallback(callback_id) + except KeyError: + pass + reactor.callLater(1800, purgeCallback) + return callback_id def removeCallback(self, callback_id): """ Remove a previously registered callback @param callback_id: id returned by [registerCallback] """ + debug("Removing callback [%s]" % callback_id) del self._cb_map[callback_id] def launchCallback(self, callback_id, data=None, profile_key="@NONE@"): @@ -889,6 +900,9 @@ kwargs["profile"] = profile del kwargs["with_data"] + if kwargs.pop('one_shot', False): + self.removeCallback(callback_id) + return defer.maybeDeferred(callback, *args, **kwargs) #Menus management