[adwaita-icon-theme] win32/replace.py: Sync with GLib master



commit cc72a258e683bfc98d557d6f1fb55e82febf8fae
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Mon Mar 13 15:28:13 2017 +0800

    win32/replace.py: Sync with GLib master
    
    This enables us to generate the .pc files upon 'nmake install'.

 win32/replace.py |   19 +++++++++++++++----
 1 files changed, 15 insertions(+), 4 deletions(-)
---
diff --git a/win32/replace.py b/win32/replace.py
index ea75daf..a81bab9 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:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]