diff -r db2ec0feca8a -r 3dfcbe478fd3 Toolbox/SingleApplication.py --- a/Toolbox/SingleApplication.py Sun Dec 09 14:14:06 2018 +0100 +++ b/Toolbox/SingleApplication.py Sun Dec 09 15:24:39 2018 +0100 @@ -9,9 +9,15 @@ from __future__ import unicode_literals +import json + from PyQt5.QtCore import QByteArray from PyQt5.QtNetwork import QLocalServer, QLocalSocket +from E5Gui import E5MessageBox + +import Utilities + class SingleApplicationServer(QLocalServer): """ @@ -41,34 +47,46 @@ """ sock = self.nextPendingConnection() - # If we already have a connection, refuse this one. It will be closed + # If we already have a connection, refuse this one. It will be closed # automatically. if self.qsock is not None: return self.qsock = sock - self.qsock.readyRead.connect(self.__parseLine) + self.qsock.readyRead.connect(self.__receiveJson) self.qsock.disconnected.connect(self.__disconnected) - def __parseLine(self): + def __receiveJson(self): """ - Private method to handle data from the client. + Private method to receive the data from the client. """ while self.qsock and self.qsock.canReadLine(): line = bytes(self.qsock.readLine()).decode() ## print(line) ##debug - eoc = line.find('<') + 1 + try: + commandDict = json.loads(line.strip()) + except (TypeError, ValueError) as err: + E5MessageBox.critical( + None, + self.tr("Single Application Protocol Error"), + self.tr("""<p>The response received from the single""" + """ application client could not be decoded.""" + """ Please report this issue with the received""" + """ data to the eric bugs email address.</p>""" + """<p>Error: {0}</p>""" + """<p>Data:<br/>{0}</p>""").format( + str(err), Utilities.html_encode(line.strip())), + E5MessageBox.StandardButtons( + E5MessageBox.Ok)) + return - boc = line.find('>') - if boc >= 0 and eoc > boc: - # handle the command sent by the client. - cmd = line[boc:eoc] - params = line[eoc:-1] - - self.handleCommand(cmd, params) + command = commandDict["command"] + arguments = commandDict["arguments"] + + self.handleCommand(command, arguments) def __disconnected(self): """ @@ -88,14 +106,16 @@ self.close() - def handleCommand(self, cmd, params): + def handleCommand(self, command, arguments): """ Public slot to handle the command sent by the client. <b>Note</b>: This method must be overridden by subclasses. - @param cmd commandstring (string) - @param params parameterstring (string) + @param command command sent by the client + @type str + @param arguments list of command arguments + @type list of str @exception RuntimeError raised to indicate that this method must be implemented by a subclass """ @@ -156,14 +176,23 @@ """ raise RuntimeError("'processArgs' must be overridden") - def sendCommand(self, cmd): + def sendCommand(self, command, arguments): """ Public method to send the command to the application server. - @param cmd command to be sent (string) + @param command command to be sent to the server + @type str + @param arguments list of command arguments + @type list of str """ if self.connected: - self.sock.write(QByteArray(cmd.encode())) + commandDict = { + "command": command, + "arguments": arguments, + } + self.sock.write(QByteArray( + "{0}\n".format(json.dumps(commandDict)).encode() + )) self.sock.flush() def errstr(self):