Пример #1
0
    def getArguments(self):
        lineast = self.generateTreesForLinesMatchingKeyword("def").next()
        args = []

        class FnVisitor(_ASTLineDecorator):
            def visitFunction(self, node):
                node.index = self.positions[1]
                assert self.words[1] == "def"
                self.popWordsUpTo(node.name)
                for arg, default in self.zipArgs(node.argnames, node.defaults,
                                                 node.kwargs):
                    if self.words[0].endswith('**'):
                        args.append(("**" + arg, self.positions[1], default))
                    else:
                        args.append((arg, self.positions[1], default))
                    self.popWordsUpTo(arg)
                    if default is not None:
                        self.visit(default)

        visitor.walk(lineast.compilernode, FnVisitor(lineast.maskedline))

        for arg, index, expr in args:
            x, y = indexToCoordinates(lineast.maskedline, index)
            y += lineast.linenumber
            yield arg, y, x, expr
Пример #2
0
def getAssignments(lines):
    class AssignVisitor:
        def __init__(self):
            self.assigns = []

        def visitAssTuple(self, node):
            for a in node.nodes:
                if a.name not in self.assigns:
                    self.assigns.append(a.name)

        def visitAssName(self, node):
            if node.name not in self.assigns:
                self.assigns.append(node.name)

        def visitAugAssign(self, node):
            if isinstance(node.node, compiler.ast.Name):
                if node.node.name not in self.assigns:
                    self.assigns.append(node.node.name)

    assignfinder = AssignVisitor()
    for line in lines:
        doctoredline = makeLineParseable(line)
        try:
            ast = compiler.parse(doctoredline)
        except ParserError:
            raise ParserException("couldnt parse:"+doctoredline)
        visitor.walk(ast, assignfinder)
    return assignfinder.assigns
Пример #3
0
 def getVariablesAssignedInScope(self,**kwargs): 
     keyword = kwargs.get("keyword",None)
     for lineast in self.generateTreesForLinesMatchingKeyword(keyword):
         n = AssignmentVisitor(lineast.maskedline,keyword)
         visitor.walk(lineast.compilernode,n)
         for name,index,node in n.results:
             x,y = indexToCoordinates(lineast.maskedline,index)
             y += lineast.linenumber
             yield name,y,x,node
Пример #4
0
 def getVariablesAssignedInScope(self, **kwargs):
     keyword = kwargs.get("keyword", None)
     for lineast in self.generateTreesForLinesMatchingKeyword(keyword):
         n = AssignmentVisitor(lineast.maskedline, keyword)
         visitor.walk(lineast.compilernode, n)
         for name, index, node in n.results:
             x, y = indexToCoordinates(lineast.maskedline, index)
             y += lineast.linenumber
             yield name, y, x, node
Пример #5
0
def scanScopeForMatches(sourcenode, scope, matchFinder, targetname):
    lineno = scope.getStartLine()
    for line in generateLogicalLines(scope.getMaskedLines()):
        if line.find(targetname) != -1:
            doctoredline = makeLineParseable(line)
            try:
                ast = compiler.parse(doctoredline)
            except:
                log.warning.write('Error parsing: %s\n' % doctoredline)
                log.warning.write('Params: \n--%s\n--%s\n--%s\n--%s\n' %
                                  (sourcenode, scope, matchFinder, targetname))
                raise
            scope = getScopeForLine(sourcenode, lineno)
            matchFinder.reset(line)
            matchFinder.setScope(scope)
            matches = visitor.walk(ast, matchFinder).getMatches()
            for index, confidence in matches:
                match = Match()
                match.filename = sourcenode.filename
                match.sourcenode = sourcenode
                x, y = indexToCoordinates(line, index)
                match.lineno = lineno + y
                match.colno = x
                match.colend = match.colno + len(targetname)
                match.confidence = confidence
                yield match
        lineno += line.count("\n")
Пример #6
0
def scanScopeForMatches(sourcenode,scope,matchFinder,targetname):
    lineno = scope.getStartLine()
    for line in generateLogicalLines(scope.getMaskedLines()):
        if line.find(targetname) != -1:
            doctoredline = makeLineParseable(line)
            try:
                ast = compiler.parse(doctoredline)
            except :
                log.warning.write('Error parsing: %s\n' % doctoredline)
                log.warning.write('Params: \n--%s\n--%s\n--%s\n--%s\n' % (sourcenode,scope,matchFinder,targetname))
                raise
            scope = getScopeForLine(sourcenode, lineno)
            matchFinder.reset(line)
            matchFinder.setScope(scope)
            matches = visitor.walk(ast, matchFinder).getMatches()
            for index, confidence in matches:
                match = Match()
                match.filename = sourcenode.filename
                match.sourcenode = sourcenode
                x, y = indexToCoordinates(line, index)
                match.lineno = lineno+y
                match.colno = x
                match.colend = match.colno+len(targetname)
                match.confidence = confidence
                yield match
        lineno+=line.count("\n")
Пример #7
0
def getVariableReferencesInLines(lines):
    class NameVisitor:
        def __init__(self):
            self.result = []
        def visitName(self, node):
            if node.name not in self.result:
                self.result.append(node.name)
    reffinder = NameVisitor()
    for line in lines:
        doctoredline = makeLineParseable(line)
        try:
            ast = compiler.parse(doctoredline)
        except ParserError:
            raise ParserException("couldnt parse:"+doctoredline)
        visitor.walk(ast, reffinder)
    return reffinder.result
Пример #8
0
def searchImportedModulesForDefinition(scope,node):
    lines = scope.module.getSourceNode().getLines()
    for lineno in scope.getImportLineNumbers():
        logicalline = getLogicalLine(lines,lineno)
        logicalline = makeLineParseable(logicalline)
        ast = compiler.parse(logicalline)
        class ImportVisitor:
            def __init__(self,node):
                self.target = node
                self.match = None
                assert isinstance(self.target,Name), \
                       "Getattr not supported"
                
            def visitFrom(self, node):
                module = resolveImportedModuleOrPackage(scope,node.modname)
                if module is None: # couldn't find module
                    return 
                
                if node.names[0][0] == '*': # e.g. from foo import *
                    match = findDefinitionFromASTNode(module,self.target)
                    if match is not None:
                        self.match = match
                    return
                    
                for name, alias in node.names:
                    if alias is None and name == self.target.name:
                        match = findDefinitionFromASTNode(module,self.target)
                        if match is not None:
                            self.match = match
                        return


        match = visitor.walk(ast, ImportVisitor(node)).match
        if match:
            return match
Пример #9
0
def walkLinesContainingStrings(scope, astWalker, targetnames):
    lineno = scope.getStartLine()
    for line in generateLogicalLines(scope.getMaskedLines()):
        if lineContainsOneOf(line, targetnames):
            doctoredline = makeLineParseable(line)
            ast = compiler.parse(doctoredline)
            astWalker.lineno = lineno
            matches = visitor.walk(ast, astWalker)
        lineno += line.count("\n")
Пример #10
0
def walkLinesContainingStrings(scope,astWalker,targetnames):
    lineno = scope.getStartLine()
    for line in generateLogicalLines(scope.getMaskedLines()):
        if lineContainsOneOf(line,targetnames):
            doctoredline = makeLineParseable(line)
            ast = compiler.parse(doctoredline)
            astWalker.lineno = lineno
            matches = visitor.walk(ast, astWalker)
        lineno+=line.count("\n")
Пример #11
0
def getImportedType(scope, fqn):
    lines = scope.module.getSourceNode().getLines()
    for lineno in scope.getImportLineNumbers():
        logicalline = generateLogicalLines(lines[lineno - 1:]).next()
        logicalline = makeLineParseable(logicalline)
        ast = compiler.parse(logicalline)
        match = visitor.walk(ast, ImportVisitor(scope, fqn)).match
        if match:
            return match
Пример #12
0
def getImportedType(scope, fqn):
    lines = scope.module.getSourceNode().getLines()
    for lineno in scope.getImportLineNumbers():
        logicalline = generateLogicalLines(lines[lineno-1:]).next()
        logicalline = makeLineParseable(logicalline)
        ast = compiler.parse(logicalline)
        match = visitor.walk(ast, ImportVisitor(scope,fqn)).match
        if match:
            return match
Пример #13
0
def scanScopeAST(scope,keyword,astvisitor):
    lines = scope.getLinesNotIncludingThoseBelongingToChildScopes()
    src = ''.join(lines)
    match = None
    #print "scanScopeAST:"+str(scope)
    for line in splitLogicalLines(src):
        if isWordInLine(keyword, line):
            #print "scanning for "+keyword+" in line:"+line[:-1]
            doctoredline = makeLineParseable(line)
            ast = compiler.parse(doctoredline)
            match = visitor.walk(ast,astvisitor).getMatch()
            if match:
                return match
    return match
Пример #14
0
    def getArguments(self):
        lineast = self.generateTreesForLinesMatchingKeyword("def").next()
        args = []
        class FnVisitor(_ASTLineDecorator):
            def visitFunction(self,node):
                node.index = self.positions[1]
                assert self.words[1] == "def"
                self.popWordsUpTo(node.name)
                for arg, default in self.zipArgs(node.argnames, node.defaults, node.kwargs):
                    if self.words[0].endswith('**'):
                        args.append(("**"+arg,self.positions[1],default))
                    else:
                        args.append((arg,self.positions[1],default))
                    self.popWordsUpTo(arg)
                    if default is not None:
                        self.visit(default)

        visitor.walk(lineast.compilernode,FnVisitor(lineast.maskedline))

        for arg,index,expr in args:
            x,y = indexToCoordinates(lineast.maskedline,index)
            y += lineast.linenumber
            yield arg,y,x,expr
Пример #15
0
def scanScopeAST(scope,keyword,matchfinder):
    lines = scope.generateLinesNotIncludingThoseBelongingToChildScopes()
    match = None
    for line,linenum in generateLogicalLinesAndLineNumbers(lines):
        if isWordInLine(keyword, line):
            doctoredline = makeLineParseable(line)
            ast = compiler.parse(doctoredline)
            matchfinder.reset(line)
            match = visitor.walk(ast,matchfinder).getMatch()
            if match is not None:
                column,yoffset = indexToCoordinates(line,match)
                m = createMatch(scope,linenum + yoffset,column)
                return m
    return None
Пример #16
0
def scanScopeAST(scope, keyword, astvisitor):
    lines = scope.getLinesNotIncludingThoseBelongingToChildScopes()
    src = ''.join(lines)
    match = None
    #print "scanScopeAST:"+str(scope)
    for line in splitLogicalLines(src):
        if isWordInLine(keyword, line):
            #print "scanning for "+keyword+" in line:"+line[:-1]
            doctoredline = makeLineParseable(line)
            ast = compiler.parse(doctoredline)
            match = visitor.walk(ast, astvisitor).getMatch()
            if match:
                return match
    return match
Пример #17
0
def getFunctionArgs(lines):
    if lines == []: return []

    class FunctionVisitor:
        def __init__(self):
            self.result = []
        def visitFunction(self, node):
            for n in node.argnames:
                if n != "self":
                    self.result.append(n)
    fndef = generateLogicalLines(lines).next()
    doctoredline = makeLineParseable(fndef)
    try:
        ast = compiler.parse(doctoredline)
    except ParserError:
        raise ParserException("couldnt parse:"+doctoredline)
    return visitor.walk(ast, FunctionVisitor()).result
Пример #18
0
def decorateCompilerNodesWithCoordinates(lineast):
    from bike.parsing import visitor
    a = _ASTLineDecorator(lineast.maskedline)
    visitor.walk(lineast.compilernode, a)