1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| LimitSize=5000 #limit size in octet
class ModList(object):
'''It is an container for several modification which has to be performed on the text'''
def __init__(self):
self.modList=[]
def append(self,mode='i',line=1,char=1,text='',rtext=''):
'''mode : 'i' (Insert) or 'r' (Replace)
line : used in 'i' mode, indicate the number of line where the text should be inserted, begins to one.
char : used in 'i' mode, indicate the index of the char where the text should be inserted, begins to zero.
rtext : used in 'r' mode, indicate the text that should be replaced
text : used in both modes, indicate the that will be inserted or will replace rtext.'''
mod=()
if mode=='i':
mod+=(mode,line,char,text)
if mode=='r':
mod+=(mode,rtext,text)
self.modList+=[mod]
self.modList.sort()
def verifyAndModify(self,num_line,line):
'''check if the num_line is in one of the modification, if yes modifies it, else no modification is done.
the (un)modified line is returned'''
lenOfMod=0
for mod in self.modList:
if mod[0]=='i' and num_line==mod[1]:
line=line[:mod[2]+lenOfMod]+mod[3]+line[mod[2]+lenOfMod:]
lenOfMod+=len(mod[3])
if mod[0]=='r' and mod[1] in line:
line=line.replace(mod[1],mod[2])
return line
def copyModFile(modifList,f_inName,f_outName):
if not isinstance(modifList,ModList): raise Exception('modifList parameter must be a ModifListObject')
f_in=open(f_inName,'r')
f_out=open(f_outName,'w')
copy=''
copy_size=0
line_count=0
while 1:
line=f_in.readline()
line_count+=1
#count+=1
if line:
mod=modifList.verifyAndModify(line_count,line)
print mod
copy+=mod
copy_size+=len(mod)
if copy_size>LimitSize:
f_out.write(copy)
copy=''
copy_size=0
else :
if copy!='':
f_out.write(copy)
f_in.close()
f_out.close()
break |
Partager