# HG changeset patch # User Goffi # Date 1447625814 -3600 # Node ID 9ac78437000dd2a4b873f1aea78f66f9a76bdc4a # Parent 2b82d846848e79cc53d36ee8cd79e527bcdd8675 jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks diff -r 2b82d846848e -r 9ac78437000d frontends/src/jp/base.py --- a/frontends/src/jp/base.py Sun Nov 15 23:11:41 2015 +0100 +++ b/frontends/src/jp/base.py Sun Nov 15 23:16:54 2015 +0100 @@ -106,6 +106,19 @@ def progress_id(self, value): self._progress_id = value + + def addOnQuitCallback(self, callback, *args, **kwargs): + """Add a callback which will be called on quit command + + @param callback(callback): method to call + """ + try: + callbacks_list = self._onQuitCallbacks + except AttributeError: + callbacks_list = self._onQuitCallbacks = [] + finally: + callbacks_list.append((callback, args, kwargs)) + def _make_parents(self): self.parents = {} @@ -182,7 +195,26 @@ except AttributeError: pass + def quitFromSignal(self, errcode=0): + """Same as self.quit, but from a signal handler + + /!\: return must be used after calling this method ! + """ + assert self.need_loop + # XXX: python-dbus will show a traceback if we exit in a signal handler with an error code + # so we use this little timeout trick to avoid it + GLib.timeout_add(0, self.quit, errcode) + def quit(self, errcode=0): + # first the onQuitCallbacks + try: + callbacks_list = self._onQuitCallbacks + except AttributeError: + pass + else: + for callback, args, kwargs in callbacks_list: + callback(*args, **kwargs) + self.stop_loop() if errcode: sys.exit(errcode)