-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpycompletionserver.py
More file actions
389 lines (307 loc) · 12.4 KB
/
pycompletionserver.py
File metadata and controls
389 lines (307 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#@PydevCodeAnalysisIgnore
'''
@author Fabio Zadrozny
'''
IS_PYTHON3K = 0
try:
import __builtin__
except ImportError:
import builtins as __builtin__ # Python 3.0
IS_PYTHON3K = 1
try:
True
False
except NameError:
#If it's not defined, let's define it now.
setattr(__builtin__, 'True', 1) #Python 3.0 does not accept __builtin__.True = 1 in its syntax
setattr(__builtin__, 'False', 0)
try:
from java.lang import Thread
IS_JYTHON = True
SERVER_NAME = 'jycompletionserver'
import _pydev_jy_imports_tipper #as _pydev_imports_tipper #changed to be backward compatible with 1.5
_pydev_imports_tipper = _pydev_jy_imports_tipper
except ImportError:
#it is python
IS_JYTHON = False
SERVER_NAME = 'pycompletionserver'
from threading import Thread
import _pydev_imports_tipper
import sys
if sys.platform == "darwin":
#See: https://sourceforge.net/projects/pydev/forums/forum/293649/topic/3454227
try:
import _CF #Don't fail if it doesn't work -- do it because it must be loaded on the main thread!
except:
pass
#initial sys.path
_sys_path = []
for p in sys.path:
#changed to be compatible with 1.5
_sys_path.append(p)
#initial sys.modules
_sys_modules = {}
for name, mod in sys.modules.items():
_sys_modules[name] = mod
import traceback
import time
try:
import StringIO
except:
import io as StringIO #Python 3.0
try:
from urllib import quote_plus, unquote_plus
except ImportError:
from urllib.parse import quote_plus, unquote_plus #Python 3.0
INFO1 = 1
INFO2 = 2
WARN = 4
ERROR = 8
DEBUG = INFO1 | ERROR
def dbg(s, prior):
if prior & DEBUG != 0:
sys.stdout.write('%s\n' % (s,))
# f = open('c:/temp/test.txt', 'a')
# print_ >> f, s
# f.close()
import pydev_localhost
HOST = pydev_localhost.get_localhost() # Symbolic name meaning the local host
MSG_KILL_SERVER = '@@KILL_SERVER_END@@'
MSG_COMPLETIONS = '@@COMPLETIONS'
MSG_END = 'END@@'
MSG_INVALID_REQUEST = '@@INVALID_REQUEST'
MSG_JYTHON_INVALID_REQUEST = '@@JYTHON_INVALID_REQUEST'
MSG_CHANGE_DIR = '@@CHANGE_DIR:'
MSG_OK = '@@MSG_OK_END@@'
MSG_BIKE = '@@BIKE'
MSG_PROCESSING = '@@PROCESSING_END@@'
MSG_PROCESSING_PROGRESS = '@@PROCESSING:%sEND@@'
MSG_IMPORTS = '@@IMPORTS:'
MSG_PYTHONPATH = '@@PYTHONPATH_END@@'
MSG_CHANGE_PYTHONPATH = '@@CHANGE_PYTHONPATH:'
MSG_SEARCH = '@@SEARCH'
BUFFER_SIZE = 1024
currDirModule = None
def CompleteFromDir(dir):
'''
This is necessary so that we get the imports from the same dir where the file
we are completing is located.
'''
global currDirModule
if currDirModule is not None:
del sys.path[currDirModule]
sys.path.insert(0, dir)
def ChangePythonPath(pythonpath):
'''Changes the pythonpath (clears all the previous pythonpath)
@param pythonpath: string with paths separated by |
'''
split = pythonpath.split('|')
sys.path = []
for path in split:
path = path.strip()
if len(path) > 0:
sys.path.append(path)
class KeepAliveThread(Thread):
def __init__(self, socket):
Thread.__init__(self)
self.socket = socket
self.processMsgFunc = None
self.lastMsg = None
def run(self):
time.sleep(0.1)
def send(s, msg):
if IS_PYTHON3K:
s.send(bytearray(msg, 'utf-8'))
else:
s.send(msg)
while self.lastMsg == None:
if self.processMsgFunc != None:
s = MSG_PROCESSING_PROGRESS % quote_plus(self.processMsgFunc())
sent = send(self.socket, s)
else:
sent = send(self.socket, MSG_PROCESSING)
if sent == 0:
sys.exit(0) #connection broken
time.sleep(0.1)
sent = send(self.socket, self.lastMsg)
if sent == 0:
sys.exit(0) #connection broken
class Processor:
def __init__(self):
# nothing to do
return
def removeInvalidChars(self, msg):
try:
msg = str(msg)
except UnicodeDecodeError:
pass
if msg:
try:
return quote_plus(msg)
except:
sys.stdout.write('error making quote plus in %s\n' % (msg,))
raise
return ' '
def formatCompletionMessage(self, defFile, completionsList):
'''
Format the completions suggestions in the following format:
@@COMPLETIONS(modFile(token,description),(token,description),(token,description))END@@
'''
compMsg = []
compMsg.append('%s' % defFile)
for tup in completionsList:
compMsg.append(',')
compMsg.append('(')
compMsg.append(str(self.removeInvalidChars(tup[0]))) #token
compMsg.append(',')
compMsg.append(self.removeInvalidChars(tup[1])) #description
if(len(tup) > 2):
compMsg.append(',')
compMsg.append(self.removeInvalidChars(tup[2])) #args - only if function.
if(len(tup) > 3):
compMsg.append(',')
compMsg.append(self.removeInvalidChars(tup[3])) #TYPE
compMsg.append(')')
return '%s(%s)%s' % (MSG_COMPLETIONS, ''.join(compMsg), MSG_END)
class T(Thread):
def __init__(self, thisP, serverP):
Thread.__init__(self)
self.thisPort = thisP
self.serverPort = serverP
self.socket = None #socket to send messages.
self.processor = Processor()
def connectToServer(self):
import socket
self.socket = s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((HOST, self.serverPort))
except:
sys.stderr.write('Error on connectToServer with parameters: host: %s port: %s\n' % (HOST, self.serverPort))
raise
def getCompletionsMessage(self, defFile, completionsList):
'''
get message with completions.
'''
return self.processor.formatCompletionMessage(defFile, completionsList)
def getTokenAndData(self, data):
'''
When we receive this, we have 'token):data'
'''
token = ''
for c in data:
if c != ')':
token = token + c
else:
break;
return token, data.lstrip(token + '):')
def run(self):
# Echo server program
try:
import socket
import _pydev_log
log = _pydev_log.Log()
dbg(SERVER_NAME + ' creating socket' , INFO1)
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, self.thisPort))
except:
sys.stderr.write('Error connecting with parameters: host: %s port: %s\n' % (HOST, self.serverPort))
raise
s.listen(1) #socket to receive messages.
#we stay here until we are connected.
#we only accept 1 client.
#the exit message for the server is @@KILL_SERVER_END@@
dbg(SERVER_NAME + ' waiting for connection' , INFO1)
conn, addr = s.accept()
time.sleep(0.5) #wait a little before connecting to JAVA server
dbg(SERVER_NAME + ' waiting to java client' , INFO1)
#after being connected, create a socket as a client.
self.connectToServer()
dbg(SERVER_NAME + ' Connected by ' + str(addr), INFO1)
while 1:
data = ''
returnMsg = ''
keepAliveThread = KeepAliveThread(self.socket)
while data.find(MSG_END) == -1:
received = conn.recv(BUFFER_SIZE)
if len(received) == 0:
sys.exit(0) #ok, connection ended
if IS_PYTHON3K:
data = data + received.decode('utf-8')
else:
data = data + received
try:
try:
if data.find(MSG_KILL_SERVER) != -1:
dbg(SERVER_NAME + ' kill message received', INFO1)
#break if we received kill message.
self.ended = True
sys.exit(0)
dbg(SERVER_NAME + ' starting keep alive thread', INFO2)
keepAliveThread.start()
if data.find(MSG_PYTHONPATH) != -1:
comps = []
for p in _sys_path:
comps.append((p, ' '))
returnMsg = self.getCompletionsMessage(None, comps)
else:
data = data[:data.rfind(MSG_END)]
if data.startswith(MSG_IMPORTS):
data = data.replace(MSG_IMPORTS, '')
data = unquote_plus(data)
defFile, comps = _pydev_imports_tipper.GenerateTip(data, log)
returnMsg = self.getCompletionsMessage(defFile, comps)
elif data.startswith(MSG_CHANGE_PYTHONPATH):
data = data.replace(MSG_CHANGE_PYTHONPATH, '')
data = unquote_plus(data)
ChangePythonPath(data)
returnMsg = MSG_OK
elif data.startswith(MSG_SEARCH):
data = data.replace(MSG_SEARCH, '')
data = unquote_plus(data)
(f, line, col), foundAs = _pydev_imports_tipper.Search(data)
returnMsg = self.getCompletionsMessage(f, [(line, col, foundAs)])
elif data.startswith(MSG_CHANGE_DIR):
data = data.replace(MSG_CHANGE_DIR, '')
data = unquote_plus(data)
CompleteFromDir(data)
returnMsg = MSG_OK
elif data.startswith(MSG_BIKE):
returnMsg = MSG_INVALID_REQUEST #No longer supported.
else:
returnMsg = MSG_INVALID_REQUEST
except SystemExit:
returnMsg = self.getCompletionsMessage(None, [('Exit:', 'SystemExit', '')])
keepAliveThread.lastMsg = returnMsg
raise
except:
dbg(SERVER_NAME + ' exception occurred', ERROR)
s = StringIO.StringIO()
traceback.print_exc(file=s)
err = s.getvalue()
dbg(SERVER_NAME + ' received error: ' + str(err), ERROR)
returnMsg = self.getCompletionsMessage(None, [('ERROR:', '%s\nLog:%s' % (err, log.GetContents()), '')])
finally:
log.Clear()
keepAliveThread.lastMsg = returnMsg
conn.close()
self.ended = True
sys.exit(0) #connection broken
except SystemExit:
raise
#No need to log SystemExit error
except:
s = StringIO.StringIO()
exc_info = sys.exc_info()
traceback.print_exception(exc_info[0], exc_info[1], exc_info[2], limit=None, file=s)
err = s.getvalue()
dbg(SERVER_NAME + ' received error: ' + str(err), ERROR)
raise
if __name__ == '__main__':
thisPort = int(sys.argv[1]) #this is from where we want to receive messages.
serverPort = int(sys.argv[2])#this is where we want to write messages.
t = T(thisPort, serverPort)
dbg(SERVER_NAME + ' will start', INFO1)
t.start()
time.sleep(5)
t.join()