[Pydev-cvs] org.python.pydev/PySrc simpleinspect.py,NONE,1.1 test_pyserver.py,NONE,1.1 simpleTipper.
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2004-09-10 19:42:54
|
Update of /cvsroot/pydev/org.python.pydev/PySrc In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19681/PySrc Modified Files: pycompletionserver.py Added Files: simpleinspect.py test_pyserver.py simpleTipper.py test_simpleTipper.py Log Message: Making server for code completion and making another enviroment, since iterative console is not really suited for code completion. --- NEW FILE: test_simpleTipper.py --- ''' @author Fabio Zadrozny ''' import unittest import simpleTipper class Test(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) def tearDown(self): unittest.TestCase.tearDown(self) def getDoc1(self): s = \ ''' import math class C(object): \'\'\' CDescription \'\'\' def __init__(self): print dir(self) def a(self): \'\'\' ADescription \'\'\' pass def b(self): self ''' return s def testEnv1(self): comps = simpleTipper.GenerateTip(self.getDoc1(), None) import math, inspect checkedMath = False checkedC = False for tup in comps: if tup[0] == 'math': checkedMath = True self.assertEquals(inspect.getdoc(math),tup[1]) elif tup[0] == 'C': checkedC = True self.assert_('CDescription' in tup[1]) self.assert_(checkedC and checkedMath) def testEnv1CToken(self): comps = simpleTipper.GenerateTip(self.getDoc1(), 'C') checkedA = False for tup in comps: if tup[0] == 'a': checkedA = True self.assert_('ADescription' in tup[1]) self.assert_(checkedA) if __name__ == '__main__': unittest.main() --- NEW FILE: simpleinspect.py --- ''' @author Fabio Zadrozny ''' def GenerateTip (__eraseThisV): exec(__eraseThisV) --- NEW FILE: simpleTipper.py --- ''' @author Fabio Zadrozny ''' def GenerateTip (theDoc, token): ''' Put in the doc the code so that we get the locals. ''' if token is None: theDoc+= \ ''' import inspect as __eraseThisinspect import copy as __eraseThiscopy __eraseThisf = __eraseThisinspect.currentframe() __eraseThislocs = __eraseThiscopy.copy(__eraseThisf.f_locals) for __eraseThisd in __eraseThislocs: if __eraseThisd.startswith('__eraseThis') == False : __eraseThisTips.append([__eraseThisd,None]) l = locals() for t in __eraseThisTips: t[1] = __eraseThisinspect.getdoc(l[t[0]]) ''' else : #just complete for token. theDoc+= \ ''' import inspect for d in dir(%s): __eraseThisTips.append([d,inspect.getdoc(getattr(%s, d))]) ''' % (token,token) import simpleinspect import compiler __eraseThis = compiler.compile(theDoc, 'temporary', 'exec') simpleinspect.__eraseThisTips = [] simpleinspect.GenerateTip (__eraseThis) toReturn = simpleinspect.__eraseThisTips simpleinspect.__eraseThisTips = [] return toReturn --- NEW FILE: test_pyserver.py --- ''' @author Fabio Zadrozny ''' import unittest import pycompletionserver import socket class Test(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) def tearDown(self): unittest.TestCase.tearDown(self) def testMessage(self): t = pycompletionserver.T(0,0) l = [] l.append(('Def','description' )) l.append(('Def1','description1')) l.append(('Def2','description2')) msg = t.formatCompletionMessage(l) self.assertEquals('@@COMPLETIONS((Def,description),(Def1,description1),(Def2,description2))END@@', msg) l = [] l.append(('Def','desc,,r,,i()ption' )) l.append(('Def(1','descriptio(n1')) l.append(('De,f)2','de,s,c,ription2')) msg = t.formatCompletionMessage(l) self.assertEquals('@@COMPLETIONS((Def,description),(Def1,description1),(Def2,description2))END@@', msg) def testSocketsAndMessages(self): t = pycompletionserver.T(50002,50003) t.start() sToWrite = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sToWrite.connect((pycompletionserver.HOST, 50002)) sToRead = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sToRead.bind((pycompletionserver.HOST, 50003)) sToRead.listen(1) #socket to receive messages. connToRead, addr = sToRead.accept() # print 'test connected addr', addr #now that we have the connections all set up, check the code completion messages. sToWrite.send('@@GLOBALS:import math\nEND@@') #only 1 global should be returned: math itself. completions = connToRead.recv(1024) self.assertEquals('@@COMPLETIONS((math,This module is always available. It provides access to the\n'\ 'mathematical functions defined by the C standard.))END@@', completions) #check token msg. sToWrite.send('@@TOKEN_GLOBALS(math):import math\nEND@@') completions = connToRead.recv(4086) self.assert_('@@COMPLETIONS' in completions) self.assert_('END@@' in completions) self.sendKillMsg(sToWrite) while not hasattr(t, 'ended'): pass #wait until it receives the message and quits. sToRead.close() sToWrite.close() def sendKillMsg(self, socket): socket.send(pycompletionserver.MSG_KILL_SERVER) if __name__ == '__main__': unittest.main() Index: pycompletionserver.py =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/PySrc/pycompletionserver.py,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** pycompletionserver.py 9 Sep 2004 16:16:20 -0000 1.2 --- pycompletionserver.py 10 Sep 2004 19:42:23 -0000 1.3 *************** *** 1,13 **** ''' ! Echoing server. ! ! TODO: THIS IS ONLY A TEST. ''' import threading import time ! ! from tipper import GenerateTip class T(threading.Thread): def run(self): --- 1,75 ---- ''' ! @author Fabio Zadrozny ''' import threading import time ! import simpleTipper ! ! HOST = '127.0.0.1' # Symbolic name meaning the local host ! ! ! MSG_KILL_SERVER = '@@KILL_SERVER_END@@' ! MSG_COMPLETIONS = '@@COMPLETIONS' ! MSG_END = 'END@@' ! MSG_GLOBALS = '@@GLOBALS:' ! MSG_TOKEN_GLOBALS = '@@TOKEN_GLOBALS(' ! MSG_INVALID_REQUEST = '@@INVALID_REQUEST' ! ! BUFFER_SIZE = 1024 class T(threading.Thread): + + def __init__(self, thisPort, serverPort): + threading.Thread.__init__(self) + self.thisPort = thisPort + self.serverPort = serverPort + self.socket = None #socket to send messages. + + + def connectToServer(self): + import socket + + self.socket = s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect((HOST, self.serverPort)) + + def removeInvalidChars(self, msg): + return msg.replace(',','').replace('(','').replace(')','') + + def formatCompletionMessage(self, completionsList): + ''' + Format the completions suggestions in the following format: + @@COMPLETIONS((token,description),(token,description),(token,description))END@@ + ''' + compMsg = '' + for tup in completionsList: + if compMsg != '': + compMsg += ',' + + compMsg += '(%s,%s)' % (self.removeInvalidChars(tup[0]),self.removeInvalidChars(tup[1])) + + return '%s(%s)%s'%(MSG_COMPLETIONS, compMsg, MSG_END) + + def sendCompletionsMessage(self, completionsList): + ''' + Send message with completions. + ''' + self.socket.send(self.formatCompletionMessage(completionsList)) + + def sendReceivedInvalidMessage(self): + self.socket.send(MSG_INVALID_REQUEST) + + def getTokenAndData(self, data): + ''' + When we receive this, we have 'token):data' + ''' + token = '' + for c in data: + if c != ')': + token += c + else: + break; + + return token, data.lstrip(token+'):') + def run(self): *************** *** 15,38 **** import socket - HOST = '' # Symbolic name meaning the local host - PORT = 50007 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ! s.bind((HOST, PORT)) ! s.listen(1) conn, addr = s.accept() - #print 'Connected by', addr while 1: ! data = conn.recv(1024) ! if not data: ! break ! r = '' ! for d in GenerateTip(data): ! r += d ! r += '|' ! #print 'sending data:' , data ! conn.send(r) conn.close() --- 77,123 ---- import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ! s.bind((HOST, self.thisPort)) ! 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@@ conn, addr = s.accept() + + #after being connected, create a socket as a client. + self.connectToServer() + + # print 'pycompletionserver Connected by', addr + while 1: ! data = '' ! while not data.endswith(MSG_END): ! data += conn.recv(BUFFER_SIZE) ! if MSG_KILL_SERVER in data: ! #break if we received kill message. ! break; ! else: ! data = data.rstrip(MSG_END) ! if MSG_GLOBALS in data: ! data = data.replace(MSG_GLOBALS, '') ! comps = simpleTipper.GenerateTip(data, None) ! self.sendCompletionsMessage(comps) ! ! elif MSG_TOKEN_GLOBALS in data: ! data = data.replace(MSG_TOKEN_GLOBALS, '') ! token, data = self.getTokenAndData(data) ! comps = simpleTipper.GenerateTip(data, token) ! self.sendCompletionsMessage(comps) ! ! else: ! self.sendReceivedInvalidMessage() ! ! ! conn.send(data) conn.close() *************** *** 40,71 **** if __name__ == '__main__': ! t = T() t.start() - - while(hasattr(t, 'ended') == False): - time.sleep(1) - - # # Echo client program - # import socket - # - # HOST = '127.0.0.1' # The remote host - # PORT = 50007 # The same port as used by the server - # s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - # s.connect((HOST, PORT)) - # s.send('Hello, world') - # data = s.recv(1024) - # print 'Received', `data` - # - # - # s.send('Hello, world again ') - # data = s.recv(1024) - # print 'Received', `data` - # - # s.send('Hello, world once more') - # data = s.recv(1024) - # print 'Received', `data` - # - # s.close() - # time.sleep(5) - \ No newline at end of file --- 125,134 ---- if __name__ == '__main__': ! ! import sys ! 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) t.start() |