PSCF v1.3
pscfpp.text.FileEditor Class Reference

Class to substitute text in one or more files. More...

Public Member Functions

 __init__ (self)
 Constructor.
 setFilter (self, filter)
 Set the filter string.
 setOld (self, old)
 Set the old string (the string to be replaced)
 setNew (self, new)
 Set the new string (the replacement string)
 setIsTest (self, isTest)
 Set the isTest boolean flag.
 setBlockSize (self, blockSize)
 Set the blockSize attribute.
 editFile (self, filename)
 Edit a single file - edits confined to individual lines.
 editFileBlocks (self, filename)
 Edit a single file - edits may span several lines.
 editFiles (self, dirName, pattern)
 Edit all files in specified directory that matches a pattern.

Detailed Description

Class to substitute text in one or more files.

String attributes:

  • filter : a regex used to filter lines for possible editing
  • old : a regex pattern that should be replaced in lines
  • new : the string that should replace the old string

The editFile method checks each line in a specified file to see if it contains a string that matches the regular expression defined by the filter attribute. If a line matches the filter expression, it search for substrings that match the regular expression defined by the "old" attribute, and replaces each occurence by the string given by the "new" attribute.

The editFiles method applies the editFile method to every file in a specified directory tree with a name that matches a specified filename pattern.

Definition at line 347 of file text.py.

Constructor & Destructor Documentation

◆ __init__()

Member Function Documentation

◆ setFilter()

pscfpp.text.FileEditor.setFilter ( self,
filter )

Set the filter string.

Set the regular expression used to identify lines for possible modification - lines that match the filter are checked for a sub-string that matches the "old" string.

Parameters
filterthe new "filter" string

Definition at line 369 of file text.py.

References filter, pscfpp.text.RecordEditor.filter, hasFilter, pscfpp.text.RecordEditor.hasFilter, hasNew, pscfpp.text.RecordEditor.hasNew, hasOld, isReady, and pscfpp.text.RecordEditor.isReady.

◆ setOld()

pscfpp.text.FileEditor.setOld ( self,
old )

Set the old string (the string to be replaced)

The "old" string is replaced by the "new" string in lines that match the "filter" string.

Parameters
oldthe "old" string, to be replaced

Definition at line 383 of file text.py.

References hasFilter, pscfpp.text.RecordEditor.hasFilter, hasNew, pscfpp.text.RecordEditor.hasNew, hasOld, isReady, pscfpp.text.RecordEditor.isReady, and old.

◆ setNew()

pscfpp.text.FileEditor.setNew ( self,
new )

Set the new string (the replacement string)

Parameters
newthe "new" string that replaces the old string

Definition at line 394 of file text.py.

References hasFilter, pscfpp.text.RecordEditor.hasFilter, hasNew, pscfpp.text.RecordEditor.hasNew, hasOld, isReady, pscfpp.text.RecordEditor.isReady, new, and pscfpp.text.RecordEditor.new.

◆ setIsTest()

pscfpp.text.FileEditor.setIsTest ( self,
isTest )

Set the isTest boolean flag.

If isTest is true, the edit functions only perform a dry run in which they report what changes would be made if isTest were false.

Parameters
isTestperform a dry run (no changes) if true (boolean)

Definition at line 409 of file text.py.

References pscfpp.make.MakeMaker.isTest, isTest, and pscfpp.text.RecordEditor.isTest.

◆ setBlockSize()

pscfpp.text.FileEditor.setBlockSize ( self,
blockSize )

Set the blockSize attribute.

Parameters
blockSizenumber of lines in block for editFileBlock

Definition at line 417 of file text.py.

References blockSize.

◆ editFile()

pscfpp.text.FileEditor.editFile ( self,
filename )

Edit a single file - edits confined to individual lines.

Parameters
filenamename of file to be edited

Definition at line 425 of file text.py.

References filter, pscfpp.text.RecordEditor.filter, isReady, pscfpp.text.RecordEditor.isReady, pscfpp.make.MakeMaker.isTest, isTest, pscfpp.text.RecordEditor.isTest, new, pscfpp.text.RecordEditor.new, and old.

◆ editFileBlocks()

pscfpp.text.FileEditor.editFileBlocks ( self,
filename )

Edit a single file - edits may span several lines.

Parameters
filenamename of file to be edited

Definition at line 457 of file text.py.

References blockSize, filter, pscfpp.text.RecordEditor.filter, isReady, pscfpp.text.RecordEditor.isReady, pscfpp.make.MakeMaker.isTest, isTest, pscfpp.text.RecordEditor.isTest, new, pscfpp.text.RecordEditor.new, and old.

Referenced by editFiles().

◆ editFiles()

pscfpp.text.FileEditor.editFiles ( self,
dirName,
pattern )

Edit all files in specified directory that matches a pattern.

Parameters
dirNamename of directory (string)
patternfilename pattern (string)

Definition at line 498 of file text.py.

References editFileBlocks(), isReady, and pscfpp.text.RecordEditor.isReady.


The documentation for this class was generated from the following file: