5
def __init__(self,l=[]):
10
if type(tup) == type(()) :
13
self.list.append((tup,-1))
18
self.list = self.list[:-1]
23
return self.list[self.sp]
26
if self.sp < len(self.list) :
35
def __init__(self,filename=None):
41
raise 'you must give a filename or a file descriptor'
43
self.filename = filename
48
#######################
49
#######################
50
## Private Methodes ###
51
#######################
52
#######################
56
self.fd = open(self.filename, 'r')
58
print 'file alerady opened'
61
self.buffer = self.fd.read()
62
self.end = len(self.buffer)
76
self.fd = open(self.filename,'w')
77
self.fd.write(self.buffer)
79
self.fd = open(self.filename,'r')
84
def writeTo(self,filename):
85
open(filename,'w').write(self.getBuffer())
93
def setPosition(self,pos):
94
self.pos.push((pos,-1))
97
self.pos[self.pos.sp][1] = e
99
def setScope(self,s,e):
102
def getPosition(self):
103
return self.pos.top()[0]
106
return self.pos.top()[1]
109
return self.pos.top()
111
def gotoPreviousPosition(self):
112
return self.pos.pop()
114
def gotoNexPosition(self):
115
return self.pos.next()
117
def gotoStartOfTag(self,tag):
119
matcho = tag.search(self.buffer[s:e])
121
print 'tag ',tag.pattern,' not found in', self.filename
123
self.pos.push(matcho.start()+s)
124
return self.pos.top()
126
def gotoEndOfTag(self,tag):
128
matcho = tag.search(self.buffer[s:e])
130
print 'tag ',tag.pattern,' not found in', self.filename
132
self.pos.push(matcho.end()+s)
133
return self.pos.top()
135
def getStartPositionOfTag(self,tag):
137
matcho = tag.search(self.buffer[s:e])
139
print 'tag ',tag.pattern,' not found in', self.filename
141
return matcho.start() +s
143
def getEndPositionOfTag(self,tag):
145
matcho = tag.search(self.buffer[s:e])
147
print 'tag ',tag.pattern,' not found in', self.filename
149
return matcho.end() +s
158
def context(self,before=50,after=50,start=None,end=None):
160
Displays "before" characters before the actual position and "after" lines after the actual position.
163
start = self.getPosition() - before
165
end = self.getPosition() + after
167
textAround = self.buffer[start:]+"MARK"+self.buffer[:end]
170
######################## ################################################
171
######################## ########################
172
######################## ########################
173
########################
174
########################
175
########################
176
## Text manipultions ##
177
########################
178
########################
179
########################
180
######################## ################################################
181
######################## ########################
182
######################## ########################
183
########################
185
########################
194
def getOneOf(self,listOfTags):
196
for tag in listOfTags :
197
matcho = tag.search(self.buffer[s:e])
200
print 'none of the tags were found'
203
def getTextBetweenPositions(self,s,e):
204
return self.buffer[s:e]
206
def getTextBetweenTags(self,startTag,endTag):
208
matcho = startTag.search(self.buffer[s:e])
210
print 'tag ',startTag.pattern,' not found in', self.filename
212
startpos = matcho.end()
213
matcho = endTag.search(self.buffer[s:e])
215
print 'tag ',endTag.pattern,' not found in', self.filename
217
endpos = matcho.start()
218
return self.buffer[s+startpos:s+endpos]
220
#####################
221
#####################
223
#####################
224
#####################
226
def replaceText(self,tag,replacementText):
228
matcho = tag.search(self.buffer[s:e])
230
print 'tag ',tag.pattern, 'not found in',self.filename
232
startpos = matcho.start()
233
endpos = matcho.end()
234
self.buffer = self.buffer[:s+startpos] + replacementText + self.buffer[s+endpos:]
235
self.setPosition(s+startpos+len(replacementText))
237
def replaceTextBetweenPositions(self,s,e,repText):
238
self.buffer = self.buffer[:s] + repText + self.buffer[e:]
240
def replaceTextBetweenTags(self,startTag,endTag,repText):
242
matcho = startTag.search(self.buffer[s:e])
244
print 'tag ',startTag.pattern,' not found in', self.filename
246
startpos = matcho.end()
247
matcho = endTag.search(self.buffer[s:e])
249
print 'tag ',endTag.pattern,' not found in', self.filename
251
endpos = matcho.start()
252
self.buffer = self.buffer[:s+startpos] + repText +self.buffer[s+endpos:]
259
def removeTextBetweenPositions(self,start,end):
261
self.buffer = self.buffer[:start] + self.buffer[end:]
263
def removeTextBetweenTags(self,startTag,endTag,advance=0):
265
matcho = startTag.search(self.buffer[s:e])
267
print 'tag ',startTag.pattern,' not found in', self.filename
269
startpos = matcho.end()
270
matcho = endTag.search(self.buffer[s:e])
272
print 'tag ',endTag.pattern,' not found in', self.filename
274
endpos = matcho.start()
275
self.buffer = self.buffer[:s+startpos] + self.buffer[s+endpos:]
277
self.pos.push(s+endpos)
280
def removeTagsAndTextBeteween(self,startTag,endTag):
282
matcho = startTag.search(self.buffer[s:e])
284
print 'tag ',startTag.pattern,' not found in', self.filename
286
startpos = matcho.start()
287
matcho = endTag.search(self.buffer[s:e])
289
print 'tag ',endTag.pattern,' not found in', self.filename
291
endpos = matcho.end()
292
self.buffer = self.buffer[:s+startpos] + self.buffer[s+endpos:]
294
#####################
295
#####################
297
#####################
298
#####################
300
def insertTextAtPosition(self,pos,text):
301
self.buffer = self.buffer[:pos] + text + self.buffer[pos:]
303
def insertText(self,text):
305
self.buffer = self.buffer[:s] + text + self.buffer[s:]
306
self.setPosition(s+len(text))
309
######################
310
######################
311
## Matching objects ##
312
######################
313
######################
315
def getMatcho(self,tag):
317
return tag.search(self.buffer[s:e])