Esempio n. 1
0
 def test_worksForPackageInitFile(self):
     try:
         createPackageStructure("pass","pass")
         self.assertEqual(getModuleUsingFQN("a.b",[pkgstructureRootDir]).filename,
                          os.path.join(pkgstructureChilddir,"__init__.py"))
     finally:
         removePackageStructure()
Esempio n. 2
0
 def test_worksForFullPath(self):
     try:
         createPackageStructure("pass","pass")
         self.assertEqual(pkgstructureFile2,
                          getModuleUsingFQN("a.b.bah",[pkgstructureRootDir]).filename)
     finally:
         removePackageStructure()
Esempio n. 3
0
def handleModuleClassOrFunctionScope(scope,fqn,pythonpath):
    assert isinstance(scope,Function) or isinstance(scope,Class) or isinstance(scope,Module), "this function cant take a scope of type"+str(scope.__class__)
    
    if fqn == "self" and scopeIsAMethod(scope):
        return Instance(scope.getParent())

    child = scope.getChild(fqn)
    if child: return child
    
    type = scanScopeSourceForType(scope, fqn)
    if type is not None: return type
    
    type = getImportedType(scope, fqn, pythonpath)   # try imported types
    if type is not None: return type

    if isinstance(scope,Module) and scope.getSourceNode().filename.endswith("__init__.py"):
        # try searching the package for a module
        packagedir = os.path.dirname(scope.getSourceNode().filename)
        node = getModuleUsingFQN(fqn,[packagedir]+pythonpath)
        if node is not None: return node
    
    try:
        parentScope = scope.getParent()
    except ElementHasNoParentException:
        return None  # scope is a module
    
    while isinstance(parentScope,Class):
        # don't search class scope, since this is not accessible except
        # through self   (is this true?)
        parentScope = parentScope.getParent()


    return getTypeOf(parentScope, fqn, pythonpath)
Esempio n. 4
0
 def test_worksForFullPath(self):
     try:
         createPackageStructure("pass", "pass")
         self.assertEqual(
             pkgstructureFile2,
             getModuleUsingFQN("a.b.bah", [pkgstructureRootDir]).filename)
     finally:
         removePackageStructure()
Esempio n. 5
0
 def test_worksForPackageInitFile(self):
     try:
         createPackageStructure("pass", "pass")
         self.assertEqual(
             getModuleUsingFQN("a.b", [pkgstructureRootDir]).filename,
             os.path.join(pkgstructureChilddir, "__init__.py"))
     finally:
         removePackageStructure()
Esempio n. 6
0
 def test_doesntGotIntoRecursiveLoopWhenEvaluatingARecursiveFunction(self):
     src = trimLines("""
     def fn(v):
         if v < 45:
             return fn(root+1)
     val = fn(3)
     """)
     root = createSourceNodeAt(src,"a.foo")
     mod = getModuleUsingFQN("a.foo",[pkgstructureRootDir])
     getTypeOf(mod,"val",[pkgstructureRootDir])   # stack overflow!
Esempio n. 7
0
 def test_handlesRecursionProblem(self):
     src = trimLines("""
     def fn(root):
         node = root
         node = node.getPackage('something')
     """)
     root = createSourceNodeAt(src,"a.foo")
     m = getModuleUsingFQN("a.foo",[pkgstructureRootDir])
     fn = getTypeOf(m,"fn",[pkgstructureRootDir])
     getTypeOf(fn,"node",[pkgstructureRootDir])   # stack overflow!
Esempio n. 8
0
 def test_doesntGotIntoRecursiveLoopWhenEvaluatingARecursiveFunction(self):
     src = trimLines("""
     def fn(v):
         if v < 45:
             return fn(root+1)
     val = fn(3)
     """)
     root = createSourceNodeAt(src, "a.foo")
     mod = getModuleUsingFQN("a.foo", [pkgstructureRootDir])
     getTypeOf(mod, "val", [pkgstructureRootDir])  # stack overflow!
Esempio n. 9
0
 def test_handlesRecursionProblem(self):
     src = trimLines("""
     def fn(root):
         node = root
         node = node.getPackage('something')
     """)
     root = createSourceNodeAt(src, "a.foo")
     m = getModuleUsingFQN("a.foo", [pkgstructureRootDir])
     fn = getTypeOf(m, "fn", [pkgstructureRootDir])
     getTypeOf(fn, "node", [pkgstructureRootDir])  # stack overflow!
Esempio n. 10
0
def resolveImportedModuleOrPackage(scope,fqn,pythonpath):
    # try searching from directory containing scope module
    path = os.path.dirname(scope.module.filename)
    node = getModuleUsingFQN(fqn,[path]+pythonpath)
    if node is not None:
        return node

    # try searching in same package hierarchy
    basedir = getPackageBaseDirectory(scope.module.filename)
    if fqn.split('.')[0] == os.path.split(basedir)[-1]:
        # base package in fqn matches base directory
        restOfFqn = ".".join(fqn.split('.')[1:])
        node = getModuleUsingFQN(restOfFqn,[basedir]+pythonpath)
    if node is not None:
        return node

    # try searching the python path
    node = getModuleUsingFQN(fqn,pythonpath)
    if node is not None:
        return node
Esempio n. 11
0
 def test_getsTypeOfClassReferencedViaAlias(self):
     src = trimLines("""
     from b.bah import TheClass as FooBah
     FooBah()
     """)
     root = createSourceNodeAt(src,"a.foo")
     root = createSourceNodeAt(testdata.TheClass, "a.b.bah")
     themodule = getModuleUsingFQN("a.foo",[pkgstructureRootDir])
     self.assertEqual(getTypeOf(themodule,"FooBah",[pkgstructureRootDir]).name,"TheClass")
     self.assertEqual(getTypeOf(themodule,"FooBah",[pkgstructureRootDir]).filename,
                      os.path.abspath(os.path.join("a","b","bah.py")))
Esempio n. 12
0
def resolveImportedModuleOrPackage(scope, fqn, pythonpath):
    # try searching from directory containing scope module
    path = os.path.dirname(scope.module.filename)
    node = getModuleUsingFQN(fqn, [path] + pythonpath)
    if node is not None:
        return node

    # try searching in same package hierarchy
    basedir = getPackageBaseDirectory(scope.module.filename)
    if fqn.split('.')[0] == os.path.split(basedir)[-1]:
        # base package in fqn matches base directory
        restOfFqn = ".".join(fqn.split('.')[1:])
        node = getModuleUsingFQN(restOfFqn, [basedir] + pythonpath)
    if node is not None:
        return node

    # try searching the python path
    node = getModuleUsingFQN(fqn, pythonpath)
    if node is not None:
        return node
Esempio n. 13
0
 def test_getsTypeOfClassReferenceFromImportedPackage(self):
     src = trimLines("""
     import b.bah
     a = b.bah.TheClass()
     """)
     root = createSourceNodeAt(src, "a.foo")
     root = createSourceNodeAt(testdata.TheClass, "a.b.bah")
     module = getModuleUsingFQN("a.foo", [pkgstructureRootDir])
     res = getTypeOf(module, "a", [pkgstructureRootDir])
     assert isinstance(res, Instance)
     assert isinstance(res.getType(), Class)
     assert res.getType().name == "TheClass"
Esempio n. 14
0
 def test_getsTypeOfClassReferenceFromImportedPackage(self):
     src = trimLines("""
     import b.bah
     a = b.bah.TheClass()
     """)
     root = createSourceNodeAt(src,"a.foo")
     root = createSourceNodeAt(testdata.TheClass, "a.b.bah")
     module = getModuleUsingFQN("a.foo",[pkgstructureRootDir])
     res =  getTypeOf(module,"a",[pkgstructureRootDir])
     assert isinstance(res,Instance)
     assert isinstance(res.getType(),Class)
     assert res.getType().name == "TheClass"
Esempio n. 15
0
 def test_getsTypeOfSimpleClassInstanceReference(self):
     src = trimLines("""
     from b.bah import TheClass
     a = TheClass()
     a.theMethod()
     """)
     root = createSourceNodeAt(src,"a.foo")
     root = createSourceNodeAt(testdata.TheClass, "a.b.bah")
     module = getModuleUsingFQN("a.foo",[pkgstructureRootDir])
     res =  getTypeOf(module,"a",[pkgstructureRootDir])
     assert isinstance(res,Instance)
     assert isinstance(res.getType(),Class)
     assert res.getType().name == "TheClass"
Esempio n. 16
0
 def test_doesntGetTypeDefinedInChildFunction(self):
     src = trimLines("""
     from b.bah import TheClass
     a = AnotherClass()
     def foo():
         a = TheClass()
     a.theMethod()
     """)
     root = createSourceNodeAt(src,"a.foo")
     root = createSourceNodeAt(testdata.TheClass, "a.b.bah")
     
     themodule = getModuleUsingFQN("a.foo",[pkgstructureRootDir])
     assert getTypeOf(themodule,"a",[pkgstructureRootDir]) is None
Esempio n. 17
0
 def test_getsTypeOfSimpleClassInstanceReference(self):
     src = trimLines("""
     from b.bah import TheClass
     a = TheClass()
     a.theMethod()
     """)
     root = createSourceNodeAt(src, "a.foo")
     root = createSourceNodeAt(testdata.TheClass, "a.b.bah")
     module = getModuleUsingFQN("a.foo", [pkgstructureRootDir])
     res = getTypeOf(module, "a", [pkgstructureRootDir])
     assert isinstance(res, Instance)
     assert isinstance(res.getType(), Class)
     assert res.getType().name == "TheClass"
Esempio n. 18
0
    def test_doesntGetTypeDefinedInChildFunction(self):
        src = trimLines("""
        from b.bah import TheClass
        a = AnotherClass()
        def foo():
            a = TheClass()
        a.theMethod()
        """)
        root = createSourceNodeAt(src, "a.foo")
        root = createSourceNodeAt(testdata.TheClass, "a.b.bah")

        themodule = getModuleUsingFQN("a.foo", [pkgstructureRootDir])
        assert getTypeOf(themodule, "a", [pkgstructureRootDir]) is None
Esempio n. 19
0
 def test_getsTypeOfClassReferencedViaAlias(self):
     src = trimLines("""
     from b.bah import TheClass as FooBah
     FooBah()
     """)
     root = createSourceNodeAt(src, "a.foo")
     root = createSourceNodeAt(testdata.TheClass, "a.b.bah")
     themodule = getModuleUsingFQN("a.foo", [pkgstructureRootDir])
     self.assertEqual(
         getTypeOf(themodule, "FooBah", [pkgstructureRootDir]).name,
         "TheClass")
     self.assertEqual(
         getTypeOf(themodule, "FooBah", [pkgstructureRootDir]).filename,
         os.path.abspath(os.path.join("a", "b", "bah.py")))
Esempio n. 20
0
 def test_getsTypeOfClassImportedFromPackageInitFile(self):
     initfile = trimLines("""
     from bah import TheClass
     """)
     src = trimLines("""
     from a import b
     b.TheClass()
     """)
     createSourceNodeAt(src,"a.foo")
     createSourceNodeAt(testdata.TheClass, "a.b.bah")
     createSourceNodeAt(initfile,"a.b.__init__")
     themodule = getModuleUsingFQN("a.foo",[pkgstructureRootDir])
     self.assertEqual(getTypeOf(themodule,"b.TheClass",[pkgstructureRootDir]).name,"TheClass")
     self.assertEqual(getTypeOf(themodule,"b.TheClass",[pkgstructureRootDir]).filename,
                      os.path.abspath(os.path.join("a","b","bah.py")))
Esempio n. 21
0
 def test_findsClassRefUsingFromImportStatement(self):
     src=trimLines("""
     from a.b.bah import TheClass
     """)
     classsrc=trimLines("""
     class TheClass:
         pass
     """)
     root = createSourceNodeAt(src,"a.foo")
     root = createSourceNodeAt(classsrc, "a.b.bah")
     module = getModuleUsingFQN("a.foo",[pkgstructureRootDir])
     filename = os.path.abspath(os.path.join("a","foo.py"))        
     defn = findDefinitionByCoords(filename,1,21,[])
     assert defn.filename == os.path.abspath(os.path.join("a","b","bah.py"))
     assert defn.lineno == 1
     assert defn.colno == 6
     assert defn.confidence == 100
Esempio n. 22
0
 def test_getsTypeOfInstanceThatIsAnAttributeOfSelf(self):
     src = trimLines("""
     class TheClass:
         def theMethod(self):
             pass
     
     class AnotherClass:
         def __init__(self):
             self.a = TheClass()
         def anotherFn(self):
             self.a.theMethod()
     """)
     root = createSourceNodeAt(src,"a.foo")
     module = getModuleUsingFQN('a.foo',[pkgstructureRootDir])
     theclass = getTypeOf(module,"TheClass",[pkgstructureRootDir])
     fn = getTypeOf(module,"AnotherClass.anotherFn",[pkgstructureRootDir])
     self.assertEqual("TheClass",getTypeOf(fn,"self.a",[pkgstructureRootDir]).getType().name)
Esempio n. 23
0
 def test_getsTypeOfClassImportedFromPackageInitFile(self):
     initfile = trimLines("""
     from bah import TheClass
     """)
     src = trimLines("""
     from a import b
     b.TheClass()
     """)
     createSourceNodeAt(src, "a.foo")
     createSourceNodeAt(testdata.TheClass, "a.b.bah")
     createSourceNodeAt(initfile, "a.b.__init__")
     themodule = getModuleUsingFQN("a.foo", [pkgstructureRootDir])
     self.assertEqual(
         getTypeOf(themodule, "b.TheClass", [pkgstructureRootDir]).name,
         "TheClass")
     self.assertEqual(
         getTypeOf(themodule, "b.TheClass", [pkgstructureRootDir]).filename,
         os.path.abspath(os.path.join("a", "b", "bah.py")))
Esempio n. 24
0
def handlePackageScope(package, fqn, pythonpath):
    child = package.getChild(fqn)
    if child:
        return child
    
    # try searching the fs
    node = getModuleUsingFQN(fqn,[package.path]+pythonpath)
    if node:
        return node
    
    # try the package init module
    initmod = package.getChild("__init__")
    if initmod is not None:
        type = getImportedType(initmod, fqn, pythonpath)
        if type:
            return type

    raise "Shouldn't get to here"
Esempio n. 25
0
def handlePackageScope(package, fqn, pythonpath):
    child = package.getChild(fqn)
    if child:
        return child

    # try searching the fs
    node = getModuleUsingFQN(fqn, [package.path] + pythonpath)
    if node:
        return node

    # try the package init module
    initmod = package.getChild("__init__")
    if initmod is not None:
        type = getImportedType(initmod, fqn, pythonpath)
        if type:
            return type

    raise "Shouldn't get to here"
Esempio n. 26
0
 def test_getsTypeOfInstanceThatIsAnAttributeOfSelf(self):
     src = trimLines("""
     class TheClass:
         def theMethod(self):
             pass
     
     class AnotherClass:
         def __init__(self):
             self.a = TheClass()
         def anotherFn(self):
             self.a.theMethod()
     """)
     root = createSourceNodeAt(src, "a.foo")
     module = getModuleUsingFQN('a.foo', [pkgstructureRootDir])
     theclass = getTypeOf(module, "TheClass", [pkgstructureRootDir])
     fn = getTypeOf(module, "AnotherClass.anotherFn", [pkgstructureRootDir])
     self.assertEqual(
         "TheClass",
         getTypeOf(fn, "self.a", [pkgstructureRootDir]).getType().name)
Esempio n. 27
0
def handleModuleClassOrFunctionScope(scope, fqn, pythonpath):
    assert isinstance(
        scope, Function) or isinstance(scope, Class) or isinstance(
            scope, Module), "this function cant take a scope of type" + str(
                scope.__class__)

    if fqn == "self" and scopeIsAMethod(scope):
        return Instance(scope.getParent())

    child = scope.getChild(fqn)
    if child: return child

    type = scanScopeSourceForType(scope, fqn)
    if type is not None: return type

    type = getImportedType(scope, fqn, pythonpath)  # try imported types
    if type is not None: return type

    if isinstance(
            scope,
            Module) and scope.getSourceNode().filename.endswith("__init__.py"):
        # try searching the package for a module
        packagedir = os.path.dirname(scope.getSourceNode().filename)
        node = getModuleUsingFQN(fqn, [packagedir] + pythonpath)
        if node is not None: return node

    try:
        parentScope = scope.getParent()
    except ElementHasNoParentException:
        return None  # scope is a module

    while isinstance(parentScope, Class):
        # don't search class scope, since this is not accessible except
        # through self   (is this true?)
        parentScope = parentScope.getParent()

    return getTypeOf(parentScope, fqn, pythonpath)