RefactoringRope/RefactoringClient.py

branch
server_client_variant
changeset 163
6a9e7b37a18b
parent 162
55eaaed9d590
child 164
121d426d4ed7
--- a/RefactoringRope/RefactoringClient.py	Mon Sep 11 19:55:45 2017 +0200
+++ b/RefactoringRope/RefactoringClient.py	Tue Sep 12 18:55:25 2017 +0200
@@ -19,22 +19,24 @@
     path = os.path.join(os.path.dirname(__file__), 'rope_py2')
 sys.path.insert(0, path)
 
-try:
-    bytes = unicode
-    import StringIO as io   # __IGNORE_EXCEPTION__
-except NameError:
-    import io       # __IGNORE_WARNING__
-
-import socket
-import select
-import traceback
-import time
-import json
+##try:
+##    bytes = unicode
+##    import StringIO as io   # __IGNORE_EXCEPTION__
+##except NameError:
+##    import io       # __IGNORE_WARNING__
+##
+##import socket
+##import select
+##import traceback
+##import time
+##import json
 
 import rope.base.project
 
+from JsonClient import JsonClient
 
-class RefactoringClient(object):
+
+class RefactoringClient(JsonClient):
     """
     Class implementing the refactoring client interface to rope.
     """
@@ -49,7 +51,8 @@
         @param projectPath path to the project
         @type str
         """
-        self.__connection = socket.create_connection((host, port))
+        super(RefactoringClient, self).__init__(host, port)
+##        self.__connection = socket.create_connection((host, port))
         
         from FileSystemCommands import RefactoringClientFileSystemCommands
         self.__fsCommands = RefactoringClientFileSystemCommands(self)
@@ -58,101 +61,103 @@
         self.__project = rope.base.project.Project(
             self.__projectpath, fscommands=self.__fsCommands)
     
-    def __processJson(self, jsonStr):
-        """
-        Public method to handle a command serialized as a JSON string.
-        
-        @param jsonStr string containing the command received from the IDE
-        @type str
+    def handleCall(self, method, params):
         """
-        try:
-            commandDict = json.loads(jsonStr.strip())
-        except (TypeError, ValueError) as err:
-            self.sendJson("ClientException", {
-                "ExceptionType": "ProtocolError",
-                "ExceptionValue": str(err),
-                "ProtocolData": jsonStr.strip(),
-            })
-            return
+        Public method to handle a method call from the server.
         
-        method = commandDict["method"]
-        params = commandDict["params"]
-        if "filename" in params and sys.version_info[0] == 2:
-            params["filename"] = params["filename"].encode(
-                sys.getfilesystemencoding())
+        @param method requested method name
+        @type str
+        @param params dictionary with method specific parameters
+        @type dict
+        """
+##        if "filename" in params and sys.version_info[0] == 2:
+##            params["filename"] = params["filename"].encode(
+##                sys.getfilesystemencoding())
         
         if method == "ping":
             self.sendJson("pong", {})
-    
-    def sendJson(self, command, params):
-        """
-        Public method to send a single refactoring command to the client.
-        
-        @param command command name to be sent
-        @type str
-        @param params dictionary of named parameters for the command
-        @type dict
-        """
-        import json
-        
-        commandDict = {
-            "jsonrpc": "2.0",
-            "method": command,
-            "params": params,
-        }
-        cmd = json.dumps(commandDict) + '\n'
-        self.__connection.sendall(cmd.encode('utf8', 'backslashreplace'))
-    
-    def __receiveJson(self):
-        """
-        Private method to receive a JSON encode command and data from the
-        server.
-        """
-        line = self.__connection.recv(1024 * 1024, socket.MSG_PEEK) # 1M buffer
-
-        eol = line.find(b'\n')
-
-        if eol >= 0:
-            size = eol + 1
-
-            # Now we know how big the line is, read it for real.
-            line = self.__connection.recv(size).decode(
-                'utf8', 'backslashreplace')
-            self.__processJson(line)
-    
-    def run(self):
-        """
-        Public method implementing the main loop of the client.
-        """
-        try:
-            while True:
-                try:
-                    rrdy, wrdy, xrdy = select.select(
-                        [self.__connection], [], [])
-                except (select.error, KeyboardInterrupt, socket.error):
-                    # just carry on
-                    continue
-                
-                if self.__connection in rrdy:
-                    self.__receiveJson()
-        
-        except Exception:
-            exctype, excval, exctb = sys.exc_info()
-            tbinfofile = io.StringIO()
-            traceback.print_tb(exctb, None, tbinfofile)
-            tbinfofile.seek(0)
-            tbinfo = tbinfofile.read()
-            del exctb
-            self.sendJson("ClientException", {
-                "ExceptionType": str(exctype),
-                "ExceptionValue": str(excval),
-                "Traceback": tbinfo,
-            })
-
-        # Give time to process latest response on server side
-        time.sleep(0.5)
-        self.__connection.shutdown(socket.SHUT_RDWR)
-        self.__connection.close()
+##    
+##    def sendJson(self, command, params):
+##        """
+##        Public method to send a single refactoring command to the client.
+##        
+##        @param command command name to be sent
+##        @type str
+##        @param params dictionary of named parameters for the command
+##        @type dict
+##        """
+##        import json
+##        
+##        commandDict = {
+##            "jsonrpc": "2.0",
+##            "method": command,
+##            "params": params,
+##        }
+##        cmd = json.dumps(commandDict) + '\n'
+##        self.__connection.sendall(cmd.encode('utf8', 'backslashreplace'))
+##    
+##    def __receiveJson(self):
+##        """
+##        Private method to receive a JSON encode command and data from the
+##        server.
+##        """
+##        line = self.__connection.recv(1024 * 1024, socket.MSG_PEEK) # 1M buffer
+##
+##        eol = line.find(b'\n')
+##
+##        if eol >= 0:
+##            size = eol + 1
+##
+##            # Now we know how big the line is, read it for real.
+##            line = self.__connection.recv(size).decode(
+##                'utf8', 'backslashreplace')
+##            try:
+##                commandDict = json.loads(line.strip())
+##            except (TypeError, ValueError) as err:
+##                self.sendJson("ClientException", {
+##                    "ExceptionType": "ProtocolError",
+##                    "ExceptionValue": str(err),
+##                    "ProtocolData": line.strip(),
+##                })
+##                return
+##            
+##            method = commandDict["method"]
+##            params = commandDict["params"]
+##            self.handleCall(method, params)
+##    
+##    def run(self):
+##        """
+##        Public method implementing the main loop of the client.
+##        """
+##        try:
+##            while True:
+##                try:
+##                    rrdy, wrdy, xrdy = select.select(
+##                        [self.__connection], [], [])
+##                except (select.error, KeyboardInterrupt, socket.error):
+##                    # just carry on
+##                    continue
+##                
+##                if self.__connection in rrdy:
+##                    self.__receiveJson()
+##        
+##        except Exception:
+##            exctype, excval, exctb = sys.exc_info()
+##            tbinfofile = io.StringIO()
+##            traceback.print_tb(exctb, None, tbinfofile)
+##            tbinfofile.seek(0)
+##            tbinfo = tbinfofile.read()
+##            del exctb
+##            self.sendJson("ClientException", {
+##                "ExceptionType": str(exctype),
+##                "ExceptionValue": str(excval),
+##                "Traceback": tbinfo,
+##            })
+##
+##        # Give time to process latest response on server side
+##        time.sleep(0.5)
+##        self.__connection.shutdown(socket.SHUT_RDWR)
+##        self.__connection.close()
 
 if __name__ == '__main__':
     if len(sys.argv) != 4:

eric ide

mercurial