11 import json |
11 import json |
12 import os |
12 import os |
13 import struct |
13 import struct |
14 import sys |
14 import sys |
15 from zlib import adler32 |
15 from zlib import adler32 |
|
16 import contextlib |
16 |
17 |
17 from PyQt5.QtCore import QProcess, pyqtSignal, QTimer |
18 from PyQt5.QtCore import QProcess, pyqtSignal, QTimer |
18 from PyQt5.QtWidgets import QApplication |
19 from PyQt5.QtWidgets import QApplication |
19 from PyQt5.QtNetwork import QTcpServer, QHostAddress |
20 from PyQt5.QtNetwork import QTcpServer, QHostAddress |
20 |
21 |
51 self.isWorking = None |
52 self.isWorking = None |
52 self.runningJob = [None, None, None, None] |
53 self.runningJob = [None, None, None, None] |
53 self.__queue = [] |
54 self.__queue = [] |
54 self.services = {} |
55 self.services = {} |
55 |
56 |
56 super(BackgroundService, self).__init__(parent) |
57 super().__init__(parent) |
57 |
58 |
58 networkInterface = Preferences.getDebugger("NetworkInterface") |
59 networkInterface = Preferences.getDebugger("NetworkInterface") |
59 if networkInterface == "all" or '.' in networkInterface: |
60 if networkInterface == "all" or '.' in networkInterface: |
60 self.hostAddress = '127.0.0.1' |
61 self.hostAddress = '127.0.0.1' |
61 else: |
62 else: |
181 packedData = packedData.decode('utf-8') |
182 packedData = packedData.decode('utf-8') |
182 # "check" if is's a tuple of 3 values |
183 # "check" if is's a tuple of 3 values |
183 fx, fn, data = json.loads(packedData) |
184 fx, fn, data = json.loads(packedData) |
184 |
185 |
185 if fx == 'INIT': |
186 if fx == 'INIT': |
186 pass |
187 if data != "ok": |
|
188 E5MessageBox.critical( |
|
189 None, |
|
190 self.tr("Initialization of Background Service"), |
|
191 self.tr( |
|
192 "<p>Initialization of Background Service" |
|
193 " <b>{0}</b> failed.</p><p>Reason: {1}</p>") |
|
194 .format(fn, data) |
|
195 ) |
187 elif fx == 'EXCEPTION': |
196 elif fx == 'EXCEPTION': |
188 # Remove connection because it'll close anyway |
197 # Remove connection because it'll close anyway |
189 self.connections.pop(lang, None) |
198 self.connections.pop(lang, None) |
190 # Call sys.excepthook(type, value, traceback) to emulate the |
199 # Call sys.excepthook(type, value, traceback) to emulate the |
191 # exception which was caught on the client |
200 # exception which was caught on the client |
213 |
222 |
214 if res == E5MessageBox.Retry: |
223 if res == E5MessageBox.Retry: |
215 self.enqueueRequest(*self.runningJob) |
224 self.enqueueRequest(*self.runningJob) |
216 else: |
225 else: |
217 fx, lng, fn, data = self.runningJob |
226 fx, lng, fn, data = self.runningJob |
218 try: |
227 with contextlib.suppress(KeyError, TypeError): |
219 self.services[(fx, lng)][3](fx, lng, fn, self.tr( |
228 self.services[(fx, lng)][3](fx, lng, fn, self.tr( |
220 "An error in Eric's background client stopped the" |
229 "An error in Eric's background client stopped the" |
221 " service.") |
230 " service.") |
222 ) |
231 ) |
223 except (KeyError, TypeError): |
|
224 # ignore silently |
|
225 pass |
|
226 if res != E5MessageBox.No: |
232 if res != E5MessageBox.No: |
227 self.isWorking = None |
233 self.isWorking = None |
228 self.restartService(lang, True) |
234 self.restartService(lang, True) |
229 return |
235 return |
230 elif data == 'Unknown service.': |
236 elif data == 'Unknown service.': |
238 if callback: |
244 if callback: |
239 if isinstance(data, (list, tuple)): |
245 if isinstance(data, (list, tuple)): |
240 callback[2](fn, *data) |
246 callback[2](fn, *data) |
241 elif isinstance(data, str): |
247 elif isinstance(data, str): |
242 callback[3](fx, lang, fn, data) |
248 callback[3](fx, lang, fn, data) |
243 continue |
249 if data == 'Unknown batch service.': |
|
250 self.batchJobDone.emit(fx, lang) |
|
251 self.__cancelled = True |
244 else: |
252 else: |
245 self.batchJobDone.emit(fx, lang) |
253 self.batchJobDone.emit(fx, lang) |
246 else: |
254 else: |
247 callback = self.services.get((fx, lang)) |
255 callback = self.services.get((fx, lang)) |
248 if callback: |
256 if callback: |