[Pydev-cvs] org.python.pydev/PySrc test_refactoring.py,NONE,1.1 test_pyserver.py,1.4,1.5 pycompletio
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2004-09-15 17:36:17
|
Update of /cvsroot/pydev/org.python.pydev/PySrc In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25438/PySrc Modified Files: test_pyserver.py pycompletionserver.py refactoring.py Added Files: test_refactoring.py Log Message: Making refactoring. Index: pycompletionserver.py =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/PySrc/pycompletionserver.py,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** pycompletionserver.py 14 Sep 2004 17:42:07 -0000 1.6 --- pycompletionserver.py 15 Sep 2004 17:36:08 -0000 1.7 *************** *** 5,8 **** --- 5,10 ---- import time import simpleTipper + import refactoring + import sys HOST = '127.0.0.1' # Symbolic name meaning the local host *************** *** 18,24 **** MSG_CHANGE_DIR = '@@CHANGE_DIR:' MSG_OK = '@@MSG_OK_END@@' ! BUFFER_SIZE = 1024 * 4 class T(threading.Thread): --- 20,43 ---- MSG_CHANGE_DIR = '@@CHANGE_DIR:' MSG_OK = '@@MSG_OK_END@@' + MSG_REFACTOR = '@@REFACTOR' + MSG_PROCESSING = '@@PROCESSING_END@@' ! BUFFER_SIZE = 1024 + class KeepAliveThread(threading.Thread): + def __init__(self, socket): + threading.Thread.__init__(self) + self.socket = socket + self.lastMsg = None + + def run(self): + time.sleep(0.1) + while self.lastMsg == None: + print 'sending', MSG_PROCESSING + self.socket.send(MSG_PROCESSING) + time.sleep(0.1) + print 'sending', self.lastMsg + self.socket.send(self.lastMsg) + class T(threading.Thread): *************** *** 55,70 **** 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 sendOkMsg(self): - self.socket.send(MSG_OK) - def getTokenAndData(self, data): ''' --- 74,83 ---- return '%s(%s)%s'%(MSG_COMPLETIONS, compMsg, MSG_END) ! def getCompletionsMessage(self, completionsList): ''' ! get message with completions. ''' ! return self.formatCompletionMessage(completionsList) def getTokenAndData(self, data): ''' *************** *** 103,145 **** 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; ! ! elif MSG_RELOAD_MODULES in data: ! simpleTipper.ReloadModules() ! self.sendOkMsg() ! ! else: ! data = data[:data.find(MSG_END)] ! if MSG_GLOBALS in data: ! data = data.replace(MSG_GLOBALS, '') ! comps = simpleTipper.GenerateTip(data, None, False) ! 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, False) ! self.sendCompletionsMessage(comps) ! elif MSG_CLASS_GLOBALS in data: ! data = data.replace(MSG_CLASS_GLOBALS, '') ! token, data = self.getTokenAndData(data) ! comps = simpleTipper.GenerateTip(data, token, True) ! self.sendCompletionsMessage(comps) - elif MSG_CHANGE_DIR in data: - data = data.replace(MSG_CHANGE_DIR, '') - simpleTipper.CompleteFromDir(data) - self.sendOkMsg() - else: ! self.sendReceivedInvalidMessage() ! conn.close() --- 116,169 ---- while 1: data = '' ! returnMsg = '' ! keepAliveThread = KeepAliveThread(self.socket) ! while not data.endswith(MSG_END): ! data += conn.recv(BUFFER_SIZE) ! try: ! if MSG_KILL_SERVER in data: ! #break if we received kill message. ! break; ! keepAliveThread.start() ! ! if MSG_RELOAD_MODULES in data: ! simpleTipper.ReloadModules() ! returnMsg = MSG_OK else: ! data = data[:data.find(MSG_END)] ! ! if data.startswith(MSG_GLOBALS): ! data = data.replace(MSG_GLOBALS, '') ! comps = simpleTipper.GenerateTip(data, None, False) ! returnMsg = self.getCompletionsMessage(comps) ! ! elif data.startswith(MSG_TOKEN_GLOBALS ): ! data = data.replace(MSG_TOKEN_GLOBALS, '') ! token, data = self.getTokenAndData(data) ! comps = simpleTipper.GenerateTip(data, token, False) ! returnMsg = self.getCompletionsMessage(comps) ! ! elif data.startswith(MSG_CLASS_GLOBALS ): ! data = data.replace(MSG_CLASS_GLOBALS, '') ! token, data = self.getTokenAndData(data) ! comps = simpleTipper.GenerateTip(data, token, True) ! returnMsg = self.getCompletionsMessage(comps) ! ! elif data.startswith(MSG_CHANGE_DIR ): ! data = data.replace(MSG_CHANGE_DIR, '') ! simpleTipper.CompleteFromDir(data) ! returnMsg = MSG_OK ! ! elif data.startswith(MSG_REFACTOR): ! data = data.replace(MSG_REFACTOR, '') ! returnMsg = refactoring.HandleRefactorMessage(data) ! ! else: ! returnMsg = MSG_INVALID_REQUEST ! finally: ! keepAliveThread.lastMsg = returnMsg conn.close() *************** *** 147,156 **** 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() --- 171,184 ---- if __name__ == '__main__': + #let's log this!! + out = open('c:/temp/pydev.log', 'w') + sys.stdout = out + sys.stderr = out 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) + print 'will start' t.start() --- NEW FILE: test_refactoring.py --- ''' Refactoring tests. ''' from coilib import unittest import refactoring import os #=============================================================================== # delete #=============================================================================== def delete(filename): '''Removes filename, or does nothing if the file doesn't exist. ''' try: os.remove(filename) except OSError: pass #================================================================================ # createFile #================================================================================ def createFile(filename, contents='', flag='w'): '''Creates the given filename with the given contents. ''' f = file(filename, flag) f.write(contents) f.close() FILE = 'temporary_file.py' class Test(unittest.TestCase): def getInitialFile(self): s = \ ''' class C: def a(self): a = 2 b = 3 c = a+b #this should be refactored. return c ''' return s def getRefactoredFile(self): s = \ ''' class C: def a(self): a = 2 b = 3 c = self.plusMet(a, b) return c def plusMet(self, a, b): c = a+b #this should be refactored. return c ''' return s def setUp(self): unittest.TestCase.setUp(self) createFile(FILE, self.getInitialFile()) def tearDown(self): unittest.TestCase.tearDown(self) delete(FILE) def testIt(self): r = refactoring.Refactoring() s = r.extractMethod(FILE, 5+1, 0, 5+1, 44, 'plusMet') f = file(FILE, 'r') contents = f.read() f.close() self.assertEquals(contents, self.getRefactoredFile()) if __name__ == '__main__': unittest.main() Index: test_pyserver.py =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/PySrc/test_pyserver.py,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** test_pyserver.py 14 Sep 2004 17:42:07 -0000 1.4 --- test_pyserver.py 15 Sep 2004 17:36:08 -0000 1.5 *************** *** 34,38 **** self.assertEquals('@@COMPLETIONS((Def,description),(Def1,description1),(Def2,description2))END@@', msg) ! def testSocketsAndMessages(self): t = pycompletionserver.T(50002,50003) --- 34,41 ---- self.assertEquals('@@COMPLETIONS((Def,description),(Def1,description1),(Def2,description2))END@@', msg) ! def createConnections(self): ! ''' ! Creates the connections needed for testing. ! ''' t = pycompletionserver.T(50002,50003) *************** *** 47,55 **** connToRead, addr = sToRead.accept() #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@@', --- 50,75 ---- connToRead, addr = sToRead.accept() + + return t, sToWrite, sToRead, connToRead, addr + + # def testRefactoring(self): + # t, sToWrite, sToRead, connToRead, addr = self.createConnections() + # self. + + def readMsg(self): + msg = '@@PROCESSING_END@@' + while msg == '@@PROCESSING_END@@': + msg = self.connToRead.recv(1024*4) + + return msg + + def testSocketsAndMessages(self): + t, sToWrite, sToRead, self.connToRead, addr = self.createConnections() #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 = self.readMsg() ! self.assertEquals('@@COMPLETIONS((math,This module is always available. It provides access to the\n'\ 'mathematical functions defined by the C standard.))END@@', *************** *** 59,63 **** #check token msg. sToWrite.send('@@TOKEN_GLOBALS(math):import math\nEND@@') ! completions = connToRead.recv(4086) self.assert_('@@COMPLETIONS' in completions) --- 79,83 ---- #check token msg. sToWrite.send('@@TOKEN_GLOBALS(math):import math\nEND@@') ! completions = self.readMsg() self.assert_('@@COMPLETIONS' in completions) *************** *** 83,90 **** sToWrite.send('@@TOKEN_GLOBALS(C):%s\nEND@@'%s) ! completions = connToRead.recv(4086) sToWrite.send('@@CLASS_GLOBALS(C):%s\nEND@@'%s) ! completions2 = connToRead.recv(4086) self.assert_(len(completions) != len(completions2)) --- 103,110 ---- sToWrite.send('@@TOKEN_GLOBALS(C):%s\nEND@@'%s) ! completions = self.readMsg() sToWrite.send('@@CLASS_GLOBALS(C):%s\nEND@@'%s) ! completions2 = self.readMsg() self.assert_(len(completions) != len(completions2)) *************** *** 92,96 **** #reload modules test # sToWrite.send('@@RELOAD_MODULES_END@@') ! # ok = connToRead.recv(4086) # self.assertEquals('@@MSG_OK_END@@' , ok) # this test is not executed because it breaks our current enviroment. --- 112,116 ---- #reload modules test # sToWrite.send('@@RELOAD_MODULES_END@@') ! # ok = self.readMsg() # self.assertEquals('@@MSG_OK_END@@' , ok) # this test is not executed because it breaks our current enviroment. *************** *** 109,116 **** self.assert_(newDir != None) sToWrite.send('@@CHANGE_DIR:%sEND@@'%newDir) ! ok = connToRead.recv(4086) self.assertEquals('@@MSG_OK_END@@' , ok) sToWrite.send('@@TOKEN_GLOBALS(math acos):import math\nEND@@') ! completions = connToRead.recv(4086) self.sendKillMsg(sToWrite) --- 129,136 ---- self.assert_(newDir != None) sToWrite.send('@@CHANGE_DIR:%sEND@@'%newDir) ! ok = self.readMsg() self.assertEquals('@@MSG_OK_END@@' , ok) sToWrite.send('@@TOKEN_GLOBALS(math acos):import math\nEND@@') ! completions = self.readMsg() self.sendKillMsg(sToWrite) Index: refactoring.py =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/PySrc/refactoring.py,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** refactoring.py 14 Sep 2004 17:42:07 -0000 1.1 --- refactoring.py 15 Sep 2004 17:36:08 -0000 1.2 *************** *** 3,7 **** import sys import os ! --- 3,8 ---- import sys import os ! import traceback ! import StringIO *************** *** 22,31 **** """ self.brmctx = bike.init() ! self.logging = Preferences.getRefactoring("Logging") ! if self.logging: ! self.brmctx.setProgressLogger(self.ui.stdout) ! else: ! self.brmctx.setProgressLogger(SilentLogger()) ! self.brmctx.setWarningLogger(self.ui.stderr) def handleReset(self): --- 23,28 ---- """ self.brmctx = bike.init() ! self.brmctx.setProgressLogger(sys.stdout) ! self.brmctx.setWarningLogger(sys.stderr) def handleReset(self): *************** *** 35,39 **** --- 32,83 ---- self.init() + + def extractMethod(self, filename, startline, startcolumn, + endline, endcolumn, newname): + ''' + Receives all as a string and changes to correct type. + ''' + self.brmctx.extractMethod(filename, int(startline), int(startcolumn), + int(endline), int(endcolumn), + newname) + savedfiles = self.brmctx.save() + return str(savedfiles) + + def renameByCoordinates(self, filename, line, column, newname): + ''' + Receives all as a string and changes to correct type. + ''' + self.brmctx.renameByCoordinates(filename, int(line), int(column), newname) + savedfiles = self.brmctx.save() + return str(savedfiles) + + __Refactoring = None + + def GetRefactorer(): + global __Refactoring + if __Refactoring is None: + __Refactoring = Refactoring() + return __Refactoring + + def HandleRefactorMessage(msg): + ''' + The message received should have: the method of the class + ''' + msgSplit = msg.split(' ') + func = msgSplit.pop(0) + + func = getattr(GetRefactorer(), func) + + try: + return func(*msgSplit)+'END@@' + except: + import sys + s = StringIO.StringIO() + exc_info = sys.exc_info() + print >> s, str(exc_info[1]) + traceback.print_exception(exc_info[0], exc_info[1], exc_info[2], limit=None, file = s) + return 'ERROR: %s\nEND@@'%(s.getvalue()) + |