Repository navigation
Expand file tree
/
Copy pathpython_completion_server.py
More file actions
121 lines (113 loc) · 4.09 KB
/
Copy pathpython_completion_server.py
File metadata and controls
121 lines (113 loc) · 4.09 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
import code
import sys
import os.path
import time
import stdlib_parser
from SimpleXMLRPCServer import SimpleXMLRPCServer
from pysmell import idehelper
from pysmell import tags
from re import split
def _uniquify(l):
found = set()
for item in l:
if item not in found:
yield item
found.add(item)
class AsyncServer:
def __init__(self,port):
# Create XMLRPC server
self.timeout=0.2
self._quit=False
self.port=port
self.std_lib=None
def run(self):
self.server = SimpleXMLRPCServer(("localhost", self.port))
self.server.logRequests=0
self.server.register_introspection_functions()
#self.server.socket.settimeout(self.timeout) ##timeouts cause probs
self.server.register_function(self.end,'end')
self.server.register_function(self.get_completions,'get_completions')
self.server.register_function(self.tag_gen,'tag_gen')
self.server.register_function(self.load_stdlib,'load_stdlib')
self.server.register_function(self.create_stdlib,'create_stdlib')
self.server.register_function(self.complete_phrase,'complete_phrase')
while not self._quit:
self.server.handle_request()
def load_stdlib(self,path):
print 'loading lib',path
self.stdlib=stdlib_parser.load(path)
return self.stdlib!=None
def create_stdlib(self,path):
print 'creating',path
self.stdlib=stdlib_parser.create(path)
return self.stdlib!=None
def complete_phrase(self,phrase):
print 'complete phrase',phrase
completions=[]
try:
for m in self.stdlib:
if m+'.'==phrase:
res=sorted([s for s in self.stdlib[m]])
print 'x',res
return res
if phrase.startswith(m+'.'):
res= sorted([s for s in self.stdlib[m] if s.startswith(phrase)])
print 'x',res
return res
# if m==phrase:
# return [phrase]
if m.startswith(phrase):
completions.append(m)
print 'x',completions
return sorted(completions)
except:
import traceback,sys
traceback.print_exception(*sys.exc_info())
print 'wtf'
def tool_tip(self,function):
return False
def tag_gen(self, projectDir):
args = ['pysmell', projectDir, '-o', os.path.join(projectDir, 'PYSMELLTAGS')]
sys.argv = args
tags.main()
return True
def get_completions(self, cur_file, source, line_no, cur_col):
"""arguments: fullPath, origSource, lineNo, origCol, matcher
When visiting the file at fullPath, with edited source origSource, find a list
of possible completion strings for the symbol located at origCol on orgLineNo using
matching mode matcher"""
print cur_file,line_no,cur_col
if not cur_file:
write('No filename - is the file saved?')
return TOOLTIP
source = file(cur_file,'r').read()
PYSMELLDICT = idehelper.findPYSMELLDICT(cur_file)
if PYSMELLDICT is None:
write('No PYSMELLTAGS found - you have to generate one.')
return TOOLTIP
print 'found dict'
line = source.splitlines()[line_no - 1]
index = idehelper.findBase(line, cur_col)
base = line[index:cur_col]
options = idehelper.detectCompletionType(cur_file, source, line_no, cur_col, base, PYSMELLDICT)
completions = idehelper.findCompletions(base, PYSMELLDICT, options)
return completions
def end(self):
self._quit=True
return True
def cmd_err():
print 'Correct usage: python_completion_server.py <port>'
print '<port> must be a positive integer'
exit()
if __name__=='__main__':
if len(sys.argv)!=2:
port=8000
else:
try:
port = int(sys.argv[1])
except:
cmd_err()
if port<=0:
cmd_err()
server=AsyncServer(port)
server.run()