def test_findsReferenceInModuleWhichImportsClassWithFrom(self): src = trimLines(""" from b.bah import TheClass def foo(): a = TheClass() a.theMethod() """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(ClassTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename, 1, 6)] self.assertEqual(refs[0].filename, os.path.abspath(os.path.join("a", "foo.py"))) self.assertEqual(refs[0].lineno, 1) self.assertEqual(refs[0].colno, 18) self.assertEqual(refs[0].confidence, 100) self.assertEqual(refs[1].filename, os.path.abspath(os.path.join("a/foo.py"))) self.assertEqual(refs[1].lineno, 3) self.assertEqual(refs[1].colno, 8) self.assertEqual(refs[1].confidence, 100)
def test_renamesMethodReferenceOfInstanceCreatedInSubsequentFunction(self): src = trimLines(""" class TheClass: def theMethod(): pass class NotTheClass: def theMethod(): pass def foo(): a = bah() a.theMethod() def bah(): return TheClass() """) root = createSourceNodeAt(src, "a.foo") filename = os.path.abspath("a/foo.py") refs = [ x for x in findReferences(filename, 2, 8, []) if x.confidence == 100 ] self.assertEqual(refs[0].filename, os.path.abspath(os.path.join("a", "foo.py"))) self.assertEqual(refs[0].lineno, 10) self.assertEqual(refs[0].colno, 6)
def moveFunctionToNewModule(origfile, line, newfile): srcnode = getSourceNode(origfile) targetsrcnode = getSourceNode(newfile) scope = getScopeForLine(srcnode, line) linesep = getLineSeperator(srcnode.getLines()[0]) matches = findReferences(origfile, line, scope.getColumnOfName()) origFileImport = [] fromline = 'from %s import %s' % (filenameToModulePath(newfile), scope.name) for match in matches: if match.filename == origfile: origFileImport = fromline + linesep else: s = getSourceNode(match.filename) m = s.fastparseroot if match.lineno in m.getImportLineNumbers(): getUndoStack().addSource(s.filename, s.getSource()) maskedline = m.getLogicalLine(match.lineno) origline = s.getLines()[match.lineno - 1] reMatch = re.match(exactFromRE % (scope.name), maskedline) if reMatch and not (',' in reMatch.group(2) or \ '\\' in reMatch.group(2)): restOfOrigLine = origline[len(reMatch.group(1)):] s.getLines()[match.lineno - 1] = fromline + restOfOrigLine elif re.match(fromRE, maskedline): #remove the element from the import stmt line = re.sub('%s\s*?,' % (scope.name), '', origline) s.getLines()[match.lineno - 1] = line #and add a new line nextline = match.lineno + maskedline.count('\\') + 1 s.getLines()[nextline - 1:nextline - 1] = [fromline + linesep] queueFileToSave(s.filename, s.getSource()) refs = getVariableReferencesInLines(scope.getMaskedLines()) scopeLines = srcnode.getLines()[scope.getStartLine() - 1:scope.getEndLine() - 1] importModules = deduceImportsForNewFile(refs, scope) importlines = composeNewFileImportLines(importModules, linesep) getUndoStack().addSource(srcnode.filename, srcnode.getSource()) getUndoStack().addSource(targetsrcnode.filename, targetsrcnode.getSource()) srcnode.getLines()[scope.getStartLine() - 1:scope.getEndLine() - 1] = origFileImport targetsrcnode.getLines().extend(importlines + scopeLines) queueFileToSave(srcnode.filename, srcnode.getSource()) queueFileToSave(targetsrcnode.filename, targetsrcnode.getSource())
def helper4(self, src, importedsrc, line, col): try: createPackageStructure(src,importedsrc) filename = pkgstructureFile1 refs = [x for x in findReferences(filename,line,col) if x.confidence == 100] finally: removePackageStructure() return refs
def helper4(self, src, importedsrc, line, col): try: createPackageStructure(src,importedsrc) filename = pkgstructureFile1 refs = [x for x in findReferences(filename,line,col) if x.confidence == 100] finally: removePackageStructure() return refs
def test_returnsEmptyListIfNoReferences(self): src = trimLines(""" class MyClass: pass a = TheClass() """) root = createSourceNodeAt(src,"mymodule") refs = [x for x in findReferences(os.path.abspath("mymodule.py"),1,6)] self.assertEqual(refs,[])
def inlineLocalVariable_old(sourcenode, lineno, col): definition, region, regionlinecount = getLocalVariableInfo( sourcenode, lineno, col) addUndo(sourcenode) replaceReferences( sourcenode, findReferences(sourcenode.filename, definition.lineno, definition.colno), region) delLines(sourcenode, definition.lineno - 1, regionlinecount) updateSource(sourcenode)
def test_doesntfindReferenceInModuleWhichDoesntImportClass(self): src = trimLines(""" a = TheClass() """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(ClassTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename, 1, 6)] assert refs == []
def test_doesntBarfOnFromImportStarWhenNameIsInFromClause(self): src = trimLines(""" from a.b.bah import TheClass a = TheClass() """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(ClassTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename,1,6)]
def test_doesntfindReferenceInModuleWhichDoesntImportClass(self): src = trimLines(""" a = TheClass() """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(ClassTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename,1,6)] assert refs == []
def test_doesntBarfOnFromImportStarWhenNameIsInFromClause(self): src = trimLines(""" from a.b.bah import TheClass a = TheClass() """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(ClassTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename, 1, 6)]
def test_returnsEmptyListIfNoReferences(self): src = trimLines(""" class MyClass: pass a = TheClass() """) root = createSourceNodeAt(src, "mymodule") refs = [ x for x in findReferences(os.path.abspath("mymodule.py"), 1, 6) ] self.assertEqual(refs, [])
def test_findsSimpleReferenceInSameModule(self): src = trimLines(""" class TheClass: pass a = TheClass() """) root = createSourceNodeAt(src,"mymodule") refs = [x for x in findReferences(os.path.abspath("mymodule.py"),1,6)] self.assertEqual(refs[0].filename,os.path.abspath("mymodule.py")) self.assertEqual(refs[0].lineno,3) self.assertEqual(refs[0].colno,4) self.assertEqual(refs[0].confidence,100)
def test_findsReferenceToClassImportedInSameClassScope(self): src=trimLines(""" class AnotherClass: from b.bah import TheClass TheClass.baz = 0 """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(ClassTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename,1,6)] assert refs != []
def test_findsReferenceToClassImportedInSameClassScope(self): src = trimLines(""" class AnotherClass: from b.bah import TheClass TheClass.baz = 0 """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(ClassTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename, 1, 6)] assert refs != []
def test_findsClassReferenceInInstanceCreation(self): src = trimLines(""" class TheClass: def theMethod(self): pass TheClass().theMethod() """) root = createSourceNodeAt(src, "a.foo") filename = os.path.abspath("a/foo.py") refs = [x for x in findReferences(filename,1,6)] self.assertEqual(refs[0].filename,os.path.abspath(os.path.join("a","foo.py"))) self.assertEqual(refs[0].lineno,3) self.assertEqual(refs[0].colno,0) self.assertEqual(refs[0].confidence,100)
def test_findsReferenceInMultiLineImportStatement(self): src =trimLines(""" from b.bah import foo, \\ TheFooBah, TheClass, Foobah, SomethingElse """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(ClassTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename,1,6)] self.assertEqual(refs[0].filename,os.path.abspath(os.path.join("a","foo.py"))) self.assertEqual(refs[0].lineno,2) self.assertEqual(refs[0].colno,21) self.assertEqual(refs[0].confidence,100)
def test_doesntGetReferenceToMethodWhenObjectCreatedInChildScopeToMethodReference(self): src = trimLines(""" from b.bah import TheClass a = AnotherClass() def foo(): a = TheClass() a.theMethod() """) root = createSourceNodeAt(src,"a.foo") root = createSourceNodeAt(MethodTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename,2,8) if x.confidence == 100] assert len(refs) == 0
def test_findsSimpleReferenceInSameModule(self): src = trimLines(""" class TheClass: pass a = TheClass() """) root = createSourceNodeAt(src, "mymodule") refs = [ x for x in findReferences(os.path.abspath("mymodule.py"), 1, 6) ] self.assertEqual(refs[0].filename, os.path.abspath("mymodule.py")) self.assertEqual(refs[0].lineno, 3) self.assertEqual(refs[0].colno, 4) self.assertEqual(refs[0].confidence, 100)
def test_findsReferenceInModuleWhichImportsClassWithFromImportStar2(self): src = trimLines(""" from a.b.bah import * a = TheClass() """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(ClassTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename,1,6)] self.assertEqual(refs[0].filename,os.path.abspath(os.path.join("a","foo.py"))) self.assertEqual(refs[0].lineno,2) self.assertEqual(refs[0].colno,4) self.assertEqual(refs[0].confidence,100)
def test_findsClassReferenceInInstanceCreation(self): src = trimLines(""" class TheClass: def theMethod(self): pass TheClass().theMethod() """) root = createSourceNodeAt(src, "a.foo") filename = os.path.abspath("a/foo.py") refs = [x for x in findReferences(filename, 1, 6)] self.assertEqual(refs[0].filename, os.path.abspath(os.path.join("a", "foo.py"))) self.assertEqual(refs[0].lineno, 3) self.assertEqual(refs[0].colno, 0) self.assertEqual(refs[0].confidence, 100)
def test_doesntGetReferenceToMethodWhenObjectCreatedInChildScopeToMethodReference(self): src = trimLines(""" from b.bah import TheClass a = AnotherClass() def foo(): a = TheClass() a.theMethod() """) root = createSourceNodeAt(src,"a.foo") root = createSourceNodeAt(MethodTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename,2,8) if x.confidence == 100] assert len(refs) == 0
def test_findsReferenceInMultiLineImportStatement(self): src = trimLines(""" from b.bah import foo, \\ TheFooBah, TheClass, Foobah, SomethingElse """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(ClassTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename, 1, 6)] self.assertEqual(refs[0].filename, os.path.abspath(os.path.join("a", "foo.py"))) self.assertEqual(refs[0].lineno, 2) self.assertEqual(refs[0].colno, 21) self.assertEqual(refs[0].confidence, 100)
def test_findsReferenceInClassBases(self): src =trimLines(""" from b.bah import TheClass class DerivedClass(TheClass): pass """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(ClassTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename,1,6)] self.assertEqual(refs[1].filename,os.path.abspath(os.path.join("a","foo.py"))) self.assertEqual(refs[1].lineno,2) self.assertEqual(refs[1].colno,19) self.assertEqual(refs[1].confidence,100)
def test_findsReferencesInModuleWhichImportsClass(self): src = trimLines(""" import b.bah def foo(): a = b.bah.TheClass() a.theMethod() """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(ClassTestdata, "a.b.bah") refs = [x for x in findReferences(os.path.abspath("a/b/bah.py"),1,6)] self.assertEqual(refs[0].filename,os.path.abspath(os.path.join("a","foo.py"))) self.assertEqual(refs[0].lineno,3) self.assertEqual(refs[0].colno,14) self.assertEqual(refs[0].confidence,100)
def test_getsReferenceOfSimpleMethodCall(self): src = trimLines(""" from b.bah import TheClass a = TheClass() a.theMethod() """) root = createSourceNodeAt(src,"a.foo") root = createSourceNodeAt(MethodTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename,2,8) if x.confidence == 100] self.assertEqual(refs[0].filename, os.path.abspath(os.path.join("a","foo.py"))) self.assertEqual(refs[0].lineno,3) self.assertEqual(refs[0].colno,2) self.assertEqual(refs[0].colno,2)
def test_findsReferenceInClassBases(self): src = trimLines(""" from b.bah import TheClass class DerivedClass(TheClass): pass """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(ClassTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename, 1, 6)] self.assertEqual(refs[1].filename, os.path.abspath(os.path.join("a", "foo.py"))) self.assertEqual(refs[1].lineno, 2) self.assertEqual(refs[1].colno, 19) self.assertEqual(refs[1].confidence, 100)
def test_getsReferenceInMiddleOfBiggerCompoundCall(self): src = trimLines(""" class TheClass: def theMethod(self): return AnotherClass() TheClass().theMethod().anotherMethod() """) root = createSourceNodeAt(src,"a.foo") filename = os.path.abspath("a/foo.py") refs = [x for x in findReferences(filename,2,8) if x.confidence == 100] self.assertEqual(refs[0].filename, os.path.abspath(os.path.join("a","foo.py"))) self.assertEqual(refs[0].lineno,3) self.assertEqual(refs[0].colno,11) self.assertEqual(refs[0].colend,20)
def test_getsReferenceInMiddleOfBiggerCompoundCall(self): src = trimLines(""" class TheClass: def theMethod(self): return AnotherClass() TheClass().theMethod().anotherMethod() """) root = createSourceNodeAt(src,"a.foo") filename = os.path.abspath("a/foo.py") refs = [x for x in findReferences(filename,2,8) if x.confidence == 100] self.assertEqual(refs[0].filename, os.path.abspath(os.path.join("a","foo.py"))) self.assertEqual(refs[0].lineno,3) self.assertEqual(refs[0].colno,11) self.assertEqual(refs[0].colend,20)
def test_getsReferenceOfSimpleMethodCall(self): src = trimLines(""" from b.bah import TheClass a = TheClass() a.theMethod() """) root = createSourceNodeAt(src,"a.foo") root = createSourceNodeAt(MethodTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename,2,8) if x.confidence == 100] self.assertEqual(refs[0].filename, os.path.abspath(os.path.join("a","foo.py"))) self.assertEqual(refs[0].lineno,3) self.assertEqual(refs[0].colno,2) self.assertEqual(refs[0].colno,2)
def test_getsReferenceOfMethodCallInSameClass(self): src = trimLines(""" class TheClass: def theMethod(self): pass def anotherMethod(self): self.theMethod() """) root = createSourceNodeAt(src,"a.foo") filename = os.path.abspath("a/foo.py") refs = [x for x in findReferences(filename,2,8) if x.confidence == 100] self.assertEqual(refs[0].filename, os.path.abspath(os.path.join("a","foo.py"))) self.assertEqual(refs[0].lineno,5) self.assertEqual(refs[0].colno,13)
def test_findsReferenceInModuleWhichImportsClassWithFromAndAlias(self): src = trimLines(""" from b.bah import TheClass as MyTheClass def foo(): a = MyTheClass() a.theMethod() """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(ClassTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename,1,6)] self.assertEqual(refs[0].filename,os.path.abspath(os.path.join("a","foo.py"))) self.assertEqual(refs[0].lineno,1) self.assertEqual(refs[0].colno,18) self.assertEqual(refs[0].confidence,100)
def test_getsReferenceOfMethodCallInSameClass(self): src = trimLines(""" class TheClass: def theMethod(self): pass def anotherMethod(self): self.theMethod() """) root = createSourceNodeAt(src,"a.foo") filename = os.path.abspath("a/foo.py") refs = [x for x in findReferences(filename,2,8) if x.confidence == 100] self.assertEqual(refs[0].filename, os.path.abspath(os.path.join("a","foo.py"))) self.assertEqual(refs[0].lineno,5) self.assertEqual(refs[0].colno,13)
def test_findsReferenceWhenModulenameSameAsClassMethodName(self): # asserts that brm doesnt search class scope after not finding name # in method scope (since class scope is invisible unless called on 'self' src =trimLines(""" from a.b import bah class baz: def bah(self): print bah.TheClass """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(ClassTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename,1,6)] self.assertEqual(refs[0].filename,os.path.abspath(os.path.join("a","foo.py"))) self.assertEqual(refs[0].lineno,4) self.assertEqual(refs[0].colno,18) self.assertEqual(refs[0].confidence,100)
def test_findsClassReferenceInInstanceCreationWithFQN(self): src = trimLines(""" import b.bah def foo(): a = b.bah.TheClass() a.theMethod() """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(ClassTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename, 1, 6)] self.assertEqual(refs[0].filename, os.path.abspath(os.path.join("a", "foo.py"))) self.assertEqual(refs[0].lineno, 3) self.assertEqual(refs[0].colno, 14) self.assertEqual(refs[0].confidence, 100)
def test_findsReferenceWhenModulenameSameAsClassMethodName(self): # asserts that brm doesnt search class scope after not finding name # in method scope (since class scope is invisible unless called on 'self' src = trimLines(""" from a.b import bah class baz: def bah(self): print bah.TheClass """) root = createSourceNodeAt(src, "a.foo") root = createSourceNodeAt(ClassTestdata, "a.b.bah") filename = os.path.abspath("a/b/bah.py") refs = [x for x in findReferences(filename, 1, 6)] self.assertEqual(refs[0].filename, os.path.abspath(os.path.join("a", "foo.py"))) self.assertEqual(refs[0].lineno, 4) self.assertEqual(refs[0].colno, 18) self.assertEqual(refs[0].confidence, 100)
def test_renamesMethodReferenceOfInstanceCreatedInSubsequentFunction(self): src = trimLines(""" class TheClass: def theMethod(): pass class NotTheClass: def theMethod(): pass def foo(): a = bah() a.theMethod() def bah(): return TheClass() """) root = createSourceNodeAt(src,"a.foo") filename = os.path.abspath("a/foo.py") refs = [x for x in findReferences(filename,2,8,[]) if x.confidence == 100] self.assertEqual(refs[0].filename, os.path.abspath(os.path.join("a","foo.py"))) self.assertEqual(refs[0].lineno,10) self.assertEqual(refs[0].colno,6)
def findReferencesByCoordinates(self, filename_path, line, column): filename_path = self.normalizeFilename(filename_path) self._setNonLibPythonPath(filename_path) return findReferences(filename_path,line,column)
def inlineLocalVariable_old(sourcenode,lineno,col): definition, region, regionlinecount = getLocalVariableInfo(sourcenode, lineno, col) addUndo(sourcenode) replaceReferences(sourcenode, findReferences(sourcenode.filename, definition.lineno, definition.colno), region) delLines(sourcenode, definition.lineno-1, regionlinecount) updateSource(sourcenode)
def findReferencesByCoordinates(self, filename_path, line, column): filename_path = self.normalizeFilename(filename_path) path = self._removeLibdirsFromPath(sys.path) return findReferences(filename_path, line, column, 0, path)
def moveFunctionToNewModule(origfile,line,newfile): srcnode = getSourceNode(origfile) targetsrcnode = getSourceNode(newfile) scope = getScopeForLine(srcnode,line) linesep = getLineSeperator(srcnode.getLines()[0]) matches =[m for m in findReferences(origfile, line, scope.getColumnOfName())] origFileImport = [] fromline = 'from %s import %s'%(filenameToModulePath(newfile),scope.name) for match in matches: if match.filename == origfile: origFileImport = fromline + linesep else: s = getSourceNode(match.filename) m = s.fastparseroot if match.lineno in m.getImportLineNumbers(): getUndoStack().addSource(s.filename, s.getSource()) maskedline = m.getLogicalLine(match.lineno) origline = s.getLines()[match.lineno-1] reMatch = re.match(exactFromRE%(scope.name),maskedline) if reMatch and not (',' in reMatch.group(2) or \ '\\' in reMatch.group(2)): # i.e. line is 'from module import foo' if match.filename == newfile: #remove the import s.getLines()[match.lineno-1:match.lineno] = [] pass else: restOfOrigLine = origline[len(reMatch.group(1)):] s.getLines()[match.lineno-1] = fromline + restOfOrigLine elif re.match(fromRE,maskedline): # i.e. line is 'from module import foo,bah,baz' #remove the element from the import stmt line = removeNameFromMultipleImportLine(scope.name, origline) s.getLines()[match.lineno-1] = line #and add a new line nextline = match.lineno + maskedline.count('\\') + 1 s.getLines()[nextline-1:nextline-1] = [fromline+linesep] queueFileToSave(s.filename,s.getSource()) refs = getVariableReferencesInLines(scope.getMaskedLines()) scopeLines = srcnode.getLines()[scope.getStartLine()-1: scope.getEndLine()-1] importModules = deduceImportsForNewFile(refs, scope) importlines = composeNewFileImportLines(importModules, linesep) getUndoStack().addSource(srcnode.filename, srcnode.getSource()) getUndoStack().addSource(targetsrcnode.filename, targetsrcnode.getSource()) srcnode.getLines()[scope.getStartLine()-1: scope.getEndLine()-1] = origFileImport targetsrcnode.getLines().extend(importlines+scopeLines) queueFileToSave(srcnode.filename,srcnode.getSource()) queueFileToSave(targetsrcnode.filename,targetsrcnode.getSource())
def findReferencesByCoordinates(self, filename_path, line, column): filename_path = self.normalizeFilename(filename_path) self._setNonLibPythonPath(filename_path) return findReferences(filename_path, line, column)
def findReferencesByCoordinates(self, filename_path, line, column): filename_path = self.normalizeFilename(filename_path) path = self._removeLibdirsFromPath(sys.path) return findReferences(filename_path,line,column,0,path)