RefactoringRope/RefactoringClient.py

branch
server_client_variant
changeset 166
6fc202183b3b
parent 165
ea41742015af
child 167
3c8e875d0326
equal deleted inserted replaced
165:ea41742015af 166:6fc202183b3b
18 else: 18 else:
19 path = os.path.join(os.path.dirname(__file__), 'rope_py2') 19 path = os.path.join(os.path.dirname(__file__), 'rope_py2')
20 str = unicode # __IGNORE_WARNING__ 20 str = unicode # __IGNORE_WARNING__
21 sys.path.insert(0, path) 21 sys.path.insert(0, path)
22 22
23 import rope
23 import rope.base.project 24 import rope.base.project
24 import rope.base.libutils 25 import rope.base.libutils
25 26
26 from JsonClient import JsonClient 27 from JsonClient import JsonClient
27 28
41 @param projectPath path to the project 42 @param projectPath path to the project
42 @type str 43 @type str
43 """ 44 """
44 super(RefactoringClient, self).__init__(host, port) 45 super(RefactoringClient, self).__init__(host, port)
45 46
47 self.__methodMapping = {
48 "AbortAction": self.__abortAction,
49 "Validate": self.__validate,
50 "QueryReferences": self.__queryReferences,
51 "QueryDefinition": self.__queryDefinition,
52 "QueryImplementations": self.__queryImplementations,
53 "GetConfig": self.__getConfig,
54 "ConfigChanged": self.__configChanged,
55 "PerformSoa": self.__performSOA,
56 "ReportChanged": self.__reportChanged,
57 }
58
46 from FileSystemCommands import RefactoringClientFileSystemCommands 59 from FileSystemCommands import RefactoringClientFileSystemCommands
47 self.__fsCommands = RefactoringClientFileSystemCommands(self) 60 self.__fsCommands = RefactoringClientFileSystemCommands(self)
48 61
49 self.__projectpath = projectPath 62 self.__projectpath = projectPath
50 self.__project = rope.base.project.Project( 63 self.__project = rope.base.project.Project(
63 """ 76 """
64 ## if "filename" in params and sys.version_info[0] == 2: 77 ## if "filename" in params and sys.version_info[0] == 2:
65 ## params["filename"] = params["filename"].encode( 78 ## params["filename"] = params["filename"].encode(
66 ## sys.getfilesystemencoding()) 79 ## sys.getfilesystemencoding())
67 80
68 if method == "AbortAction": 81 self.__methodMapping[method](params)
69 if self.__progressHandle is not None and \
70 not self.__progressHandle.is_stopped():
71 self.__progressHandle.stop()
72
73 elif method == "Validate":
74 self.__project.validate(self.__project.root)
75
76 elif method == "QueryReferences":
77 self.__queryReferences(params)
78
79 elif method == "ping":
80 self.sendJson("pong", {})
81 82
82 def __handleRopeError(self, err): 83 def __handleRopeError(self, err):
83 """ 84 """
84 Private method to process a rope error. 85 Private method to process a rope error.
85 86
98 errorDict["ErrorFile"] = err.filename 99 errorDict["ErrorFile"] = err.filename
99 errorDict["ErrorLine"] = err.lineno 100 errorDict["ErrorLine"] = err.lineno
100 101
101 return errorDict 102 return errorDict
102 103
104 def __abortAction(self, params):
105 """
106 Private method to abort the current action.
107
108 @param params dictionary containing the method parameters sent by
109 the server
110 @type dict
111 """
112 if self.__progressHandle is not None and \
113 not self.__progressHandle.is_stopped():
114 self.__progressHandle.stop()
115
116 def __validate(self, params):
117 """
118 Private slot to validate the project.
119
120 @param params dictionary containing the method parameters sent by
121 the server
122 @type dict
123 """
124 self.__project.validate(self.__project.root)
125
126 def __getConfig(self, params):
127 """
128 Private method to send some configuration data to the server.
129
130 @param params dictionary containing the method parameters sent by
131 the server
132 @type dict
133 """
134 result = {
135 "RopeFolderName": self.__project.ropefolder.real_path,
136 "DefaultConfig": self.__project._default_config(),
137 }
138 if sys.version_info[0] >= 3:
139 directory = 'rope_py3'
140 else:
141 directory = 'rope_py2'
142 result["RopeHelpFile"] = os.path.join(
143 os.path.dirname(__file__), directory,
144 "rope", "docs", "overview.txt")
145
146 self.sendJson("Config", result)
147
148 def __configChanged(self, params):
149 """
150 Private method to handle a change of the configuration file.
151
152 @param params dictionary containing the method parameters sent by
153 the server
154 @type dict
155 """
156 self.__project.close()
157 self.__project = rope.base.project.Project(
158 self.__projectpath, fscommands=self.__fsCommands)
159
103 def __queryReferences(self, params): 160 def __queryReferences(self, params):
104 """ 161 """
105 Private method to handle the Find References action. 162 Private method to handle the Find References action.
106 163
107 @param params dictionary containing the method parameters sent by 164 @param params dictionary containing the method parameters sent by
117 174
118 import rope.contrib.findit 175 import rope.contrib.findit
119 from ProgressHandle import ProgressHandle 176 from ProgressHandle import ProgressHandle
120 resource = rope.base.libutils.path_to_resource( 177 resource = rope.base.libutils.path_to_resource(
121 self.__project, filename) 178 self.__project, filename)
122 handle = ProgressHandle(self, title, True) 179 self.__progressHandle = ProgressHandle(self, title, True)
123 try: 180 try:
124 occurrences = rope.contrib.findit.find_occurrences( 181 occurrences = rope.contrib.findit.find_occurrences(
125 self.__project, resource, offset, 182 self.__project, resource, offset,
126 unsure=True, in_hierarchy=True, task_handle=handle) 183 unsure=True, in_hierarchy=True,
184 task_handle=self.__progressHandle)
127 except Exception as err: 185 except Exception as err:
128 errorDict = self.__handleRopeError(err) 186 errorDict = self.__handleRopeError(err)
129 handle.reset() 187 self.__progressHandle.reset()
188 self.__progressHandle = None
130 189
131 result = { 190 result = {
132 "Title": title, 191 "Title": title,
133 "EntriesCount": len(occurrences), 192 "EntriesCount": len(occurrences),
134 "Entries": [ 193 "Entries": [
137 ], 196 ],
138 } 197 }
139 result.update(errorDict) 198 result.update(errorDict)
140 199
141 self.sendJson("QueryReferencesResult", result) 200 self.sendJson("QueryReferencesResult", result)
201
202 def __queryDefinition(self, params):
203 """
204 Private method to handle the Find Definition action.
205
206 @param params dictionary containing the method parameters sent by
207 the server
208 @type dict
209 """
210 title = params["Title"]
211 filename = params["FileName"]
212 offset = params["Offset"]
213 source = params["Source"]
214 subcommand = params["Subcommand"]
215
216 errorDict = {}
217 location = None
218
219 import rope.contrib.findit
220 resource = rope.base.libutils.path_to_resource(
221 self.__project, filename)
222 try:
223 location = rope.contrib.findit.find_definition(
224 self.__project, source, offset, resource)
225 except Exception as err:
226 errorDict = self.__handleRopeError(err)
227
228 result = {
229 "Title": title,
230 "Subcommand": subcommand,
231 }
232 if location is not None:
233 result["Location"] = [
234 location.resource.real_path, location.lineno
235 ]
236 result.update(errorDict)
237
238 self.sendJson("QueryDefinitionResult", result)
239
240 def __queryImplementations(self, params):
241 """
242 Private method to handle the Find Implementations action.
243
244 @param params dictionary containing the method parameters sent by
245 the server
246 @type dict
247 """
248 title = params["Title"]
249 filename = params["FileName"]
250 offset = params["Offset"]
251
252 errorDict = {}
253 occurrences = []
254
255 import rope.contrib.findit
256 from ProgressHandle import ProgressHandle
257 resource = rope.base.libutils.path_to_resource(
258 self.__project, filename)
259 self.__progressHandle = ProgressHandle(self, title, True)
260 try:
261 occurrences = rope.contrib.findit.find_implementations(
262 self.__project, resource, offset,
263 task_handle=self.__progressHandle)
264 except Exception as err:
265 errorDict = self.__handleRopeError(err)
266 self.__progressHandle.reset()
267 self.__progressHandle = None
268
269 result = {
270 "Title": title,
271 "EntriesCount": len(occurrences),
272 "Entries": [
273 [occurrence.resource.real_path, occurrence.lineno,
274 occurrence.unsure] for occurrence in occurrences
275 ],
276 }
277 result.update(errorDict)
278
279 self.sendJson("QueryImplementationsResult", result)
280
281 def __performSOA(self, params):
282 """
283 Private method to perform SOA on all modules.
284
285 @param params dictionary containing the method parameters sent by
286 the server
287 @type dict
288 """
289 title = params["Title"]
290
291 errorDict = {}
292
293 from ProgressHandle import ProgressHandle
294 self.__progressHandle = ProgressHandle(self, title, True)
295 try:
296 rope.base.libutils.analyze_modules(
297 self.__project, task_handle=self.__progressHandle)
298 except Exception as err:
299 errorDict = self.__handleRopeError(err)
300 self.__progressHandle.reset()
301 self.__progressHandle = None
302
303 result = {
304 "Title": title,
305 }
306 result.update(errorDict)
307
308 self.sendJson("SoaFinished", result)
309
310 def __reportChanged(self, params):
311 """
312 Private method to register some changed sources.
313
314 @param params dictionary containing the method parameters sent by
315 the server
316 @type dict
317 """
318 filename = params["FileName"]
319 oldSource = params["OldSource"]
320
321 try:
322 rope.base.libutils.report_change(
323 self.__project, filename, oldSource)
324 except Exception:
325 # simply ignore it
326 pass
142 327
143 if __name__ == '__main__': 328 if __name__ == '__main__':
144 if len(sys.argv) != 4: 329 if len(sys.argv) != 4:
145 print('Host, port and project path parameters are missing. Abort.') 330 print('Host, port and project path parameters are missing. Abort.')
146 sys.exit(1) 331 sys.exit(1)
147 332
148 host, port, projectPath = sys.argv[1:] 333 host, port, projectPath = sys.argv[1:]
149 client = RefactoringClient(host, int(port), projectPath) 334 client = RefactoringClient(host, int(port), projectPath)
150 # Start the main loop 335 # Start the main loop
151 client.run() 336 client.run()
337
338 sys.exit(0)
152 339
153 # 340 #
154 # eflag: noqa = M801 341 # eflag: noqa = M801

eric ide

mercurial