def moveFunctionToNewModule(origfile, line, newfile): srcnode = getSourceNode(origfile) targetsrcnode = getSourceNode(newfile) scope = getScopeForLine(srcnode, line) linesep = getLineSeperator(srcnode.getLines()[0]) matches = findReferences(origfile, line, scope.getColumnOfName()) origFileImport = [] fromline = 'from %s import %s' % (filenameToModulePath(newfile), scope.name) for match in matches: if match.filename == origfile: origFileImport = fromline + linesep else: s = getSourceNode(match.filename) m = s.fastparseroot if match.lineno in m.getImportLineNumbers(): getUndoStack().addSource(s.filename, s.getSource()) maskedline = m.getLogicalLine(match.lineno) origline = s.getLines()[match.lineno - 1] reMatch = re.match(exactFromRE % (scope.name), maskedline) if reMatch and not (',' in reMatch.group(2) or \ '\\' in reMatch.group(2)): restOfOrigLine = origline[len(reMatch.group(1)):] s.getLines()[match.lineno - 1] = fromline + restOfOrigLine elif re.match(fromRE, maskedline): #remove the element from the import stmt line = re.sub('%s\s*?,' % (scope.name), '', origline) s.getLines()[match.lineno - 1] = line #and add a new line nextline = match.lineno + maskedline.count('\\') + 1 s.getLines()[nextline - 1:nextline - 1] = [fromline + linesep] queueFileToSave(s.filename, s.getSource()) refs = getVariableReferencesInLines(scope.getMaskedLines()) scopeLines = srcnode.getLines()[scope.getStartLine() - 1:scope.getEndLine() - 1] importModules = deduceImportsForNewFile(refs, scope) importlines = composeNewFileImportLines(importModules, linesep) getUndoStack().addSource(srcnode.filename, srcnode.getSource()) getUndoStack().addSource(targetsrcnode.filename, targetsrcnode.getSource()) srcnode.getLines()[scope.getStartLine() - 1:scope.getEndLine() - 1] = origFileImport targetsrcnode.getLines().extend(importlines + scopeLines) queueFileToSave(srcnode.filename, srcnode.getSource()) queueFileToSave(targetsrcnode.filename, targetsrcnode.getSource())
def commit(self): for filename in self.modifiedsrc.keys(): srcnode = getSourceNode(filename) for lineno in self.modifiedsrc[filename]: lines = srcnode.getLines() lines[lineno-1] = self._dictToLine(self.modifiedsrc[filename][lineno]) queueFileToSave(filename,"".join(srcnode.getLines()))
def getModule(filename_path): from bike.parsing.load import CantLocateSourceNodeException, getSourceNode try: sourcenode = getSourceNode(filename_path) return sourcenode.fastparseroot except CantLocateSourceNodeException: return None
def getModule(filename_path): from bike.parsing.load import CantLocateSourceFileException, getSourceNode try: sourcenode = getSourceNode(filename_path) return sourcenode.fastparseroot except CantLocateSourceFileException: return None
def moveClassToNewModule(origfile, line, newfile): srcnode = getSourceNode(origfile) targetsrcnode = getSourceNode(newfile) classnode = getScopeForLine(srcnode, line) classlines = srcnode.getLines()[classnode.getStartLine() - 1:classnode.getEndLine() - 1] getUndoStack().addSource(srcnode.filename, srcnode.getSource()) getUndoStack().addSource(targetsrcnode.filename, targetsrcnode.getSource()) srcnode.getLines()[classnode.getStartLine() - 1:classnode.getEndLine() - 1] = [] targetsrcnode.getLines().extend(classlines) queueFileToSave(srcnode.filename, srcnode.getSource()) queueFileToSave(targetsrcnode.filename, targetsrcnode.getSource())
def moveClassToNewModule(origfile,line,newfile): srcnode = getSourceNode(origfile) targetsrcnode = getSourceNode(newfile) classnode = getScopeForLine(srcnode,line) classlines = srcnode.getLines()[classnode.getStartLine()-1: classnode.getEndLine()-1] getUndoStack().addSource(srcnode.filename, srcnode.getSource()) getUndoStack().addSource(targetsrcnode.filename, targetsrcnode.getSource()) srcnode.getLines()[classnode.getStartLine()-1: classnode.getEndLine()-1] = [] targetsrcnode.getLines().extend(classlines) queueFileToSave(srcnode.filename,srcnode.getSource()) queueFileToSave(targetsrcnode.filename,targetsrcnode.getSource())
def getSourceNodesContainingRegex(regexstr,contextFilename): regex = re.compile(regexstr) for fname in generateModuleFilenamesInPythonPath(contextFilename): try: f = file(fname) src = f.read() finally: f.close() if regex.search(src) is not None: yield getSourceNode(fname)
def getSourceNodesContainingRegex(regexstr, contextFilename): regex = re.compile(regexstr) for fname in generateModuleFilenamesInPythonPath(contextFilename): try: f = file(fname) src = f.read() finally: f.close() if regex.search(src) is not None: yield getSourceNode(fname)
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
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
def findReferences(filename,lineno,col,includeDefn=0,pythonpath=[]): sourcenode = getSourceNode(filename) node = translateSourceCoordsIntoASTNode(filename,lineno,col) assert node is not None scope,defnmatch = getDefinitionAndScope(sourcenode,lineno,node,pythonpath) try: for match in findReferencesIncludingDefn_impl(sourcenode,node, scope,defnmatch,pythonpath): if not includeDefn and match == defnmatch: continue # don't return definition else: yield match except CouldntFindDefinitionException: raise CouldntFindDefinitionException("Could not find definition. Please locate manually (maybe using find definition) and find references from that")
def test_walksClasses(self): src=trimLines(""" class TestClass(a, baseclass): pass """) class MyWalker: def visitClass(self, node): self.basenames = [] for name in node.bases: self.basenames.append(name.name) writeTmpTestFile(src) srcnode = getSourceNode(tmpfile) walker = MyWalker() walkLinesContainingStrings(srcnode.fastparseroot,walker, "baseclass") self.assertEqual(["a","baseclass"],walker.basenames)
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()
def test_walksClasses(self): src = trimLines(""" class TestClass(a, baseclass): pass """) class MyWalker: def visitClass(self, node): self.basenames = [] for name in node.bases: self.basenames.append(name.name) writeTmpTestFile(src) srcnode = getSourceNode(tmpfile) walker = MyWalker() walkLinesContainingStrings(srcnode.fastparseroot, walker, "baseclass") self.assertEqual(["a", "baseclass"], walker.basenames)
def findDefinitionByCoords(filepath, lineno, col, pythonpath): node = translateSourceCoordsIntoASTNode(filepath,lineno,col) if node is None: raise "selected node type not supported" scope = getScopeForLine(getSourceNode(filepath),lineno) if isinstance(node,compiler.ast.Function) or \ isinstance(node,compiler.ast.Class) or isinstance(node,compiler.ast.Keyword) \ or isinstance(node,FunctionArg): return createMatchObject(scope,lineno,col,node.name,100) if isinstance(node,ModuleName): module = resolveImportedModuleOrPackage(scope,node.modname,pythonpath) return createMatchObject(module,0,0,"") if not isinstance(scope, Module) and lineno == scope.linenum: # looking for defn in fn line scope = scope.getParent() match = findDefinitionFromASTNode(scope,node,pythonpath) return match
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()
def deduceImportsForNewFile(refs, scope): importModules = {} for ref in refs: match = findDefinitionFromASTNode(scope,Name(ref)) if match.filename == scope.module.filename: tgtscope = getScopeForLine(getSourceNode(match.filename), match.lineno) while tgtscope != scope and not isinstance(tgtscope,Module): tgtscope = tgtscope.getParent() if not isinstance(tgtscope,Module): continue # was defined in this function mpath = filenameToModulePath(match.filename) if mpath in importModules: importModules[mpath].append(ref) else: importModules[mpath] = [ref] return importModules
def findAllPossibleDefinitionsByCoords(filepath,lineno,col): #try: node = translateSourceCoordsIntoASTNode(filepath,lineno,col) #except: # import traceback # traceback.print_exc() if node is None: raise "selected node type not supported" scope = getScopeForLine(getSourceNode(filepath),lineno) match = findDefinitionFromASTNode(scope,node) if match is not None: yield match if isinstance(node,Getattr) and (match is None or match.confidence != 100): root = getRoot() name = node.attrname for match in scanPythonPathForMatchingMethodNames(name,filepath): yield match log.progress.write("done\n")
def extractLocalVariable(filename, startcoords, endcoords, varname): sourceobj = getSourceNode(filename) if startcoords.line != endcoords.line: raise "Can't do multi-line extracts yet" startcoords, endcoords = \ reverseCoordsIfWrongWayRound(startcoords,endcoords) line = sourceobj.getLine(startcoords.line) tabwidth = getTabWidthOfLine(line) linesep = getLineSeperator(line) region = line[startcoords.column:endcoords.column] getUndoStack().addSource(sourceobj.filename,sourceobj.getSource()) sourceobj.getLines()[startcoords.line-1] = \ line[:startcoords.column] + varname + line[endcoords.column:] defnline = tabwidth*" " + varname + " = " + region + linesep sourceobj.getLines().insert(startcoords.line-1,defnline) queueFileToSave(sourceobj.filename,"".join(sourceobj.getLines()))
def extractLocalVariable(filename, startcoords, endcoords, varname): sourceobj = getSourceNode(filename) if startcoords.line != endcoords.line: raise "Can't do multi-line extracts yet" startcoords, endcoords = \ reverseCoordsIfWrongWayRound(startcoords,endcoords) line = sourceobj.getLine(startcoords.line) tabwidth = getTabWidthOfLine(line) linesep = getLineSeperator(line) region = line[startcoords.column:endcoords.column] getUndoStack().addSource(sourceobj.filename, sourceobj.getSource()) sourceobj.getLines()[startcoords.line-1] = \ line[:startcoords.column] + varname + line[endcoords.column:] defnline = tabwidth * " " + varname + " = " + region + linesep sourceobj.getLines().insert(startcoords.line - 1, defnline) queueFileToSave(sourceobj.filename, "".join(sourceobj.getLines()))
def deduceImportsForNewFile(refs, scope): importModules = {} for ref in refs: match = findDefinitionFromASTNode(scope, Name(ref)) if match.filename == scope.module.filename: tgtscope = getScopeForLine(getSourceNode(match.filename), match.lineno) while tgtscope != scope and not isinstance(tgtscope, Module): tgtscope = tgtscope.getParent() if not isinstance(tgtscope, Module): continue # was defined in this function mpath = filenameToModulePath(match.filename) if mpath in importModules: importModules[mpath].append(ref) else: importModules[mpath] = [ref] return importModules
def writeSourceAndCreateNode(src): from bike.parsing.load import getSourceNode writeFile(tmpfile,src) return getSourceNode(tmpfile)
def translateSourceCoordsIntoASTNode(filename, lineno, col): sourcenode = getSourceNode(filename) return sourcenode.translateCoordsToASTNode(lineno, col)
def createAST(src): from bike.parsing.load import getSourceNode writeFile(tmpfile, src) return getSourceNode(tmpfile)
def queueFileToSave(filename,src): outputqueue[filename] = src from bike.parsing.load import getSourceNode getSourceNode(filename).resetWithSource(src)
def inlineLocalVariable(filename, lineno,col): sourceobj = getSourceNode(filename) return inlineLocalVariable_old(sourceobj, lineno,col)
def extractMethod(filename, startcoords, endcoords, newname): ExtractMethod(getSourceNode(filename), startcoords, endcoords, newname).execute()
def queueFileToSave(filename, src): outputqueue[filename] = src from bike.parsing.load import getSourceNode getSourceNode(filename).resetWithSource(src)
def translateSourceCoordsIntoASTNode(filename,lineno,col): sourcenode = getSourceNode(filename) return sourcenode.translateCoordsToASTNode(lineno,col)
def createAST(src): from bike.parsing.load import getSourceNode writeFile(tmpfile,src) return getSourceNode(tmpfile)
def moveFunctionToNewModule(origfile,line,newfile): srcnode = getSourceNode(origfile) targetsrcnode = getSourceNode(newfile) scope = getScopeForLine(srcnode,line) linesep = getLineSeperator(srcnode.getLines()[0]) matches =[m for m in findReferences(origfile, line, scope.getColumnOfName())] origFileImport = [] fromline = 'from %s import %s'%(filenameToModulePath(newfile),scope.name) for match in matches: if match.filename == origfile: origFileImport = fromline + linesep else: s = getSourceNode(match.filename) m = s.fastparseroot if match.lineno in m.getImportLineNumbers(): getUndoStack().addSource(s.filename, s.getSource()) maskedline = m.getLogicalLine(match.lineno) origline = s.getLines()[match.lineno-1] reMatch = re.match(exactFromRE%(scope.name),maskedline) if reMatch and not (',' in reMatch.group(2) or \ '\\' in reMatch.group(2)): # i.e. line is 'from module import foo' if match.filename == newfile: #remove the import s.getLines()[match.lineno-1:match.lineno] = [] pass else: restOfOrigLine = origline[len(reMatch.group(1)):] s.getLines()[match.lineno-1] = fromline + restOfOrigLine elif re.match(fromRE,maskedline): # i.e. line is 'from module import foo,bah,baz' #remove the element from the import stmt line = removeNameFromMultipleImportLine(scope.name, origline) s.getLines()[match.lineno-1] = line #and add a new line nextline = match.lineno + maskedline.count('\\') + 1 s.getLines()[nextline-1:nextline-1] = [fromline+linesep] queueFileToSave(s.filename,s.getSource()) refs = getVariableReferencesInLines(scope.getMaskedLines()) scopeLines = srcnode.getLines()[scope.getStartLine()-1: scope.getEndLine()-1] importModules = deduceImportsForNewFile(refs, scope) importlines = composeNewFileImportLines(importModules, linesep) getUndoStack().addSource(srcnode.filename, srcnode.getSource()) getUndoStack().addSource(targetsrcnode.filename, targetsrcnode.getSource()) srcnode.getLines()[scope.getStartLine()-1: scope.getEndLine()-1] = origFileImport targetsrcnode.getLines().extend(importlines+scopeLines) queueFileToSave(srcnode.filename,srcnode.getSource()) queueFileToSave(targetsrcnode.filename,targetsrcnode.getSource())
def writeSourceAndCreateNode(src): from bike.parsing.load import getSourceNode writeFile(tmpfile, src) return getSourceNode(tmpfile)