예제 #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
예제 #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
예제 #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()
예제 #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()
예제 #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
예제 #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
예제 #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()
예제 #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()
예제 #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)
예제 #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()
예제 #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
예제 #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()
예제 #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()
예제 #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()
예제 #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")
예제 #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()
예제 #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)
예제 #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
예제 #23
0
def getPythonPath():
    return getRoot().pythonpath
예제 #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
예제 #25
0
def getPythonPath():
    return getRoot().pythonpath