[gsettings-desktop-schemas] Visual Studio builds: Sync replace.py with the one in GLib



commit 0fdfae52f4322e7c543c897e8bf760802eacf5bf
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Thu May 12 17:59:17 2016 +0800

    Visual Studio builds: Sync replace.py with the one in GLib
    
    This is also done to make some things cleaner, as well as preparing
    for the generation of the .pc files.

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