PSCF v1.1
text.py
1'''! Utilities for manipulating and searching text strings. '''
2
3# import os
4import re
5from os.path import *
6from string import *
7from pscfpp.file import *
8
9
12class Wrapper:
13
14
28 def __init__(self, eol = '\n', nIndent = 0):
29 self.eol = eol
30 self.nIndent = nIndent
31 self.column = 0
32 self.text = ''
33 self.limit = 78
34
35
38 def clear(self):
39 self.column = 0
40 self.text = ''
41
42
47 def append(self, string):
48 size = len(string)
49 if (self.column + 1 + size > self.limit):
50 self.text += self.eol
51 self.column = 0
52 for i in range(self.nIndent):
53 self.text += ' '
54 self.column = self.nIndent
55 elif (self.column > self.nIndent):
56 self.text += ' '
57 self.column += 1
58 self.text += string
59 self.column += size
60
61
64 def __str__(self):
65 return self.text
66
67
70 def __repr__(self):
71 return self.text
72
73
90class Record:
91
92
97 def __init__(self, line):
98 if line[-1] == '\n':
99 line = line[:-1]
100 self.line = line
101 self.spaces = []
102 self.fields = []
103 n = 0
104 blank = True
105 gap = ''
106 for i in range(len(line)):
107 if blank:
108 if line[i] != ' ':
109 begin = i
110 blank = False
111 else:
112 gap = gap + ' '
113 else:
114 if line[i] == ' ':
115 end = i
116 blank = True
117 self.fields.append(line[begin:end])
118 self.spaces.append(gap)
119 gap = ' '
120 n += 1
121 if not blank:
122 end = len(line)
123 self.fields.append(line[begin:])
124 self.spaces.append(gap)
125 n +=1
126 self.size = n
127
128
131 def __str__(self):
132 line = ''
133 for i in range(self.size):
134 line += self.spaces[i]
135 line += self.fields[i]
136 return line
137
138
145
146
149 def __init__(self):
150 self.hasFilter = False
151 self.hasNew = False
152 self.isReady = False
153 self.isTest = True
154
155
161 def setFilter(self, filter, field = 0):
162 self.filter = filter
163 self.filterField = field
164 self.hasFilter = True
165 if self.hasNew:
166 self.isReady = True
167
168
174 def setNew(self, new, field):
175 self.new = new
176 self.dataField = field
177 self.hasNew = True
178 if self.hasFilter:
179 self.isReady = True
180
181
186 def setIsTest(self, isTest):
187 self.isTest = isTest
188
189
194 def editFile(self, filename):
195 if (not self.isReady):
196 print('RecordEditor is not ready')
197 return
198
199 oldfile = open(filename, 'r')
200 lines = oldfile.readlines()
201 n = len(lines)
202 oldfile.close()
203
204 if (not self.isTest):
205 newfile = open (filename, 'w')
206 found = False
207 for line in lines:
208 record = Record(line)
209 if (self.filter == record.fields[self.filterField]):
210 record.fields[self.dataField] = self.new
211 if (not self.isTest):
212 newfile.write(str(record) + '\n')
213 if (not self.isTest):
214 newfile.close()
215
216
217
232def readLabelledLine(file, label):
233 line = file.readline()
234 groups = line.strip().split('=')
235 if groups[0].strip() != label:
236 print('Error: Expected label = ' + label)
237 print(' : Found = ' + groups[0].strip())
238 raise
239 if len(groups) > 2:
240 print('Error: More than one = sign in line')
241 raise
242 if len(groups) == 1:
243 return ''
244 else:
245 return groups[1].strip()
246
247
263def readLabelledList(file, label):
264 line = readLabelledLine(file, label)
265 list = []
266 if line != '':
267 groups = line.split(' ')
268 for i in range(len(groups)):
269 if groups[i] != '':
270 list.append(groups[i])
271 return list
272
273
274
283class Grep:
284
285
288 def __init__(self):
289 self.nFilter = 0
290 self.filters = []
291 self.results = []
292
293
298 def addFilter(self, filter):
299 self.filters.append(re.compile(filter))
300 self.results.append(0)
301 self.nFilter += 1
302
303
306 def clearResults(self):
307 for i in range(self.nFilter):
308 self.results[i] = 0
309
310
315 def grep(self, filename):
316 file = open(filename, 'r')
317 lines = file.readlines()
318 n = len(lines)
319 file.close()
320
321 for line in lines:
322 for i in range(self.nFilter):
323 if (self.filters[i].search(line)):
324 self.results[i] += 1
325
326
327
348
349
352 def __init__(self):
353 self.hasFilter = False
354 self.hasNew = False
355 self.hasOld = False
356 self.isReady = False
357 self.isTest = True
358 self.blockSize = 1
359
360
369 def setFilter(self, filter):
370 self.filter = re.compile(filter)
371 self.hasFilter = True
372 if (self.hasOld and self.hasNew):
373 self.isReady = True
374
375
383 def setOld(self, old):
384 self.old = old
385 self.hasOld = True
386 if (self.hasFilter and self.hasNew):
387 self.isReady = True
388
389
394 def setNew(self, new):
395 self.new = new
396 self.hasNew = True
397 if (self.hasFilter and self.hasOld):
398 self.isReady = True
399
400
409 def setIsTest(self, isTest):
410 self.isTest = isTest
411
412
417 def setBlockSize(self, blockSize):
418 self.blockSize = blockSize
419
420
425 def editFile(self, filename):
426
427 if (not self.isReady):
428 print('FileEditor is not ready')
429 return
430
431 oldfile = open(filename, 'r')
432 lines = oldfile.readlines()
433 n = len(lines)
434 oldfile.close()
435
436 if (not self.isTest):
437 newfile = open (filename, 'w')
438 found = False
439 for line in lines:
440 if (self.filter.search(line)):
441 if (not found):
442 print(filename)
443 found = True
444 print(line)
445 line = re.sub(self.old, self.new, line);
446 print(line)
447 if (not self.isTest):
448 newfile.write(line)
449 if (not self.isTest):
450 newfile.close()
451
452
457 def editFileBlocks(self, filename):
458
459 if (not self.isReady):
460 print('FileEditor is not ready')
461 return
462
463 oldfile = open(filename, 'r')
464 lines = oldfile.readlines()
465 n = len(lines)
466 oldfile.close()
467
468 if (not self.isTest):
469 newfile = open (filename, 'w')
470
471 found = False
472 for i in range(n + 1 - self.blockSize):
473 block = join(lines[i:i + self.blockSize], '')
474 if (self.filter.search(block)):
475 if (not found):
476 print(filename)
477 found = True
478 print(block)
479 block = re.sub(self.old, self.new, block)
480 print(block)
481 if (block[-1] == '\n'):
482 block = block[0:-1]
483 list = split(block, '\n')
484 for j in range(len(list)):
485 list[j] = list[j] + '\n'
486 lines[i:i+self.blockSize] = list
487 if (not self.isTest):
488 for line in lines:
489 newfile.write(line)
490 newfile.close()
491
492
498 def editFiles(self, dirName, pattern):
499 if (not self.isReady):
500 print('FileEditor is not ready')
501 return
502 dir = Directory(dirName)
503 filenames = dir.filenames(pattern)
504 for filename in filenames:
505 if isfile(filename):
506 self.editFileBlocks(filename)
507
Class that represents a directory.
Definition: file.py:190
Class to substitute text in one or more files.
Definition: text.py:347
def __init__(self)
Constructor.
Definition: text.py:352
def setIsTest(self, isTest)
Set the isTest boolean flag.
Definition: text.py:409
def setNew(self, new)
Set the new string (the replacement string)
Definition: text.py:394
def setOld(self, old)
Set the old string (the string to be replaced)
Definition: text.py:383
def setFilter(self, filter)
Set the filter string.
Definition: text.py:369
def editFile(self, filename)
Edit a single file - edits confined to individual lines.
Definition: text.py:425
def editFiles(self, dirName, pattern)
Edit all files in specified directory that matches a pattern.
Definition: text.py:498
def setBlockSize(self, blockSize)
Set the blockSize attribute.
Definition: text.py:417
def editFileBlocks(self, filename)
Edit a single file - edits may span several lines.
Definition: text.py:457
Class to search for text in a file.
Definition: text.py:283
def grep(self, filename)
Search for lines in the file that match any filter.
Definition: text.py:315
def clearResults(self)
Clear results list (but not filters)
Definition: text.py:306
def addFilter(self, filter)
Add a regular expression string to the list.
Definition: text.py:298
def __init__(self)
Constructor.
Definition: text.py:288
Class to modify selected Records in a file of records.
Definition: text.py:144
def editFile(self, filename)
Open and edit the specified file of records.
Definition: text.py:194
def setNew(self, new, field)
Set new string.
Definition: text.py:174
def setIsTest(self, isTest)
Set isTest flag, to decide whether to perform a dry run.
Definition: text.py:186
def __init__(self)
Constructor.
Definition: text.py:149
def setFilter(self, filter, field=0)
Set filter string.
Definition: text.py:161
A Record represents a string of fields separated by whitespace.
Definition: text.py:90
def __init__(self, line)
Constructor.
Definition: text.py:97
def __str__(self)
String representation - line from which Record was constructed.
Definition: text.py:131
Class to wrap line breaks.
Definition: text.py:12
def clear(self)
Clear mutable attributes (column and text).
Definition: text.py:38
def __repr__(self)
Return string containing wrapped text (self.text).
Definition: text.py:70
def __init__(self, eol='\n', nIndent=0)
Constructor.
Definition: text.py:28
def __str__(self)
Return string containing wrapped text (self.text).
Definition: text.py:64
def append(self, string)
Add contents of string to wrapped text.
Definition: text.py:47
Utilities for manipulating files and paths.
Definition: file.py:1
def readLabelledList(file, label)
Read line of form "label = string", in which string may contains spaces.
Definition: text.py:263
def readLabelledLine(file, label)
Read a line of the form "label = string", return string.
Definition: text.py:232