moveToModule.py :  » Development » Bicycle-Repair-Man » bicyclerepair-0.9 » bike » refactor » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Bicycle Repair Man 
Bicycle Repair Man » bicyclerepair 0.9 » bike » refactor » moveToModule.py
import bike.globals
from bike.parsing.load import getSourceNode
from bike.parsing.fastparserast import Module
from bike.query.common import getScopeForLine,convertNodeToMatchObject
from bike.transformer.save import queueFileToSave,save
from bike.transformer.undo import getUndoStack
from bike.refactor.extractMethod import getVariableReferencesInLines
from bike.refactor.utils import getLineSeperator
from bike.query.findDefinition import findDefinitionFromASTNode
from bike.query.findReferences import findReferences
from bike.parsing.pathutils import filenameToModulePath
from compiler.ast import Name
import re

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())


exactFromRE = "(from\s+\S+\s+import\s+%s)(.*)"    
fromRE = "from\s+\S+\s+import\s+(.*)"

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 removeNameFromMultipleImportLine(name, origline):
    def replacefn(match):
        return match.group(1)
    line = re.sub('(\W)%s\s*?,'%(name),replacefn,origline)
    return line




def composeNewFileImportLines(importModules, linesep):
    importlines = []
    for mpath in importModules:
        importlines += "from %s import %s"%(mpath,
                                  ', '.join(importModules[mpath]))
        importlines += linesep
    return importlines

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
    
    
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.