예제 #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()
예제 #2
0
 def test_worksForFullPath(self):
     try:
         createPackageStructure("pass","pass")
         self.assertEqual(pkgstructureFile2,
                          getModuleUsingFQN("a.b.bah",[pkgstructureRootDir]).filename)
     finally:
         removePackageStructure()
예제 #3
0
파일: getTypeOf.py 프로젝트: lebauce/artub
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)
예제 #4
0
 def test_worksForFullPath(self):
     try:
         createPackageStructure("pass", "pass")
         self.assertEqual(
             pkgstructureFile2,
             getModuleUsingFQN("a.b.bah", [pkgstructureRootDir]).filename)
     finally:
         removePackageStructure()
예제 #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()
예제 #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!
예제 #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!
예제 #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!
예제 #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!
예제 #10
0
파일: getTypeOf.py 프로젝트: lebauce/artub
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
예제 #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")))
예제 #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
예제 #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"
예제 #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"
예제 #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"
예제 #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
예제 #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"
예제 #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
예제 #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")))
예제 #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")))
예제 #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
예제 #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)
예제 #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")))
예제 #24
0
파일: getTypeOf.py 프로젝트: lebauce/artub
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"
예제 #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"
예제 #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)
예제 #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)