Example #1
0
def findDefinitionOfAttributeExpression(scope, node, pythonpath):
    assert isinstance(node,Getattr) or isinstance(node,AssAttr)
    exprtype = getTypeOfExpr(scope,node.expr, pythonpath)
    if not (exprtype is None):
        if isinstance(exprtype,Instance):
            klass = exprtype.getType()
            return findDefinitionOfClassAttributeGivenClass(klass,node.attrname,
                                                            pythonpath)
        else:
            return findDefinitionOfName(exprtype,node.attrname,pythonpath)
    else: # try a getTypeOfExpr on the expression itself
        exprtype = getTypeOfExpr(scope,node, pythonpath)
        if exprtype:
            return convertScopeToMatchObject(exprtype)

    return None
Example #2
0
    def visitAssAttr(self, node):
        for c in node.getChildNodes():
            self.visit(c)

        if node.attrname == self.targetAttributeName:
            exprtype = getTypeOfExpr(self.scope,node.expr)
            if isinstance(exprtype,Instance) and \
               exprtype.getType() in self.targetClasses:
                self.appendMatch(self.targetAttributeName)
            #else:
            #    self.appendMatch(self.targetAttributeName,50)
        self.popWordsUpTo(node.attrname)
Example #3
0
    def visitGetattr(self, node):
        for c in node.getChildNodes():
            self.visit(c)

        if node.attrname == self.targetAttributeName:
            exprtype = getTypeOfExpr(self.scope,node.expr)            

            if isinstance(exprtype,Instance) and \
                 self._isAClassInTheSameHierarchy(exprtype.getType()):
                self.appendMatch(self.targetAttributeName)
            elif isinstance(exprtype,UnfoundType) or \
                 exprtype is None:   # couldn't find type, so not sure
                self.appendMatch(self.targetAttributeName,50)
            else:
                pass # definately not a match
        self.popWordsUpTo(node.attrname)
Example #4
0
def generateRefsToAttribute(classobj,attrname,pythonpath):
    rootClasses = getRootClassesOfHierarchy(classobj,pythonpath)
    for sourcenode, linenum, col in _generateCoordsMatchingString(attrname,classobj.filename,pythonpath):
        scope = getScopeForLine(sourcenode, linenum)
        if col != 0 and sourcenode.getLines()[linenum-1][col-1] == '.':  # possible attribute
            expr = translateSourceCoordsIntoASTNode(sourcenode.filename,linenum,col)
            assert isinstance(expr,Getattr) or isinstance(expr,AssAttr)
            exprtype = getTypeOfExpr(scope,expr.expr,pythonpath)
            if isinstance(exprtype,Instance) and \
                   _isAClassInTheSameHierarchy(exprtype.getType(),rootClasses,pythonpath):
                yield createMatchObject(scope,linenum,col,attrname,100)
            elif exprtype is None:
                # can't deduce type of expression - still could be a match
                yield createMatchObject(scope,linenum,col,attrname,50)
        elif scopeIsAMethod(scope) and scope.name == attrname:  # possible method 
            if _isAClassInTheSameHierarchy(scope.getParent(),rootClasses,pythonpath):
                yield convertScopeToMatchObject(scope,100)
Example #5
0
def suggest(filename,linenum,column,pythonpath):
    s = getSourceNode(filename)
    print linenum
    line = s.getLine(linenum)
    if line[column-1] == ".":
        i = column - 2
        while i < 0:
            if re.match("\w+",line[i]):
                i -=1
            else:
                continue
        expr = line[i:column-1]
        expr =  parseLogicalLine(expr,expr,1).compilernode.nodes[0].expr
        scope = s.getScopeForLine(linenum)
        klass = getTypeOfExpr(scope,expr,pythonpath).getType()
        for scope in klass.getChildNodes():
            line = scope.generateLinesNotIncludingThoseBelongingToChildScopes().next()
            yield scope.name, line.strip()
Example #6
0
def suggest(filename, linenum, column, pythonpath):
    s = getSourceNode(filename)
    print linenum
    line = s.getLine(linenum)
    if line[column - 1] == ".":
        i = column - 2
        while i < 0:
            if re.match("\w+", line[i]):
                i -= 1
            else:
                continue
        expr = line[i:column - 1]
        expr = parseLogicalLine(expr, expr, 1).compilernode.nodes[0].expr
        scope = s.getScopeForLine(linenum)
        klass = getTypeOfExpr(scope, expr, pythonpath).getType()
        for scope in klass.getChildNodes():
            line = scope.generateLinesNotIncludingThoseBelongingToChildScopes(
            ).next()
            yield scope.name, line.strip()
Example #7
0
def findReferencesIncludingDefn_impl(sourcenode,node,scope,defnmatch,pythonpath):    
    if isinstance(node,Name) or isinstance(node,AssName):
        return generateRefsToName(node.name,scope,sourcenode,defnmatch,pythonpath)
    elif isinstance(node,Getattr) or isinstance(node,AssAttr):        
        exprtype = getTypeOfExpr(scope,node.expr,pythonpath)
        if exprtype is None:
            raise CouldntFindDefinitionException()

        if isinstance(exprtype,Instance):
            exprtype = exprtype.getType()
            return generateRefsToAttribute(exprtype,node.attrname,pythonpath)
        
        else:
            targetname = node.attrname
            return globalScanForNameReferences(targetname, sourcenode.filename, defnmatch, pythonpath)

    elif isinstance(node,compiler.ast.Function) or \
                             isinstance(node,compiler.ast.Class):
        return handleClassOrFunctionRefs(scope, node, defnmatch,pythonpath)
    else:
        assert 0,"Seed to references must be Name,Getattr,Function or Class"
Example #8
0
def findDefinitionOfClassAttributeGivenClass(klass,attrname,pythonpath):
    assert isinstance(klass,Class)

    # first scan the method names:
    for child in klass.getChildNodes():
        if child.name == attrname:
            return convertScopeToMatchObject(child,100)
    # then scan the method source for attribues
    for child in klass.getChildNodes():        
        if isinstance(child,Function):
            for attr,linenum,col in child.getAssignmentAttributesMatchingKeyword(attrname):
                exprtype = getTypeOfExpr(child,attr.expr,pythonpath)                    
                if isinstance(exprtype,Instance) and exprtype.getType() == klass:
                    return createMatchObject(child,linenum,col,attrname)
    # try the class scope
    for name,line,col, node in klass.getVariablesAssignedInScope(keyword=attrname):
        return createMatchObject(klass,line,col,name)

    # try base classes
    for baseclassname in klass.getBaseClassNames():
        match = findDefinitionOfName(klass.getParent(),baseclassname,pythonpath)
        baseclass = getScopeForLine(match.sourcenode,match.lineno)
        return findDefinitionOfClassAttributeGivenClass(baseclass,attrname,pythonpath)        
Example #9
0
def findDefinitionFromASTNode(scope,node):
    assert node is not None
    if isinstance(node,Name) or isinstance(node,AssName):
        while 1:
            # try scope children
            childscope = scope.getChild(node.name)
            if childscope is not None:
                return convertNodeToMatchObject(childscope,100)

            if isinstance(scope,Package):
                scope = scope.getChild("__init__")

            # try arguments and assignments
            match = scanScopeAST(scope,node.name,
                                 AssignmentAndFnArgsSearcher(node.name))
            if match is not None:
                return match
            
            # try imports
            match = searchImportedModulesForDefinition(scope,node)
            if match is not None:
                return match


            if not isinstance(scope,Module):
                # try parent scope
                scope = scope.getParent()
            else:
                break
        assert isinstance(scope,Module)

    elif isinstance(node,Getattr) or isinstance(node,AssAttr):
        exprtype = getTypeOfExpr(scope,node.expr)
        if not (exprtype is None or isinstance(exprtype,UnfoundType)):
            if isinstance(exprtype,Instance):
                exprtype = exprtype.getType()
                match = findDefinitionOfAttributeFromASTNode(exprtype,
                                                         node.attrname)
            else:
                match = findDefinitionFromASTNode(exprtype,
                                                  Name(node.attrname))
            if match is not None:
                return match

    elif isinstance(node,compiler.ast.Function) or \
             isinstance(node,compiler.ast.Class):
        if isAMethod(scope,node): 
            match = findDefinitionOfAttributeFromASTNode(scope,
                                                        node.name)
        else:
            match = findDefinitionFromASTNode(scope,Name(node.name))
        if match is not None:
            return match


    type = getTypeOfExpr(scope,node)
    if type is not None and (not isinstance(type,UnfoundType)) and \
                             (not isinstance(type,Instance)):
        return  convertNodeToMatchObject(type,100)
    else:
        return None