16     """! Utility class to perform simple operations on files.""" 
   19         """! Initialise with an file pattern. 
   21         @param input_file_pattern Pattern of files to perform actions on. 
   26         """! Append string_append in input_file_name. 
   28         @param string_append String to append. 
   31             with open(input_file_name, 
"ab") 
as f_input:
 
   32                 f_input.write(string_append + 
"\n")
 
   34     def text_replace(self, regex_find, string_replace, count=0, regex_line_match=None):
 
   35         """! Replace regex_find by regex_replace in input_file_name. 
   36         Returns the number of lines where such replacement was made. 
   38         @param regex_find        Regular expression to search for. 
   39         @param string_replace    String to replace by. 
   40         @param count             If non-zero then only replace first count occurences in each line. 
   41         @param regex_line_match  If not 'None' then only do replacement on lines matching this 
   44         c_regex_find = re.compile(regex_find)
 
   46             shutil.move(input_file_name, 
"{}.text_replace_backup".
format(input_file_name))
 
   47             with open(
"{}.text_replace_backup".
format(input_file_name), 
"r") 
as f_input:
 
   48                 with open(input_file_name, 
"w") 
as f_output:
 
   50                         if regex_line_match 
is not None and not re.search(regex_line_match, line):
 
   53                         new_line = c_regex_find.sub(string_replace, line.rstrip(), count)
 
   54                         f_output.write(new_line + 
"\n")
 
   56             os.remove(
"{}.text_replace_backup".
format(input_file_name))
 
   60         """! Replace a whole list of regex_find_replace_count in input_file_name. 
   61         Returns the number of lines where such replacement was made. 
   63         @param regex_find_replace_count    List of regular expressions to search for, strings to replace by, and how often to replace 
   64         @param regex_line_match  If not 'None' then only do replacement on lines matching this 
   67         for i 
in range(len(regex_find_replace_count)):
 
   68             regex_find_replace_count[i][0] = re.compile(regex_find_replace_count[i][0])
 
   70             shutil.move(input_file_name, 
"{}.text_replace_backup".
format(input_file_name))
 
   71             f_input = 
open(
"{}.text_replace_backup".
format(input_file_name), 
"r")
 
   72             f_output = 
open(input_file_name, 
"w")
 
   74                 if regex_line_match 
is not None and not re.search(regex_line_match, line):
 
   77                 new_line = line.rstrip()
 
   78                 for i 
in range(len(regex_find_replace_count)):
 
   79                     if regex_find_replace_count[i][2] != 0 
and regex_find_replace_count[i][0].
match(new_line):
 
   80                         new_line = regex_find_replace_count[i][0].sub(regex_find_replace_count[i][1], new_line)
 
   81                         regex_find_replace_count[i][2] -= 1
 
   83                 f_output.write(new_line + 
"\n")
 
   84             os.remove(
"{}.text_replace_backup".
format(input_file_name))
 
   88         """! Remove lines matching regex_find in input_file_name. 
   89         Returns the number of removed lines. 
   91         @param regex_find Regular expression to search for. 
   94         c_regex_find = re.compile(regex_find)
 
   97             shutil.move(input_file_name, 
"{}.text_replace_backup".
format(input_file_name))
 
   98             with open(
"{}.text_replace_backup".
format(input_file_name), 
"r") 
as f_input:
 
   99                 with open(input_file_name, 
"w") 
as f_output:
 
  101                         if c_regex_find.search(line.rstrip()) 
is None:
 
  105             os.remove(
"{}.text_replace_backup".
format(input_file_name))
 
  109         """! Remove all lines matching regex_find in input_file_name 
  110         if at least one also maches pattern_to_match. 
  111         Returns the number of removed lines. 
  113         @param regex_find Regular expression to search for in the lines to be considered. 
  114         @param pattern_to_match Regular expression to search for in those lines. 
  119             with open(input_file_name, 
"r") 
as f_input:
 
  121                     if re.search(regex_find, line.rstrip()) 
is not None and re.search(pattern_to_match, line.rstrip()) 
is not None:
 
  129                 shutil.move(input_file_name, 
"{}.text_replace_backup".
format(input_file_name))
 
  130                 with open(
"{}.text_replace_backup".
format(input_file_name), 
"r") 
as f_input:
 
  131                     with open(input_file_name, 
"w") 
as f_output:
 
  133                             if re.search(regex_find, line.rstrip()) 
is None:
 
  137                 os.remove(
"{}.text_replace_backup".
format(input_file_name))