RefactoringRope/RefactoringClient.py

branch
server_client_variant
changeset 166
6fc202183b3b
parent 165
ea41742015af
child 167
3c8e875d0326
--- a/RefactoringRope/RefactoringClient.py	Fri Sep 15 19:50:07 2017 +0200
+++ b/RefactoringRope/RefactoringClient.py	Sat Sep 16 16:40:50 2017 +0200
@@ -20,6 +20,7 @@
     str = unicode   # __IGNORE_WARNING__
 sys.path.insert(0, path)
 
+import rope
 import rope.base.project
 import rope.base.libutils
 
@@ -43,6 +44,18 @@
         """
         super(RefactoringClient, self).__init__(host, port)
         
+        self.__methodMapping = {
+            "AbortAction": self.__abortAction,
+            "Validate": self.__validate,
+            "QueryReferences": self.__queryReferences,
+            "QueryDefinition": self.__queryDefinition,
+            "QueryImplementations": self.__queryImplementations,
+            "GetConfig": self.__getConfig,
+            "ConfigChanged": self.__configChanged,
+            "PerformSoa": self.__performSOA,
+            "ReportChanged": self.__reportChanged,
+        }
+        
         from FileSystemCommands import RefactoringClientFileSystemCommands
         self.__fsCommands = RefactoringClientFileSystemCommands(self)
         
@@ -65,19 +78,7 @@
 ##            params["filename"] = params["filename"].encode(
 ##                sys.getfilesystemencoding())
         
-        if method == "AbortAction":
-            if self.__progressHandle is not None and \
-               not self.__progressHandle.is_stopped():
-                self.__progressHandle.stop()
-        
-        elif method == "Validate":
-            self.__project.validate(self.__project.root)
-        
-        elif method == "QueryReferences":
-            self.__queryReferences(params)
-        
-        elif method == "ping":
-            self.sendJson("pong", {})
+        self.__methodMapping[method](params)
     
     def __handleRopeError(self, err):
         """
@@ -100,6 +101,62 @@
         
         return errorDict
     
+    def __abortAction(self, params):
+        """
+        Private method to abort the current action.
+        
+        @param params dictionary containing the method parameters sent by
+            the server
+        @type dict
+        """
+        if self.__progressHandle is not None and \
+           not self.__progressHandle.is_stopped():
+            self.__progressHandle.stop()
+    
+    def __validate(self, params):
+        """
+        Private slot to validate the project.
+        
+        @param params dictionary containing the method parameters sent by
+            the server
+        @type dict
+        """
+        self.__project.validate(self.__project.root)
+    
+    def __getConfig(self, params):
+        """
+        Private method to send some configuration data to the server.
+        
+        @param params dictionary containing the method parameters sent by
+            the server
+        @type dict
+        """
+        result = {
+            "RopeFolderName": self.__project.ropefolder.real_path,
+            "DefaultConfig": self.__project._default_config(),
+        }
+        if sys.version_info[0] >= 3:
+            directory = 'rope_py3'
+        else:
+            directory = 'rope_py2'
+        result["RopeHelpFile"] = os.path.join(
+            os.path.dirname(__file__), directory,
+            "rope", "docs", "overview.txt")
+        
+        self.sendJson("Config", result)
+    
+    def __configChanged(self, params):
+        """
+        Private method to handle a change of the configuration file.
+        
+        @param params dictionary containing the method parameters sent by
+            the server
+        @type dict
+        """
+        self.__project.close()
+        self.__project = rope.base.project.Project(
+            self.__projectpath, fscommands=self.__fsCommands)
+    
     def __queryReferences(self, params):
         """
         Private method to handle the Find References action.
@@ -119,14 +176,16 @@
         from ProgressHandle import ProgressHandle
         resource = rope.base.libutils.path_to_resource(
             self.__project, filename)
-        handle = ProgressHandle(self, title, True)
+        self.__progressHandle = ProgressHandle(self, title, True)
         try:
             occurrences = rope.contrib.findit.find_occurrences(
                 self.__project, resource, offset,
-                unsure=True, in_hierarchy=True, task_handle=handle)
+                unsure=True, in_hierarchy=True,
+                task_handle=self.__progressHandle)
         except Exception as err:
             errorDict = self.__handleRopeError(err)
-        handle.reset()
+        self.__progressHandle.reset()
+        self.__progressHandle = None
         
         result = {
             "Title": title,
@@ -139,6 +198,132 @@
         result.update(errorDict)
         
         self.sendJson("QueryReferencesResult", result)
+    
+    def __queryDefinition(self, params):
+        """
+        Private method to handle the Find Definition action.
+        
+        @param params dictionary containing the method parameters sent by
+            the server
+        @type dict
+        """
+        title = params["Title"]
+        filename = params["FileName"]
+        offset = params["Offset"]
+        source = params["Source"]
+        subcommand = params["Subcommand"]
+        
+        errorDict = {}
+        location = None
+        
+        import rope.contrib.findit
+        resource = rope.base.libutils.path_to_resource(
+            self.__project, filename)
+        try:
+            location = rope.contrib.findit.find_definition(
+                self.__project, source, offset, resource)
+        except Exception as err:
+            errorDict = self.__handleRopeError(err)
+        
+        result = {
+            "Title": title,
+            "Subcommand": subcommand,
+        }
+        if location is not None:
+            result["Location"] = [
+                location.resource.real_path, location.lineno
+            ]
+        result.update(errorDict)
+        
+        self.sendJson("QueryDefinitionResult", result)
+    
+    def __queryImplementations(self, params):
+        """
+        Private method to handle the Find Implementations action.
+        
+        @param params dictionary containing the method parameters sent by
+            the server
+        @type dict
+        """
+        title = params["Title"]
+        filename = params["FileName"]
+        offset = params["Offset"]
+        
+        errorDict = {}
+        occurrences = []
+        
+        import rope.contrib.findit
+        from ProgressHandle import ProgressHandle
+        resource = rope.base.libutils.path_to_resource(
+            self.__project, filename)
+        self.__progressHandle = ProgressHandle(self, title, True)
+        try:
+            occurrences = rope.contrib.findit.find_implementations(
+                self.__project, resource, offset,
+                task_handle=self.__progressHandle)
+        except Exception as err:
+            errorDict = self.__handleRopeError(err)
+        self.__progressHandle.reset()
+        self.__progressHandle = None
+        
+        result = {
+            "Title": title,
+            "EntriesCount": len(occurrences),
+            "Entries": [
+                [occurrence.resource.real_path, occurrence.lineno,
+                 occurrence.unsure] for occurrence in occurrences
+            ],
+        }
+        result.update(errorDict)
+        
+        self.sendJson("QueryImplementationsResult", result)
+    
+    def __performSOA(self, params):
+        """
+        Private method to perform SOA on all modules.
+        
+        @param params dictionary containing the method parameters sent by
+            the server
+        @type dict
+        """
+        title = params["Title"]
+        
+        errorDict = {}
+        
+        from ProgressHandle import ProgressHandle
+        self.__progressHandle = ProgressHandle(self, title, True)
+        try:
+            rope.base.libutils.analyze_modules(
+                self.__project, task_handle=self.__progressHandle)
+        except Exception as err:
+            errorDict = self.__handleRopeError(err)
+        self.__progressHandle.reset()
+        self.__progressHandle = None
+        
+        result = {
+            "Title": title,
+        }
+        result.update(errorDict)
+        
+        self.sendJson("SoaFinished", result)
+    
+    def __reportChanged(self, params):
+        """
+        Private method to register some changed sources.
+        
+        @param params dictionary containing the method parameters sent by
+            the server
+        @type dict
+        """
+        filename = params["FileName"]
+        oldSource = params["OldSource"]
+        
+        try:
+            rope.base.libutils.report_change(
+                self.__project, filename, oldSource)
+        except Exception:
+            # simply ignore it
+            pass
 
 if __name__ == '__main__':
     if len(sys.argv) != 4:
@@ -149,6 +334,8 @@
     client = RefactoringClient(host, int(port), projectPath)
     # Start the main loop
     client.run()
+    
+    sys.exit(0)
 
 #
 # eflag: noqa = M801

eric ide

mercurial