diff frontends/src/jp/base.py @ 1604:9ac78437000d

jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
author Goffi <goffi@goffi.org>
date Sun, 15 Nov 2015 23:16:54 +0100
parents 8d41cd4da2f6
children 0aded9648c5c
line wrap: on
line diff
--- 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)