Mercurial > libervia-backend
comparison 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 |
comparison
equal
deleted
inserted
replaced
1603:2b82d846848e | 1604:9ac78437000d |
---|---|
104 | 104 |
105 @progress_id.setter | 105 @progress_id.setter |
106 def progress_id(self, value): | 106 def progress_id(self, value): |
107 self._progress_id = value | 107 self._progress_id = value |
108 | 108 |
109 | |
110 def addOnQuitCallback(self, callback, *args, **kwargs): | |
111 """Add a callback which will be called on quit command | |
112 | |
113 @param callback(callback): method to call | |
114 """ | |
115 try: | |
116 callbacks_list = self._onQuitCallbacks | |
117 except AttributeError: | |
118 callbacks_list = self._onQuitCallbacks = [] | |
119 finally: | |
120 callbacks_list.append((callback, args, kwargs)) | |
121 | |
109 def _make_parents(self): | 122 def _make_parents(self): |
110 self.parents = {} | 123 self.parents = {} |
111 | 124 |
112 # we have a special case here as the start-session option is present only if connection is not needed, | 125 # we have a special case here as the start-session option is present only if connection is not needed, |
113 # so we create two similar parents, one with the option, the other one without it | 126 # so we create two similar parents, one with the option, the other one without it |
180 try: | 193 try: |
181 self.loop.quit() | 194 self.loop.quit() |
182 except AttributeError: | 195 except AttributeError: |
183 pass | 196 pass |
184 | 197 |
198 def quitFromSignal(self, errcode=0): | |
199 """Same as self.quit, but from a signal handler | |
200 | |
201 /!\: return must be used after calling this method ! | |
202 """ | |
203 assert self.need_loop | |
204 # XXX: python-dbus will show a traceback if we exit in a signal handler with an error code | |
205 # so we use this little timeout trick to avoid it | |
206 GLib.timeout_add(0, self.quit, errcode) | |
207 | |
185 def quit(self, errcode=0): | 208 def quit(self, errcode=0): |
209 # first the onQuitCallbacks | |
210 try: | |
211 callbacks_list = self._onQuitCallbacks | |
212 except AttributeError: | |
213 pass | |
214 else: | |
215 for callback, args, kwargs in callbacks_list: | |
216 callback(*args, **kwargs) | |
217 | |
186 self.stop_loop() | 218 self.stop_loop() |
187 if errcode: | 219 if errcode: |
188 sys.exit(errcode) | 220 sys.exit(errcode) |
189 | 221 |
190 def check_jids(self, jids): | 222 def check_jids(self, jids): |