4 """ A simple replacement tool that honors some C/C++ syntax when replacing.
6 This will take a particular phrase given by the user and find it in a set of
7 documents. That phrase will be replaced when it appears completely, and is not
8 in a C or C++ style comment (// or /* ... */). It also must be clear of any
9 other alphanumeric pollution, and only be surrounded by white space or operation
14 """ Initializes the class with a set of arguments to work with.
16 The arguments need to be in the form described by print_instructions().
26 """ Shows the instructions for using this class. """
28 This script will replace all occurrences of a phrase you specify in a set of files. The
29 replacement process will be careful about C and C++ syntax and will not replace occurrences
30 within comments or which are not "complete" phrases (due to other alpha-numeric characters
31 that abut the phrase). The arguments to the script are:
33 {0}: PhraseToReplace ReplacementPhrase File1 [File2 ...]
35 For example, if the phrase to replace is Goop, it will be replaced in these contexts:
39 but it will not be found in these contexts:
46 """ Performs command line argument handling. """
60 """ loads the file into our memory buffer for processing. """
62 our_file =
open(filename,
"rb")
64 file_buffer = our_file.read()
66 print(
"There was an error reading the file {0}".format(filename))
71 print(
"There was an error opening the file {0}".format(filename))
77 """ takes the processed buffer and sends it back out to the filename. """
79 output_filename = filename
81 our_file =
open(output_filename,
"wb")
85 print(
"There was an error writing the file {0}".format(output_filename))
90 print(
"There was an error opening the file {0}".format(output_filename))
95 """ given a character, this returns true if it's between a-z, A-Z or 0-9. """
96 if (check_char[0] ==
"_"):
98 if ( (check_char[0] <=
"z")
and (check_char[0] >=
"a")):
100 if ( (check_char[0] <=
"Z")
and (check_char[0] >=
"A")):
102 if ( (check_char[0] <=
"9")
and (check_char[0] >=
"0")):
107 """ given a string to fix, this replaces all appropriate locations of the phrase. """
110 while (indy < len(fix_string)):
120 char_before = fix_string[indy-1]
134 """ handle emission of a chunk of normal code (without comments). """
142 """ emits the piled up text for comments found in the code. """
147 """ iterates through the stored version of the file and replaces the phrase. """
162 if ((len(next_line) > 0)
and (self.
statestate == self.
EATING_NORMAL_TEXTEATING_NORMAL_TEXT)
and (
'/' in next_line)):
164 while (indy < len(next_line)):
166 indy = next_line.find(
'/', indy)
169 if ((len(next_line) > indy + 1)
and (next_line[indy + 1] ==
'/')):
172 next_line = next_line[indy:]
177 if ((len(next_line) > indy + 1)
and (next_line[indy + 1] ==
'*')):
180 next_line = next_line[indy:]
203 if (
"*/" in next_line):
209 print(
"file seems to have unclosed multi-line comment.")
216 """ Orchestrates the process of replacing the phrases. """
219 if (try_command_line !=
True):
220 print(
"failed to process the command line...\n")
224 for i
in range(0, len(self.
filesfiles)):
225 print(
"file {0} is \'{1}\'".format(i, self.
filesfiles[i]))
227 if (worked
is False):
228 print(
"skipping since file read failed on: {0}".format(self.
filesfiles[i]))
232 if (worked
is False):
233 print(
"skipping, since processing failed on: {0}".format(self.
filesfiles[i]))
236 if (worked
is False):
237 print(
"writing file back failed on: {0}".format(self.
filesfiles[i]))
238 print(
"finished processing all files.")
241 if __name__ ==
"__main__":
244 slicer.replace_all_occurrences()
def is_alphanumeric(self, check_char)
def replace_all_occurrences(self)
def emit_comment_accumulator(self)
def emit_normal_accumulator(self)
def read_file_data(self, filename)
def print_instructions(self)
def validate_and_consume_command_line(self)
def write_file_data(self, filename)
def replace_within_string(self, fix_string)
def process_file_data(self)