Esempio n. 1
0
    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
Esempio n. 2
0
    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
Esempio n. 3
0
 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()
Esempio n. 4
0
 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()
Esempio n. 5
0
 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
Esempio n. 6
0
 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
Esempio n. 7
0
 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()
Esempio n. 8
0
 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()
Esempio n. 9
0
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)
Esempio n. 10
0
 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()
Esempio n. 11
0
 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
Esempio n. 14
0
 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()
Esempio n. 15
0
    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()
Esempio n. 16
0
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()
Esempio n. 18
0
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")
Esempio n. 19
0
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()
Esempio n. 21
0
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)
Esempio n. 22
0
 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
Esempio n. 23
0
def getPythonPath():
    return getRoot().pythonpath
Esempio n. 24
0
 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
Esempio n. 25
0
def getPythonPath():
    return getRoot().pythonpath