SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.

Sun, 09 Dec 2018 15:24:39 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 09 Dec 2018 15:24:39 +0100
changeset 6622
3dfcbe478fd3
parent 6621
db2ec0feca8a
child 6623
c0882a599e18

SingleApplication, E5SingleApplication, TRSingleApplication: changed the single application logic to use JSON command dictionaries.

E5Gui/E5SingleApplication.py file | annotate | diff | comparison | revisions
Toolbox/SingleApplication.py file | annotate | diff | comparison | revisions
Tools/TRSingleApplication.py file | annotate | diff | comparison | revisions
--- a/E5Gui/E5SingleApplication.py	Sun Dec 09 14:14:06 2018 +0100
+++ b/E5Gui/E5SingleApplication.py	Sun Dec 09 15:24:39 2018 +0100
@@ -19,16 +19,16 @@
 import Utilities
 
 ###########################################################################
-# define some module global stuff
+## define some module global stuff
 ###########################################################################
 
 SAFile = "eric6"
 
 # define the protocol tokens
-SAOpenFile = '>OpenFile<'
-SAOpenProject = '>OpenProject<'
-SAOpenMultiProject = '>OpenMultiProject<'
-SAArguments = '>Arguments<'
+SAOpenFile = 'OpenFile'
+SAOpenProject = 'OpenProject'
+SAOpenMultiProject = 'OpenMultiProject'
+SAArguments = 'Arguments'
 
 
 class E5SingleApplicationServer(SingleApplicationServer):
@@ -41,27 +41,29 @@
         """
         SingleApplicationServer.__init__(self, SAFile)
 
-    def handleCommand(self, cmd, params):
+    def handleCommand(self, command, arguments):
         """
         Public slot to handle the command sent by the client.
         
-        @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
         """
-        if cmd == SAOpenFile:
-            self.__saOpenFile(params)
+        if command == SAOpenFile:
+            self.__saOpenFile(arguments[0])
             return
 
-        if cmd == SAOpenProject:
-            self.__saOpenProject(params)
+        if command == SAOpenProject:
+            self.__saOpenProject(arguments[0])
             return
 
-        if cmd == SAOpenMultiProject:
-            self.__saOpenMultiProject(params)
+        if command == SAOpenMultiProject:
+            self.__saOpenMultiProject(arguments[0])
             return
 
-        if cmd == SAArguments:
-            self.__saArguments(params)
+        if command == SAArguments:
+            self.__saArguments(arguments[0])
             return
 
     def __saOpenFile(self, fname):
@@ -161,8 +163,7 @@
         
         @param fname name of file to be opened (string)
         """
-        cmd = "{0}{1}\n".format(SAOpenFile, Utilities.normabspath(fname))
-        self.sendCommand(cmd)
+        self.sendCommand(SAOpenFile, [Utilities.normabspath(fname)])
         
     def __openProject(self, pfname):
         """
@@ -170,8 +171,7 @@
         
         @param pfname name of the projectfile to be opened (string)
         """
-        cmd = "{0}{1}\n".format(SAOpenProject, Utilities.normabspath(pfname))
-        self.sendCommand(cmd)
+        self.sendCommand(SAOpenProject, [Utilities.normabspath(pfname)])
         
     def __openMultiProject(self, pfname):
         """
@@ -179,9 +179,7 @@
         
         @param pfname name of the projectfile to be opened (string)
         """
-        cmd = "{0}{1}\n".format(SAOpenMultiProject,
-                                Utilities.normabspath(pfname))
-        self.sendCommand(cmd)
+        self.sendCommand(SAOpenMultiProject, [Utilities.normabspath(pfname)])
         
     def __sendArguments(self, argsStr):
         """
@@ -189,5 +187,4 @@
         
         @param argsStr space delimited list of command args (string)
         """
-        cmd = "{0}{1}\n".format(SAArguments, argsStr)
-        self.sendCommand(cmd)
+        self.sendCommand(SAArguments, [argsStr])
--- 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):
--- a/Tools/TRSingleApplication.py	Sun Dec 09 14:14:06 2018 +0100
+++ b/Tools/TRSingleApplication.py	Sun Dec 09 15:24:39 2018 +0100
@@ -17,14 +17,14 @@
     SingleApplicationServer
 
 ###########################################################################
-# define some module global stuff
+## define some module global stuff
 ###########################################################################
 
 SAFile = "eric6_trpreviewer"
 
 # define the protocol tokens
-SALoadForm = '>LoadForm<'
-SALoadTranslation = '>LoadTranslation<'
+SALoadForm = 'LoadForm'
+SALoadTranslation = 'LoadTranslation'
 
 
 class TRSingleApplicationServer(SingleApplicationServer):
@@ -48,20 +48,20 @@
         
         self.parent = parent
 
-    def handleCommand(self, cmd, params):
+    def handleCommand(self, command, arguments):
         """
         Public slot to handle the command sent by the client.
         
-        @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
         """
-        if cmd == SALoadForm:
-            self.__saLoadForm(eval(params))
-            return
+        if command == SALoadForm:
+            self.__saLoadForm(arguments)
 
-        if cmd == SALoadTranslation:
-            self.__saLoadTranslation(eval(params))
-            return
+        elif command == SALoadTranslation:
+            self.__saLoadTranslation(arguments)
 
     def __saLoadForm(self, fnames):
         """
@@ -118,9 +118,7 @@
             elif ext == '.qm':
                 qmFiles.append(arg)
         
-        cmd = "{0}{1}\n".format(SALoadForm, str(uiFiles))
-        self.sendCommand(cmd)
-        cmd = "{0}{1}\n".format(SALoadTranslation, str(qmFiles))
-        self.sendCommand(cmd)
+        self.sendCommand(SALoadForm, uiFiles)
+        self.sendCommand(SALoadTranslation, qmFiles)
         
         self.disconnect()

eric ide

mercurial