Пример #1
0
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())
Пример #2
0
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()))
Пример #3
0
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()))
Пример #4
0
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())
Пример #5
0
 def getLineSeperator(self):
     line = self.sourcenode.getLines()[self.startline-1]
     linesep = getLineSeperator(line)
     self.linesep = linesep