Beispiel #1
0
def findDefinitionOfName(scope, targetname, pythonpath):
    while 1:
        # try scope children
        childscope = scope.getChild(targetname)
        if childscope is not None:
            return convertScopeToMatchObject(childscope,100)

        # check variables created in this scope
        for name,line,col, node in scope.getVariablesAssignedInScope(keyword=targetname):
            return createMatchObject(scope,line,col,name)

        # try imports
        match = searchImportedModulesForDefinition(scope,targetname,pythonpath)
        if match is not None:
            return match
    
        if not isinstance(scope,Module):
            if scopeIsAMethod(scope):
                # can't access class scope from a method,
                scope = scope.getParent().getParent()
            else:
                # try parent scope
                scope = scope.getParent()
        else: 
            break 
    return None # couldn't find it
Beispiel #2
0
def getDefinitionAndScope(sourcenode,lineno,node,pythonpath):
    scope = getScopeForLine(sourcenode,lineno)
    if scope.getStartLine() == lineno and \
           scope.matchesCompilerNode(node):  # scope is the node
        return scope.getParent(), convertScopeToMatchObject(scope,100)
    defnmatch = findDefinitionFromASTNode(scope,node,pythonpath)
    if defnmatch is None:
        raise CouldntFindDefinitionException("Couldn't find definition")
    scope = getScopeForLine(sourcenode,defnmatch.lineno)
    return scope,defnmatch
Beispiel #3
0
def _getTypeFromCoordinates(filepath,lineno,col,pythonpath=[]):
    from bike.query.common import getScopeForLine, translateSourceCoordsIntoASTNode, convertScopeToMatchObject
    from bike.parsing.load import getSourceNode
    node = translateSourceCoordsIntoASTNode(filepath,lineno,col)
    scope = getScopeForLine(getSourceNode(filepath),lineno)
    scope = getTypeOfExpr(scope,node,pythonpath)
    if scope is not None:
        if isinstance(scope,Instance):
            scope = scope.getType()
        return convertScopeToMatchObject(scope)
    else:
        return None
Beispiel #4
0
def _getTypeFromCoordinates(filepath, lineno, col, pythonpath=[]):
    from bike.query.common import getScopeForLine, translateSourceCoordsIntoASTNode, convertScopeToMatchObject
    from bike.parsing.load import getSourceNode
    node = translateSourceCoordsIntoASTNode(filepath, lineno, col)
    scope = getScopeForLine(getSourceNode(filepath), lineno)
    scope = getTypeOfExpr(scope, node, pythonpath)
    if scope is not None:
        if isinstance(scope, Instance):
            scope = scope.getType()
        return convertScopeToMatchObject(scope)
    else:
        return None
Beispiel #5
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
Beispiel #6
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)
Beispiel #7
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)        
Beispiel #8
0
def scanPythonPathForMatchingMethodNames(name, contextFilename, pythonpath):
    for srcnode in getSourceNodesContainingRegex(name,contextFilename,pythonpath):
        for scope in srcnode.getFlattenedListOfScopes():
            if scope.name == name and scopeIsAMethod(scope):
                yield convertScopeToMatchObject(scope,50)