ATLAS Offline Software
Loading...
Searching...
No Matches
file_parser.py
Go to the documentation of this file.
1# Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
2
3# @PowhegControl FileParser
4# PowhegControl Text alteration operations for files
5#
6# Authors: James Robinson <james.robinson@cern.ch>
7
8#! /usr/bin/env python
9import glob
10import os
11import re
12import shutil
13
14
16 """! Utility class to perform simple operations on files."""
17
18 def __init__(self, input_file_pattern):
19 """! Initialise with an file pattern.
20
21 @param input_file_pattern Pattern of files to perform actions on.
22 """
23 self.__input_file_names = glob.glob(input_file_pattern)
24
25 def text_append(self, string_append):
26 """! Append string_append in input_file_name.
27
28 @param string_append String to append.
29 """
30 for input_file_name in self.__input_file_names:
31 with open(input_file_name, "ab") as f_input:
32 f_input.write(string_append + "\n")
33
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.
37
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
42 """
43 replaced_lines = 0
44 c_regex_find = re.compile(regex_find)
45 for input_file_name in self.__input_file_names:
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:
49 for line in f_input:
50 if regex_line_match is not None and not re.search(regex_line_match, line):
51 f_output.write(line)
52 continue
53 new_line = c_regex_find.sub(string_replace, line.rstrip(), count)
54 f_output.write(new_line + "\n")
55 replaced_lines += 1
56 os.remove("{}.text_replace_backup".format(input_file_name))
57 return replaced_lines
58
59 def text_replace_multi(self, regex_find_replace_count, regex_line_match=None):
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.
62
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
65 """
66 replaced_lines = 0
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])
69 for input_file_name in self.__input_file_names:
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")
73 for line in f_input:
74 if regex_line_match is not None and not re.search(regex_line_match, line):
75 f_output.write(line)
76 continue
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
82 replaced_lines += 1
83 f_output.write(new_line + "\n")
84 os.remove("{}.text_replace_backup".format(input_file_name))
85 return replaced_lines
86
87 def text_remove(self, regex_find):
88 """! Remove lines matching regex_find in input_file_name.
89 Returns the number of removed lines.
90
91 @param regex_find Regular expression to search for.
92 """
93 removed_lines = 0
94 c_regex_find = re.compile(regex_find)
95
96 for input_file_name in self.__input_file_names:
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:
100 for line in f_input:
101 if c_regex_find.search(line.rstrip()) is None:
102 f_output.write(line)
103 else:
104 removed_lines += 1
105 os.remove("{}.text_replace_backup".format(input_file_name))
106 return removed_lines
107
108 def text_remove_all_if_one_line_matches(self, regex_find, pattern_to_match):
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.
112
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.
115 """
116 remove = False
117 # first check if at least one such lines matches pattern_to_match
118 for input_file_name in self.__input_file_names:
119 with open(input_file_name, "r") as f_input:
120 for line in f_input:
121 if re.search(regex_find, line.rstrip()) is not None and re.search(pattern_to_match, line.rstrip()) is not None:
122 remove = True
123 break # no need to continue, one instance is sufficient
124
125 removed_lines = 0
126 # then if that's the case, remove all such lines
127 if remove:
128 for input_file_name in self.__input_file_names:
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:
132 for line in f_input:
133 if re.search(regex_find, line.rstrip()) is None:
134 f_output.write(line)
135 else:
136 removed_lines += 1
137 os.remove("{}.text_replace_backup".format(input_file_name))
138 return removed_lines
Utility class to perform simple operations on files.
text_replace(self, regex_find, string_replace, count=0, regex_line_match=None)
Replace regex_find by regex_replace in input_file_name.
__init__(self, input_file_pattern)
Initialise with an file pattern.
text_append(self, string_append)
Append string_append in input_file_name.
text_remove_all_if_one_line_matches(self, regex_find, pattern_to_match)
Remove all lines matching regex_find in input_file_name if at least one also maches pattern_to_match.
text_replace_multi(self, regex_find_replace_count, regex_line_match=None)
Replace a whole list of regex_find_replace_count in input_file_name.
text_remove(self, regex_find)
Remove lines matching regex_find in input_file_name.
bool match(std::string s1, std::string s2)
match the individual directories of two strings
Definition hcg.cxx:357