comparison src/core/sat_main.py @ 806:5d6c45d6ee1b

core: added "one_shot" option to registered callback: when this keyword is used, the callback is automatically removed once it has been called.
author Goffi <goffi@goffi.org>
date Tue, 04 Feb 2014 18:24:27 +0100
parents 02ee9ef95277
children be4c5e24dab9
comparison
equal deleted inserted replaced
805:7c05c39156a2 806:5d6c45d6ee1b
845 845
846 def registerCallback(self, callback, *args, **kwargs): 846 def registerCallback(self, callback, *args, **kwargs):
847 """ Register a callback. 847 """ Register a callback.
848 Use with_data=True in kwargs if the callback use the optional data dict 848 Use with_data=True in kwargs if the callback use the optional data dict
849 use force_id=id to avoid generated id. Can lead to name conflict, avoid if possible 849 use force_id=id to avoid generated id. Can lead to name conflict, avoid if possible
850 use one_shot=True to delete callback once it have been called
850 @param callback: any callable 851 @param callback: any callable
851 @return: id of the registered callback 852 @return: id of the registered callback
852 """ 853 """
853 callback_id = kwargs.pop('force_id', None) 854 callback_id = kwargs.pop('force_id', None)
854 if callback_id is None: 855 if callback_id is None:
855 callback_id = str(uuid4()) 856 callback_id = str(uuid4())
856 else: 857 else:
857 if callback_id in self._cb_map: 858 if callback_id in self._cb_map:
858 raise exceptions.ConflictError(_(u"id already registered")) 859 raise exceptions.ConflictError(_(u"id already registered"))
859 self._cb_map[callback_id] = (callback, args, kwargs) 860 self._cb_map[callback_id] = (callback, args, kwargs)
861
862 if "one_shot" in kwargs: # One Shot callback are removed after 30 min
863 def purgeCallback():
864 try:
865 self.removeCallback(callback_id)
866 except KeyError:
867 pass
868 reactor.callLater(1800, purgeCallback)
869
860 return callback_id 870 return callback_id
861 871
862 def removeCallback(self, callback_id): 872 def removeCallback(self, callback_id):
863 """ Remove a previously registered callback 873 """ Remove a previously registered callback
864 @param callback_id: id returned by [registerCallback] """ 874 @param callback_id: id returned by [registerCallback] """
875 debug("Removing callback [%s]" % callback_id)
865 del self._cb_map[callback_id] 876 del self._cb_map[callback_id]
866 877
867 def launchCallback(self, callback_id, data=None, profile_key="@NONE@"): 878 def launchCallback(self, callback_id, data=None, profile_key="@NONE@"):
868 """Launch a specific callback 879 """Launch a specific callback
869 @param callback_id: id of the action (callback) to launch 880 @param callback_id: id of the action (callback) to launch
886 raise exceptions.DataError("Required data for this callback is missing") 897 raise exceptions.DataError("Required data for this callback is missing")
887 args,kwargs=list(args)[:],kwargs.copy() # we don't want to modify the original (kw)args 898 args,kwargs=list(args)[:],kwargs.copy() # we don't want to modify the original (kw)args
888 args.insert(0, data) 899 args.insert(0, data)
889 kwargs["profile"] = profile 900 kwargs["profile"] = profile
890 del kwargs["with_data"] 901 del kwargs["with_data"]
902
903 if kwargs.pop('one_shot', False):
904 self.removeCallback(callback_id)
891 905
892 return defer.maybeDeferred(callback, *args, **kwargs) 906 return defer.maybeDeferred(callback, *args, **kwargs)
893 907
894 #Menus management 908 #Menus management
895 909