def __init__(self): self.ast = getRoot() # Used because some refactorings delegate back to the user. # this flag ensures that code isnt imported during those times self.readyToLoadNewCode = 1 self.paths = [] getUndoStack(1) # force new undo stack if not getRoot().unittestmode: log.warning = sys.stderr self.promptUserClientCallback = None
def test_getsFilenamesInSubPackagesIfCtxFilenameIsInTheRoot(self): try: oldpath = getRoot().pythonpath getRoot().pythonpath = [] # clear the python path createPackageStructure("","") fnames = [f for f in generateModuleFilenamesInPythonPath(pkgstructureFile0)] self.assert_(pkgstructureFile1 in fnames) self.assert_(pkgstructureFile2 in fnames) finally: getRoot().pythonpath = oldpath removePackageStructure()
def test_getsAllFilenamesInSameHierarchyAsContextFile(self): try: oldpath = getRoot().pythonpath getRoot().pythonpath = [] # clear the python path createPackageStructure("","") fnames = [f for f in generateModuleFilenamesInPythonPath(pkgstructureFile1)] self.assert_(pkgstructureFile0 in fnames) self.assert_(pkgstructureFile1 in fnames) self.assert_(pkgstructureFile2 in fnames) finally: getRoot().pythonpath = oldpath removePackageStructure()
def test_returnsOtherFilesInSameNonPackageDirectory(self): try: oldpath = getRoot().pythonpath getRoot().pythonpath = [] # clear the python path writeTmpTestFile("") newtmpfile = os.path.join(tmproot, "baz.py") writeFile(newtmpfile, "") fnames = [f for f in \ generateModuleFilenamesInPythonPath(tmpfile)] assert newtmpfile in fnames finally: os.remove(newtmpfile) deleteTmpTestFile() getRoot().pythonpath = oldpath
def test_returnsOtherFilesInSameNonPackageDirectory(self): try: oldpath = getRoot().pythonpath getRoot().pythonpath = [] # clear the python path writeTmpTestFile("") newtmpfile = os.path.join(tmproot,"baz.py") writeFile(newtmpfile, "") fnames = [f for f in \ generateModuleFilenamesInPythonPath(tmpfile)] assert newtmpfile in fnames finally: os.remove(newtmpfile) deleteTmpTestFile() getRoot().pythonpath = oldpath
def test_getsFilenamesInSubPackagesIfCtxFilenameIsInTheRoot(self): try: oldpath = getRoot().pythonpath getRoot().pythonpath = [] # clear the python path createPackageStructure("", "") fnames = [ f for f in generateModuleFilenamesInPythonPath(pkgstructureFile0) ] self.assert_(pkgstructureFile1 in fnames) self.assert_(pkgstructureFile2 in fnames) finally: getRoot().pythonpath = oldpath removePackageStructure()
def test_getsAllFilenamesInSameHierarchyAsContextFile(self): try: oldpath = getRoot().pythonpath getRoot().pythonpath = [] # clear the python path createPackageStructure("", "") fnames = [ f for f in generateModuleFilenamesInPythonPath(pkgstructureFile1) ] self.assert_(pkgstructureFile0 in fnames) self.assert_(pkgstructureFile1 in fnames) self.assert_(pkgstructureFile2 in fnames) finally: getRoot().pythonpath = oldpath removePackageStructure()
def handlePackageScope(package, fqn): #print "handlePackageScope",package,fqn child = package.getChild(fqn) if child: return child if isinstance(package,Root): return getModuleOrPackageUsingFQN(fqn) # try searching the fs node = getModuleOrPackageUsingFQN(fqn,package.path) if node: return node # try the package init module initmod = package.getChild("__init__") if initmod is not None: type = getImportedType(initmod, fqn) if type: return type # maybe fqn is absolute return getTypeOf(getRoot(), fqn)
def test_doesntTraverseOtherPackagesOffOfTheRoot(self): try: oldpath = getRoot().pythonpath getRoot().pythonpath = [] # clear the python path createPackageStructure("","") os.makedirs(os.path.join(pkgstructureRootDir, "c")) writeFile(os.path.join(pkgstructureRootDir, "c", "__init__.py"), "# ") bazfile = os.path.join(pkgstructureRootDir, "c", "baz.py") writeFile(bazfile, "pass") fnames = [f for f in generateModuleFilenamesInPythonPath(pkgstructureFile1)] self.assert_(pkgstructureFile0 in fnames) self.assert_(pkgstructureFile1 in fnames) self.assert_(pkgstructureFile2 in fnames) self.assert_(bazfile not in fnames) finally: getRoot().pythonpath = oldpath os.remove(os.path.join(pkgstructureRootDir, "c", "baz.py")) os.remove(os.path.join(pkgstructureRootDir, "c", "__init__.py")) os.removedirs(os.path.join(pkgstructureRootDir, "c")) removePackageStructure()
def setUp(self): log.warning = log.SilentLogger() try: os.makedirs(tmproot) except: pass os.chdir(tmproot) resetRoot(Root([tmproot])) getRoot().unittestmode = True global filesToDelete global dirsToDelete filesToDelete = [] dirsToDelete = [] from bike.parsing.load import Cache Cache.instance.reset()
def test_findsDefnInSamePackageHierarchyAndRootNotInPath(self): src=trimLines(""" from a.b.bah import TheClass """) classsrc=trimLines(""" class TheClass: def theMethod(self): pass """) getRoot().pythonpath = [] # clear the python path defn = self.helper(src, classsrc, 1, 20) assert defn[0].filename == pkgstructureFile2 assert defn[0].lineno == 1 assert defn[0].colno == 6 assert defn[0].confidence == 100
def test_findsDefnInSamePackageHierarchyAndRootNotInPath(self): src = trimLines(""" from a.b.bah import TheClass """) classsrc = trimLines(""" class TheClass: def theMethod(self): pass """) getRoot().pythonpath = [] # clear the python path defn = self.helper(src, classsrc, 1, 20) assert defn[0].filename == pkgstructureFile2 assert defn[0].lineno == 1 assert defn[0].colno == 6 assert defn[0].confidence == 100
def test_doesntTraverseOtherPackagesOffOfTheRoot(self): try: oldpath = getRoot().pythonpath getRoot().pythonpath = [] # clear the python path createPackageStructure("", "") os.makedirs(os.path.join(pkgstructureRootDir, "c")) writeFile(os.path.join(pkgstructureRootDir, "c", "__init__.py"), "# ") bazfile = os.path.join(pkgstructureRootDir, "c", "baz.py") writeFile(bazfile, "pass") fnames = [ f for f in generateModuleFilenamesInPythonPath(pkgstructureFile1) ] self.assert_(pkgstructureFile0 in fnames) self.assert_(pkgstructureFile1 in fnames) self.assert_(pkgstructureFile2 in fnames) self.assert_(bazfile not in fnames) finally: getRoot().pythonpath = oldpath os.remove(os.path.join(pkgstructureRootDir, "c", "baz.py")) os.remove(os.path.join(pkgstructureRootDir, "c", "__init__.py")) os.removedirs(os.path.join(pkgstructureRootDir, "c")) removePackageStructure()
def createSourceNodeAt(src, fqn): modname = fqn_rcar(fqn) packagefqn = fqn_rcdr(fqn) dirpath = os.path.join(*packagefqn.split(".")) filepath = os.path.join(dirpath,modname+".py") try: os.makedirs(dirpath) except: pass dirsToDelete.append(dirpath) # add the __init__.py files path = "." for pathelem in packagefqn.split("."): path = os.path.join(path,pathelem) initfile = os.path.join(path,"__init__.py") writeFile(initfile,"#") filesToDelete.append(initfile) writeFile(filepath,src) filesToDelete.append(filepath) return getRoot()
def test_findsDefnInSameNonPackageDirectory(self): try: getRoot().pythonpath = [] # clear the python path classsrc = trimLines(""" def testFunction(): print 'hello' """) src = trimLines(""" from baz import testFunction """) writeTmpTestFile(src) newtmpfile = os.path.join(tmproot,"baz.py") writeFile(newtmpfile, classsrc) refs = [x for x in findAllPossibleDefinitionsByCoords(tmpfile,1,16)] assert refs[0].filename == newtmpfile assert refs[0].lineno == 1 finally: os.remove(newtmpfile) deleteTmpTestFile()
def findAllPossibleDefinitionsByCoords(filepath,lineno,col): #try: node = translateSourceCoordsIntoASTNode(filepath,lineno,col) #except: # import traceback # traceback.print_exc() if node is None: raise "selected node type not supported" scope = getScopeForLine(getSourceNode(filepath),lineno) match = findDefinitionFromASTNode(scope,node) if match is not None: yield match if isinstance(node,Getattr) and (match is None or match.confidence != 100): root = getRoot() name = node.attrname for match in scanPythonPathForMatchingMethodNames(name,filepath): yield match log.progress.write("done\n")
def createSourceNodeAt(src, fqn): modname = fqn_rcar(fqn) packagefqn = fqn_rcdr(fqn) dirpath = os.path.join(*packagefqn.split(".")) filepath = os.path.join(dirpath, modname + ".py") try: os.makedirs(dirpath) except: pass dirsToDelete.append(dirpath) # add the __init__.py files path = "." for pathelem in packagefqn.split("."): path = os.path.join(path, pathelem) initfile = os.path.join(path, "__init__.py") writeFile(initfile, "#") filesToDelete.append(initfile) writeFile(filepath, src) filesToDelete.append(filepath) return getRoot()
def test_findsDefnInSameNonPackageDirectory(self): try: getRoot().pythonpath = [] # clear the python path classsrc = trimLines(""" def testFunction(): print 'hello' """) src = trimLines(""" from baz import testFunction """) writeTmpTestFile(src) newtmpfile = os.path.join(tmproot, "baz.py") writeFile(newtmpfile, classsrc) refs = [ x for x in findAllPossibleDefinitionsByCoords(tmpfile, 1, 16) ] assert refs[0].filename == newtmpfile assert refs[0].lineno == 1 finally: os.remove(newtmpfile) deleteTmpTestFile()
def handlePackageScope(package, fqn): #print "handlePackageScope",package,fqn child = package.getChild(fqn) if child: return child if isinstance(package, Root): return getModuleOrPackageUsingFQN(fqn) # try searching the fs node = getModuleOrPackageUsingFQN(fqn, package.path) if node: return node # try the package init module initmod = package.getChild("__init__") if initmod is not None: type = getImportedType(initmod, fqn) if type: return type # maybe fqn is absolute return getTypeOf(getRoot(), fqn)
def _setNonLibPythonPath(self, filename): if getRoot().unittestmode: return pythonpath = self._removeLibdirsFromPath(sys.path) pythonpath = [os.path.abspath(p) for p in pythonpath] self.ast.pythonpath = pythonpath
def getPythonPath(): return getRoot().pythonpath
def _setNonLibPythonPath(self,filename): if getRoot().unittestmode: return pythonpath = self._removeLibdirsFromPath(sys.path) pythonpath = [os.path.abspath(p) for p in pythonpath] self.ast.pythonpath = pythonpath