~vil/pydev/upstream

« back to all changes in this revision

Viewing changes to org.python.pydev/PySrc/refactoring.py

  • Committer: Vladimír Lapáček
  • Date: 2006-08-30 18:38:44 UTC
  • Revision ID: vladimir.lapacek@gmail.com-20060830183844-f4d82c1239a7770a
Initial import of upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
import sys
 
4
import os
 
5
import traceback
 
6
import StringIO
 
7
import urllib
 
8
 
 
9
#kind of hack to get the bicicle repair man without having it in the pythonpath.
 
10
sys.path.insert(1, os.path.join(os.path.dirname(sys.argv[0]), 
 
11
    "ThirdParty", "brm"))
 
12
import ThirdParty.brm.bike as bike
 
13
 
 
14
 
 
15
 
 
16
class Refactoring(object):
 
17
    
 
18
    def __init__(self):
 
19
        self.progress = StringIO.StringIO()
 
20
        self.warning = StringIO.StringIO()
 
21
        self.init()
 
22
 
 
23
    def getLastProgressMsg(self):
 
24
        progress = self.progress.getvalue().split('\n')
 
25
        msg = ''
 
26
        i = -1
 
27
        while msg == '' and i > -5:
 
28
            try:
 
29
                msg = progress[i]
 
30
            except:
 
31
                msg = ''
 
32
            i -= 1
 
33
        return msg
 
34
    
 
35
    def getLastProgressMsgs(self, v):
 
36
        progress = self.progress.getvalue().split('\n')
 
37
        msg = ''
 
38
        i = -1
 
39
        while i > -v:
 
40
            try:
 
41
                msg += progress[i]+'\n'
 
42
            except:
 
43
                pass
 
44
            i -= 1
 
45
        return msg
 
46
 
 
47
    def init(self):
 
48
        """
 
49
        Private slot to handle the Reset action.
 
50
        """
 
51
        self.brmctx = bike.init()
 
52
        self.brmctx.setProgressLogger(self.progress)
 
53
        self.brmctx.setWarningLogger(self.warning)
 
54
    
 
55
    def handleReset(self):
 
56
        """
 
57
        Private slot to handle the Reset action.
 
58
        """
 
59
        self.init()
 
60
        
 
61
 
 
62
    def extractMethod(self, filename, startline, startcolumn, 
 
63
                            endline, endcolumn, newname):
 
64
        '''
 
65
        Receives all as a string and changes to correct type.
 
66
        '''
 
67
        self.brmctx.extractMethod(filename, int(startline), int(startcolumn), 
 
68
                                            int(endline), int(endcolumn), 
 
69
                                            newname)
 
70
        savedfiles = self.brmctx.save()
 
71
        return str(savedfiles)
 
72
 
 
73
 
 
74
    def renameByCoordinates(self, filename, line, column, newname):
 
75
        '''
 
76
        Receives all as a string and changes to correct type.
 
77
        '''
 
78
        self.brmctx.renameByCoordinates(filename, int(line), int(column), newname)
 
79
        savedfiles = self.brmctx.save()
 
80
        return str(savedfiles)
 
81
 
 
82
 
 
83
    def inlineLocalVariable(self, filename, line, column):
 
84
        '''
 
85
        Receives all as a string and changes to correct type.
 
86
        '''
 
87
        self.brmctx.inlineLocalVariable(filename, int(line), int(column))
 
88
        savedfiles = self.brmctx.save()
 
89
        return str(savedfiles)
 
90
        
 
91
 
 
92
    def extractLocalVariable(self,filename, begin_line, begin_col,
 
93
                             end_line, end_col, newname):
 
94
        '''
 
95
        Receives all as a string and changes to correct type.
 
96
        '''
 
97
        self.brmctx.extractLocalVariable(filename, int(begin_line), int(begin_col),
 
98
                             int(end_line), int(end_col), newname)
 
99
        savedfiles = self.brmctx.save()
 
100
        return str(savedfiles)
 
101
 
 
102
    
 
103
    def findDefinition(self, filename, line, column):
 
104
        '''
 
105
        Receives all as a string and changes to correct type.
 
106
        '''
 
107
        defns = self.brmctx.findDefinitionByCoordinates(filename, int(line), int(column))
 
108
        
 
109
        ret = ''
 
110
        
 
111
        for ref in defns:
 
112
            ret += '(%s)'%str(ref)
 
113
        
 
114
        return '[%s]' % ret
 
115
 
 
116
 
 
117
__Refactoring = None
 
118
 
 
119
def GetRefactorer():
 
120
    global __Refactoring
 
121
    if __Refactoring is None:
 
122
        __Refactoring = Refactoring()
 
123
    
 
124
    return __Refactoring
 
125
    
 
126
def restartRefactorerBuffer():
 
127
    r = GetRefactorer()
 
128
    r.warning.close()
 
129
    r.progress.close()
 
130
 
 
131
    r.warning.__init__()
 
132
    r.progress.__init__()
 
133
 
 
134
def restartRefactorer():
 
135
    global __Refactoring
 
136
    __Refactoring = Refactoring()
 
137
    
 
138
def HandleRefactorMessage(msg, keepAliveThread):
 
139
    '''
 
140
    The message received should have: the method of the class and its parameters.
 
141
    '''
 
142
    msgSplit = msg.split('|')
 
143
    
 
144
    func = msgSplit.pop(0)
 
145
    
 
146
    refactorer = GetRefactorer()
 
147
    func = getattr(refactorer, func)
 
148
    
 
149
    keepAliveThread.processMsgFunc = refactorer.getLastProgressMsg
 
150
    
 
151
    try:
 
152
        f = func(*msgSplit)
 
153
        restartRefactorerBuffer()
 
154
        s = urllib.quote_plus(f)
 
155
        return 'BIKE_OK:%s\nEND@@'%(s)
 
156
    except:
 
157
        import sys
 
158
        s = StringIO.StringIO()
 
159
        exc_info = sys.exc_info()
 
160
        
 
161
        print >> s, str(exc_info[1])
 
162
        
 
163
        print >> s, '\nWARNINGS:\n'
 
164
        print >> s, refactorer.warning.getvalue()
 
165
 
 
166
        print >> s, '\nPROGRESS:\n'
 
167
        print >> s, refactorer.getLastProgressMsgs(8)
 
168
        
 
169
        print >> s, '\nDETAILS:\n'
 
170
        traceback.print_exception(exc_info[0], exc_info[1], exc_info[2], limit=None, file = s)
 
171
        
 
172
        restartRefactorerBuffer()
 
173
        restartRefactorer()
 
174
        s = urllib.quote_plus(s.getvalue())
 
175
        return 'ERROR:%s\nEND@@'%(s)
 
176
        
 
177