diff options
-rw-r--r-- | win32/replace.py | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/win32/replace.py b/win32/replace.py index ea75dafa5..a81bab942 100644 --- a/win32/replace.py +++ b/win32/replace.py @@ -2,7 +2,11 @@ # # Simple utility script to manipulate # certain types of strings in a file -# + +# This can be used in various projects where +# there is the need to replace strings in files, +# and is copied from GLib's $(srcroot)/build/win32 + # Author: Fan, Chun-wei # Date: September 03, 2014 @@ -17,12 +21,19 @@ valid_actions = ['remove-prefix', 'replace-str', 'remove-str'] -def replace(src, dest, instring, outstring): +def replace_multi(src, dest, replace_items): with open(src, 'r') as s: with open(dest, 'w') as d: for line in s: - i = line.replace(instring, outstring) - d.write(i) + replace_dict = dict((re.escape(key), value) \ + for key, value in replace_items.items()) + replace_pattern = re.compile("|".join(replace_dict.keys())) + d.write(replace_pattern.sub(lambda m: \ + replace_dict[re.escape(m.group(0))], line)) + +def replace(src, dest, instring, outstring): + replace_item = {instring: outstring} + replace_multi(src, dest, replace_item) def check_required_args(args, params): for param in params: |