16 if sys.version_info[0] >= 3: |
16 if sys.version_info[0] >= 3: |
17 path = os.path.join(os.path.dirname(__file__), 'rope_py3') |
17 path = os.path.join(os.path.dirname(__file__), 'rope_py3') |
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 sys.path.insert(0, path) |
20 sys.path.insert(0, path) |
21 |
|
22 ##try: |
|
23 ## bytes = unicode |
|
24 ## import StringIO as io # __IGNORE_EXCEPTION__ |
|
25 ##except NameError: |
|
26 ## import io # __IGNORE_WARNING__ |
|
27 ## |
|
28 ##import socket |
|
29 ##import select |
|
30 ##import traceback |
|
31 ##import time |
|
32 ##import json |
|
33 |
21 |
34 import rope.base.project |
22 import rope.base.project |
35 |
23 |
36 from JsonClient import JsonClient |
24 from JsonClient import JsonClient |
37 |
25 |
50 @type int |
38 @type int |
51 @param projectPath path to the project |
39 @param projectPath path to the project |
52 @type str |
40 @type str |
53 """ |
41 """ |
54 super(RefactoringClient, self).__init__(host, port) |
42 super(RefactoringClient, self).__init__(host, port) |
55 ## self.__connection = socket.create_connection((host, port)) |
|
56 |
43 |
57 from FileSystemCommands import RefactoringClientFileSystemCommands |
44 from FileSystemCommands import RefactoringClientFileSystemCommands |
58 self.__fsCommands = RefactoringClientFileSystemCommands(self) |
45 self.__fsCommands = RefactoringClientFileSystemCommands(self) |
59 |
46 |
60 self.__projectpath = projectPath |
47 self.__projectpath = projectPath |
61 self.__project = rope.base.project.Project( |
48 self.__project = rope.base.project.Project( |
62 self.__projectpath, fscommands=self.__fsCommands) |
49 self.__projectpath, fscommands=self.__fsCommands) |
|
50 |
|
51 self.__progressHandle = None |
63 |
52 |
64 def handleCall(self, method, params): |
53 def handleCall(self, method, params): |
65 """ |
54 """ |
66 Public method to handle a method call from the server. |
55 Public method to handle a method call from the server. |
67 |
56 |
72 """ |
61 """ |
73 ## if "filename" in params and sys.version_info[0] == 2: |
62 ## if "filename" in params and sys.version_info[0] == 2: |
74 ## params["filename"] = params["filename"].encode( |
63 ## params["filename"] = params["filename"].encode( |
75 ## sys.getfilesystemencoding()) |
64 ## sys.getfilesystemencoding()) |
76 |
65 |
77 if method == "ping": |
66 if method == "AbortAction": |
|
67 if self.__progressHandle is not None and \ |
|
68 not self.__progressHandle.is_stopped(): |
|
69 self.__progressHandle.stop() |
|
70 |
|
71 elif method == "ping": |
78 self.sendJson("pong", {}) |
72 self.sendJson("pong", {}) |
79 ## |
|
80 ## def sendJson(self, command, params): |
|
81 ## """ |
|
82 ## Public method to send a single refactoring command to the client. |
|
83 ## |
|
84 ## @param command command name to be sent |
|
85 ## @type str |
|
86 ## @param params dictionary of named parameters for the command |
|
87 ## @type dict |
|
88 ## """ |
|
89 ## import json |
|
90 ## |
|
91 ## commandDict = { |
|
92 ## "jsonrpc": "2.0", |
|
93 ## "method": command, |
|
94 ## "params": params, |
|
95 ## } |
|
96 ## cmd = json.dumps(commandDict) + '\n' |
|
97 ## self.__connection.sendall(cmd.encode('utf8', 'backslashreplace')) |
|
98 ## |
|
99 ## def __receiveJson(self): |
|
100 ## """ |
|
101 ## Private method to receive a JSON encode command and data from the |
|
102 ## server. |
|
103 ## """ |
|
104 ## line = self.__connection.recv(1024 * 1024, socket.MSG_PEEK) # 1M buffer |
|
105 ## |
|
106 ## eol = line.find(b'\n') |
|
107 ## |
|
108 ## if eol >= 0: |
|
109 ## size = eol + 1 |
|
110 ## |
|
111 ## # Now we know how big the line is, read it for real. |
|
112 ## line = self.__connection.recv(size).decode( |
|
113 ## 'utf8', 'backslashreplace') |
|
114 ## try: |
|
115 ## commandDict = json.loads(line.strip()) |
|
116 ## except (TypeError, ValueError) as err: |
|
117 ## self.sendJson("ClientException", { |
|
118 ## "ExceptionType": "ProtocolError", |
|
119 ## "ExceptionValue": str(err), |
|
120 ## "ProtocolData": line.strip(), |
|
121 ## }) |
|
122 ## return |
|
123 ## |
|
124 ## method = commandDict["method"] |
|
125 ## params = commandDict["params"] |
|
126 ## self.handleCall(method, params) |
|
127 ## |
|
128 ## def run(self): |
|
129 ## """ |
|
130 ## Public method implementing the main loop of the client. |
|
131 ## """ |
|
132 ## try: |
|
133 ## while True: |
|
134 ## try: |
|
135 ## rrdy, wrdy, xrdy = select.select( |
|
136 ## [self.__connection], [], []) |
|
137 ## except (select.error, KeyboardInterrupt, socket.error): |
|
138 ## # just carry on |
|
139 ## continue |
|
140 ## |
|
141 ## if self.__connection in rrdy: |
|
142 ## self.__receiveJson() |
|
143 ## |
|
144 ## except Exception: |
|
145 ## exctype, excval, exctb = sys.exc_info() |
|
146 ## tbinfofile = io.StringIO() |
|
147 ## traceback.print_tb(exctb, None, tbinfofile) |
|
148 ## tbinfofile.seek(0) |
|
149 ## tbinfo = tbinfofile.read() |
|
150 ## del exctb |
|
151 ## self.sendJson("ClientException", { |
|
152 ## "ExceptionType": str(exctype), |
|
153 ## "ExceptionValue": str(excval), |
|
154 ## "Traceback": tbinfo, |
|
155 ## }) |
|
156 ## |
|
157 ## # Give time to process latest response on server side |
|
158 ## time.sleep(0.5) |
|
159 ## self.__connection.shutdown(socket.SHUT_RDWR) |
|
160 ## self.__connection.close() |
|
161 |
73 |
162 if __name__ == '__main__': |
74 if __name__ == '__main__': |
163 if len(sys.argv) != 4: |
75 if len(sys.argv) != 4: |
164 print('Host, port and project path parameters are missing. Abort.') |
76 print('Host, port and project path parameters are missing. Abort.') |
165 sys.exit(1) |
77 sys.exit(1) |