def rename(self, src, line, col, newname): writeTmpTestFile(src) ctx = bike.init() ctx.renameByCoordinates(os.path.abspath(tmpfile), line, col, newname) ctx.save() newsrc = readFile(tmpfile) return newsrc
def rename(self, src, line, col, newname): writeTmpTestFile(src) ctx = bike.init() ctx.renameByCoordinates(os.path.abspath(tmpfile),line,col,newname) ctx.save() newsrc = readFile(tmpfile) return newsrc
def test_undoesTwoConsecutiveRefactorings(self): try: src = trimLines(""" class a: def foo(self): pass """) writeTmpTestFile(src) ctx = bike.init() ctx.renameByCoordinates(tmpfile, 2, 8, "c") ctx.save() newsrc1 = readFile(tmpfile) ctx.renameByCoordinates(tmpfile, 2, 8, "d") ctx.save() # 1st undo ctx.undo() ctx.save() newsrc = readFile(tmpfile) self.assertEqual(newsrc, newsrc1) # 2nd undo ctx.undo() ctx.save() newsrc = readFile(tmpfile) self.assertEqual(newsrc, src) finally: pass
def test_extractsFunction(self): srcBefore = trimLines(""" def myFunction(): # comment a = 3 c = a + 99 b = c * 1 print b """) srcAfter = trimLines(""" def myFunction(): # comment a = 3 b = newFunction(a) print b def newFunction(a): c = a + 99 b = c * 1 return b """) writeTmpTestFile(srcBefore) ctx = bike.init() ctx.extractMethod(os.path.abspath(tmpfile), 3, 4, 4, 13, "newFunction") ctx.save() self.assertEqual(readTmpTestFile(), srcAfter) ctx.undo() ctx.save() self.assertEqual(readTmpTestFile(), srcBefore)
def test_extractsFunction(self): srcBefore=trimLines(""" def myFunction(): # comment a = 3 c = a + 99 b = c * 1 print b """) srcAfter=trimLines(""" def myFunction(): # comment a = 3 b = newFunction(a) print b def newFunction(a): c = a + 99 b = c * 1 return b """) writeTmpTestFile(srcBefore) ctx = bike.init() ctx.extractMethod(os.path.abspath(tmpfile),3,4,4,13,"newFunction") ctx.save() self.assertEqual(readTmpTestFile(),srcAfter) ctx.undo() ctx.save() self.assertEqual(readTmpTestFile(),srcBefore)
def test_undoesTwoConsecutiveRefactorings(self): try: src = trimLines(""" class a: def foo(self): pass """) writeTmpTestFile(src) ctx = bike.init() ctx.renameByCoordinates(tmpfile,2,8,"c") ctx.save() newsrc1 = readFile(tmpfile) ctx.renameByCoordinates(tmpfile,2,8,"d") ctx.save() # 1st undo ctx.undo() ctx.save() newsrc = readFile(tmpfile) self.assertEqual(newsrc, newsrc1) # 2nd undo ctx.undo() ctx.save() newsrc = readFile(tmpfile) self.assertEqual(newsrc,src) finally: pass
def __init__(self, model): self.model = model # the first one created starts the context if not hasattr(model.editor, 'brm_context') or model.editor.brm_context is None: model.editor.brm_context = context = bike.init() context.setProgressLogger( globals()[Preferences.brmProgressLogger](model.editor))
def renameMethod(self, src, line, col, newname): writeTmpTestFile(src) ctx = bike.init() ctx.setRenameMethodPromptCallback(self.callback) ctx.renameByCoordinates(tmpfile, line, col, newname) ctx.save() newsrc = readFile(tmpfile) return newsrc
def renameMethod(self, src, line, col, newname): writeTmpTestFile(src) ctx = bike.init() ctx.setRenameMethodPromptCallback(self.callback) ctx.renameByCoordinates(tmpfile,line,col,newname) ctx.save() newsrc = readFile(tmpfile) return newsrc
def test_setCompletePythonPath_removesDuplicates(self): origpath = sys.path try: sys.path = ["foobah"] ctx = bike.init() ctx._setCompletePythonPath(sys.path[-1]) self.assertEqual(1,ctx._getCurrentSearchPath().count(sys.path[-1])) finally: sys.path = origpath
def test_setCompletePythonPath_removesDuplicates(self): origpath = sys.path try: sys.path = ["foobah"] ctx = bike.init() ctx._setCompletePythonPath(sys.path[-1]) self.assertEqual(1, ctx._getCurrentSearchPath().count(sys.path[-1])) finally: sys.path = origpath
def __init__(self, window): self.ctx = bike.init() self.tmpBufferPath = '/tmp/gedit_bicyclerepairman_buffer.py' self.window = window self.encoding = gedit.gedit_encoding_get_current() self.logger = BikeLogger() self.ctx.setProgressLogger(self.logger) self.ctx.setWarningLogger(self.logger)
def renameFunction(self, src, newname): try: createPackageStructure(src, FunctionTestdata) ctx = bike.init() ctx.rename(pkgstructureFile2, 1, 4, newname) ctx.save() newsrc = readFile(pkgstructureFile1) return newsrc finally: removePackageStructure()
def renameClass(self, src, newname): try: createPackageStructure(src, TheClassTestdata) ctx = bike.init() ctx.renameByCoordinates(pkgstructureFile2,1,6,newname) ctx.save() newsrc = readFile(pkgstructureFile1) return newsrc finally: removePackageStructure()
def renameMethod(self, src, line, col, newname): try: createPackageStructure(src, MethodTestdata) ctx = bike.init() ctx.renameByCoordinates(pkgstructureFile2, line, col, newname) ctx.save() newsrc = readFile(pkgstructureFile1) return newsrc finally: removePackageStructure()
def renameClass(self, src, newname): try: createPackageStructure(src, TheClassTestdata) ctx = bike.init() ctx.renameByCoordinates(pkgstructureFile2, 1, 6, newname) ctx.save() newsrc = readFile(pkgstructureFile1) return newsrc finally: removePackageStructure()
def __init__(self, window): self.ctx = bike.init() self.tmpBufferPath = '/tmp/gedit_bicyclerepairman_buffer.py' self.window = window self.encoding = gedit.encoding_get_current() self.logger = BikeLogger() self.ctx.setProgressLogger(self.logger) self.ctx.setWarningLogger(self.logger)
def renameMethod(self, src, line, col, newname): try: createPackageStructure(src, MethodTestdata) ctx = bike.init() ctx.renameByCoordinates(pkgstructureFile2,line,col,newname) ctx.save() newsrc = readFile(pkgstructureFile1) return newsrc finally: removePackageStructure()
def test_findsSomethingInThePythonPath(self): src = trimLines(""" import compiler.ast compiler.ast.AssName """) ctx = bike.init() writeTmpTestFile(src) defn = [x for x in ctx.findDefinition(tmpfile, 2, 13)] assert len(defn) != 0 self.assertEqual("/usr/lib/python2.4/compiler/ast.py", defn[0].filename)
def test_setNonLibPathonPath_removesLibDirectories(self): origpath = sys.path try: writeTmpTestFile("pass") libdir = os.path.join(sys.prefix,"lib","python"+sys.version[:3]) sys.path = [libdir,os.path.join(libdir,"site-packages")] ctx = bike.init() ctx._setNonLibPythonPath(tmproot) self.assertEqual([tmproot],ctx._getCurrentSearchPath()) finally: sys.path = origpath
def test_findsClassRef(self): src = trimLines(""" class TheClass: pass a = TheClass() """) writeTmpTestFile(src) ctx = bike.init() defn = [x for x in ctx.findDefinitionByCoordinates(tmpfile, 3, 6)] assert defn[0].filename == os.path.abspath(tmpfile) assert defn[0].lineno == 1 assert defn[0].confidence == 100
def test_returnsReferences(self): src = trimLines(""" class TheClass: pass a = TheClass() """) writeTmpTestFile(src) ctx = bike.init() refs = [refs for refs in ctx.findReferencesByCoordinates(tmpfile,1,6)] self.assertEqual(refs[0].filename,os.path.abspath(tmpfile)) self.assertEqual(refs[0].lineno,3) assert hasattr(refs[0],"confidence")
def test_setNonLibPathonPath_removesLibDirectories(self): origpath = sys.path try: writeTmpTestFile("pass") libdir = os.path.join(sys.prefix, "lib", "python" + sys.version[:3]) sys.path = [libdir, os.path.join(libdir, "site-packages")] ctx = bike.init() ctx._setNonLibPythonPath(tmproot) self.assertEqual([tmproot], ctx._getCurrentSearchPath()) finally: sys.path = origpath
def test_findsClassRef(self): src=trimLines(""" class TheClass: pass a = TheClass() """) writeTmpTestFile(src) ctx = bike.init() defn = [x for x in ctx.findDefinitionByCoordinates(tmpfile,3,6)] assert defn[0].filename == os.path.abspath(tmpfile) assert defn[0].lineno == 1 assert defn[0].confidence == 100
def test_returnsReferences(self): src = trimLines(""" class TheClass: pass a = TheClass() """) writeTmpTestFile(src) ctx = bike.init() refs = [ refs for refs in ctx.findReferencesByCoordinates(tmpfile, 1, 6) ] self.assertEqual(refs[0].filename, os.path.abspath(tmpfile)) self.assertEqual(refs[0].lineno, 3) assert hasattr(refs[0], "confidence")
def test_works(self): srcBefore = trimLines(""" def foo(): b = 'hello' print b """) srcAfter = trimLines(""" def foo(): print 'hello' """) writeTmpTestFile(srcBefore) ctx = bike.init() ctx.inlineLocalVariable(tmpfile, 3, 10) ctx.save() self.assertEqual(file(tmpfile).read(), srcAfter)
def test_works(self): srcBefore=trimLines(""" def foo(): b = 'hello' print b """) srcAfter=trimLines(""" def foo(): print 'hello' """) writeTmpTestFile(srcBefore) ctx = bike.init() ctx.inlineLocalVariable(tmpfile,3,10) ctx.save() self.assertEqual(file(tmpfile).read(),srcAfter)
def test_undoesTheTextOfASingleFile(self): src = trimLines(""" class a: def foo(self): pass """) writeTmpTestFile(src) #ctx = bike.init() ctx = bike.init() ctx.renameByCoordinates(tmpfile,2,8,"c") ctx.save() ctx.undo() ctx.save() newsrc = readFile(tmpfile) self.assertEqual(newsrc,src)
def test_undoesTheTextOfASingleFile(self): src = trimLines(""" class a: def foo(self): pass """) writeTmpTestFile(src) #ctx = bike.init() ctx = bike.init() ctx.renameByCoordinates(tmpfile, 2, 8, "c") ctx.save() ctx.undo() ctx.save() newsrc = readFile(tmpfile) self.assertEqual(newsrc, src)
def test_works(self): srcBefore = trimLines(""" def foo(): print 3 + 2 """) srcAfter = trimLines(""" def foo(): a = 3 + 2 print a """) try: writeTmpTestFile(srcBefore) ctx = bike.init() ctx.extractLocalVariable(tmpfile, 2, 10, 2, 15, 'a') ctx.save() self.assertEqual(file(tmpfile).read(), srcAfter) finally: pass
def test_works(self): srcBefore=trimLines(""" def foo(): print 3 + 2 """) srcAfter=trimLines(""" def foo(): a = 3 + 2 print a """) try: writeTmpTestFile(srcBefore) ctx = bike.init() ctx.extractLocalVariable(tmpfile,2,10,2,15,'a') ctx.save() self.assertEqual(file(tmpfile).read(),srcAfter) finally: pass
def can_perform_the_extract_method_refactoring(self): srcBefore=trimLines(""" class MyClass: def myMethod(self): pass """) srcAfter=trimLines(""" class MyClass: def myMethod(self): self.newMethod() def newMethod(self): pass """) fname = self.packagestructure.create_module("foo",srcBefore) ctx = bike.init() ctx.extractMethod(fname,3,8,3,12,"newMethod") ctx.save() assert_equal(srcAfter,file(fname).read())
def can_perform_the_extract_method_refactoring(self): srcBefore = trimLines(""" class MyClass: def myMethod(self): pass """) srcAfter = trimLines(""" class MyClass: def myMethod(self): self.newMethod() def newMethod(self): pass """) fname = self.packagestructure.create_module("foo", srcBefore) ctx = bike.init() ctx.extractMethod(fname, 3, 8, 3, 12, "newMethod") ctx.save() assert_equal(srcAfter, file(fname).read())
def test_undoesTheTextOfAFileTwice(self): for i in range(3): src = trimLines(""" class foo: def bah(self): pass """) writeTmpTestFile(src) ctx = bike.init() ctx.renameByCoordinates(tmpfile, 2, 8, "c") ctx.save() ctx.undo() ctx.save() newsrc = readFile(tmpfile) self.assertEqual(newsrc, src) raisedexception = 0 try: ctx.undo() except UndoStackEmptyException: pass else: assert 0, "should have raised an exception"
def test_undoesTheTextOfAFileTwice(self): for i in range(3): src = trimLines(""" class foo: def bah(self): pass """) writeTmpTestFile(src) ctx = bike.init() ctx.renameByCoordinates(tmpfile,2,8,"c") ctx.save() ctx.undo() ctx.save() newsrc = readFile(tmpfile) self.assertEqual(newsrc,src) raisedexception=0 try: ctx.undo() except UndoStackEmptyException: pass else: assert 0,"should have raised an exception"
def provides_a_callback_mechanism_for_prompting_the_user_if_it_cant_work_out_whether_to_rename_a_variable( self): def mycallback(filename, line, colstart, colend): return True srcBefore = dedent(""" class TheClass: def theMethod(self): pass dontKnowTheTypeOfThisVariable = someMagicFunction() dontKnowTheTypeOfThisVariable.theMethod() """) srcAfter = dedent(""" class TheClass: def newName(self): pass dontKnowTheTypeOfThisVariable = someMagicFunction() dontKnowTheTypeOfThisVariable.newName() """) fname = self.packagestructure.create_module("foo", srcBefore) ctx = bike.init() ctx.setRenameMethodPromptCallback(mycallback) ctx.rename(fname, 3, 8, "newName") ctx.save() assert_equal(srcAfter, file(fname).read())
def provides_a_callback_mechanism_for_prompting_the_user_if_it_cant_work_out_whether_to_rename_a_variable(self): def mycallback(filename, line, colstart, colend): return True srcBefore=dedent(""" class TheClass: def theMethod(self): pass dontKnowTheTypeOfThisVariable = someMagicFunction() dontKnowTheTypeOfThisVariable.theMethod() """) srcAfter=dedent(""" class TheClass: def newName(self): pass dontKnowTheTypeOfThisVariable = someMagicFunction() dontKnowTheTypeOfThisVariable.newName() """) fname = self.packagestructure.create_module("foo",srcBefore) ctx = bike.init() ctx.setRenameMethodPromptCallback(mycallback) ctx.rename(fname,3,8,"newName") ctx.save() assert_equal(srcAfter,file(fname).read())
def test_extractsPass(self): srcBefore = trimLines(""" class MyClass: def myMethod(self): pass """) srcAfter = trimLines(""" class MyClass: def myMethod(self): self.newMethod() def newMethod(self): pass """) writeTmpTestFile(srcBefore) ctx = bike.init() ctx.extractMethod(os.path.abspath(tmpfile), 3, 8, 3, 12, "newMethod") ctx.save() self.assertEqual(readTmpTestFile(), srcAfter) ctx.undo() ctx.save() self.assertEqual(readTmpTestFile(), srcBefore)
def test_extractsPass(self): srcBefore=trimLines(""" class MyClass: def myMethod(self): pass """) srcAfter=trimLines(""" class MyClass: def myMethod(self): self.newMethod() def newMethod(self): pass """) writeTmpTestFile(srcBefore) ctx = bike.init() ctx.extractMethod(os.path.abspath(tmpfile),3,8,3,12,"newMethod") ctx.save() self.assertEqual(readTmpTestFile(),srcAfter) ctx.undo() ctx.save() self.assertEqual(readTmpTestFile(),srcBefore)
def brm(): global brmctx if not brmctx: brmctx = bike.init().brmctx return brmctx
def kill(self): self.ctx = None self.ctx = bike.init() self.ctx.setProgressLogger(logger)
return record def getLogger(): """ Build file logger """ log = logging.getLogger('BicycleRepairDaemon') log.setLevel(logging.DEBUG) formatter = JsonFormatter('%(asctime)s: %(levelname)-8s: %(message)s') hdlr = logging.StreamHandler(sys.stderr) hdlr.setFormatter(formatter) log.addHandler(hdlr) return log logger = getLogger() bikectx = bike.init() #bikectx.setProgressLogger(sys.stderr) def write(data): """ Write data to STDOUT """ if not isinstance(data, str): data = json.dumps(data) sys.stdout.write(data) if not data.endswith('\n'): sys.stdout.write('\n') try: sys.stdout.flush() except IOError:
def initbrm(self): global brmctx brmctx = bike.init() if self.TRACE == 1: brmctx.setProgressLogger(self.progressLogger)
brmctx.extract(filename,bline,bcol,eline,ecol,name) lisp.set_marker(lisp.mark_marker(),None) _revertSavedFiles(brmctx.save()) def move_class(self,newfilename): lisp.save_some_buffers() filename = lisp.buffer_file_name() line,col = _getCoords() brmctx.moveClassToNewModule(filename,line,newfilename) _revertSavedFiles(brmctx.save()) brmctx = bike.init() brmemacs = None is_xemacs = (lisp.emacs_version().find("GNU") == -1) currently_saving=0 # fix pop_excursion to work with xemacs class Let(Let): def pop_excursion(self): method, (buffer, point_marker, mark_marker) = self.stack[-1] assert method == 'excursion', self.stack[-1] del self.stack[-1] lisp.set_buffer(buffer) lisp.goto_char(point_marker) lisp.set_mark(mark_marker)
#!/usr/bin/env python """ Python refactoring for TextMate via bicycle repair man. Requires Bicycle Repair Man from http://bicyclerepair.sourceforge.net/ """ import sys, os, popen2, bike, logging from urllib import pathname2url brm = bike.init() log_file = os.environ.get('BRM_TM_LOG','biketextmate.log') logger = logging.getLogger() handler = logging.FileHandler(log_file) formatter = logging.Formatter('[%(asctime)s] %(funcName)s :: %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging.DEBUG) #################################### ### Bicycle Repair Man functions ### #################################### def findDefinition(): """ Find the definition of the item under the cursor. """
eline, ecol = _getPointCoords() lisp.exchange_point_and_mark() brmctx.extract(filename, bline, bcol, eline, ecol, name) lisp.set_marker(lisp.mark_marker(), None) _revertSavedFiles(brmctx.save()) def move_class(self, newfilename): lisp.save_some_buffers() filename = lisp.buffer_file_name() line, col = _getCoords() brmctx.moveClassToNewModule(filename, line, newfilename) _revertSavedFiles(brmctx.save()) brmctx = bike.init() brmemacs = None is_xemacs = (lisp.emacs_version().find("GNU") == -1) currently_saving = 0 # fix pop_excursion to work with xemacs class Let(Let): def pop_excursion(self): method, (buffer, point_marker, mark_marker) = self.stack[-1] assert method == 'excursion', self.stack[-1] del self.stack[-1] lisp.set_buffer(buffer) lisp.goto_char(point_marker)