def deduceArguments(self): lines = self.fn.getLinesNotIncludingThoseBelongingToChildScopes() # strip off comments lines = [commentRE.sub(self.linesep,line) for line in lines] extractedLines = maskStringsAndRemoveComments("".join(self.extractedLines)).splitlines(1) linesbefore = lines[:(self.startline - self.fn.getStartLine())] linesafter = lines[(self.endline - self.fn.getStartLine()) + 1:] # split into logical lines linesbefore = [line for line in generateLogicalLines(linesbefore)] extractedLines = [line for line in generateLogicalLines(extractedLines)] linesafter = [line for line in generateLogicalLines(linesafter)] if self.startline == self.endline: # need to include the line code is extracted from line = generateLogicalLines(lines[self.startline - self.fn.getStartLine():]).next() linesbefore.append(line[:self.startcol] + "dummyFn()" + line[self.endcol:]) assigns = getAssignments(linesbefore) fnargs = getFunctionArgs(linesbefore) candidateArgs = assigns + fnargs refs = getVariableReferencesInLines(extractedLines) self.newfn.args = [ref for ref in refs if ref in candidateArgs] assignsInExtractedBlock = getAssignments(extractedLines) usesAfterNewFunctionCall = getVariableReferencesInLines(linesafter) usesInPreceedingLoop = getVariableReferencesInLines( self.getPreceedingLinesInLoop(linesbefore,line)) self.newfn.retvals = [ref for ref in usesInPreceedingLoop+usesAfterNewFunctionCall if ref in assignsInExtractedBlock]
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")
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")
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
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
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 = compiler.walk(ast, astWalker) lineno+=line.count("\n")
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")
def getPreceedingLinesInLoop(self,linesbefore,firstLineToExtract): if linesbefore == []: return [] tabwidth = getTabWidthOfLine(firstLineToExtract) rootTabwidth = getTabWidthOfLine(linesbefore[0]) llines = [line for line in generateLogicalLines(linesbefore)] startpos = len(llines)-1 loopTabwidth = tabwidth for idx in range(startpos,0,-1): line = llines[idx] if re.match("(\s+)for",line) is not None or \ re.match("(\s+)while",line) is not None: candidateLoopTabwidth = getTabWidthOfLine(line) if candidateLoopTabwidth < loopTabwidth: startpos = idx return llines[startpos:]
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
def scanScopeForMatches(sourcenode,scope,matchFinder,targetname): lineno = scope.getStartLine() for line in generateLogicalLines(scope.getMaskedLines()): if line.find(targetname) != -1: doctoredline = makeLineParseable(line) ast = compiler.parse(doctoredline) scope = getScopeForLine(sourcenode, lineno) matchFinder.reset(line) matchFinder.setScope(scope) matches = compiler.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")
def scanScopeForMatches(sourcenode, scope, matchFinder, targetname): lineno = scope.getStartLine() for line in generateLogicalLines(scope.getMaskedLines()): if line.find(targetname) != -1: doctoredline = makeLineParseable(line) ast = compiler.parse(doctoredline) scope = getScopeForLine(sourcenode, lineno) matchFinder.reset(line) matchFinder.setScope(scope) matches = compiler.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")
def getLogicalLine(lines,lineno): return generateLogicalLines(lines[lineno-1:]).next()