summaryrefslogtreecommitdiff
path: root/win32/replace.py
diff options
context:
space:
mode:
authorSeonah Moon <seonah1.moon@samsung.com>2019-12-09 14:52:00 +0900
committerSeonah Moon <seonah1.moon@samsung.com>2019-12-09 14:52:24 +0900
commit58894334cd3f0b89c865304e8e9d6eea3cd20a55 (patch)
tree6fdb22df40c774e8db791bc7cd27b39680a12fb0 /win32/replace.py
parent9f52ffa8b526e717e324fe0835ab794478650f11 (diff)
downloadlibsoup-58894334cd3f0b89c865304e8e9d6eea3cd20a55.tar.gz
libsoup-58894334cd3f0b89c865304e8e9d6eea3cd20a55.tar.bz2
libsoup-58894334cd3f0b89c865304e8e9d6eea3cd20a55.zip
Imported Upstream version 2.62.2upstream/2.62.2
Change-Id: Id151dafaa6ac9f569d76f08282b5c9e4c3b19621
Diffstat (limited to 'win32/replace.py')
-rw-r--r--win32/replace.py115
1 files changed, 115 insertions, 0 deletions
diff --git a/win32/replace.py b/win32/replace.py
new file mode 100644
index 00000000..3aeceb1f
--- /dev/null
+++ b/win32/replace.py
@@ -0,0 +1,115 @@
+#!/usr/bin/python
+#
+# 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)/win32
+
+# Author: Fan, Chun-wei
+# Date: September 03, 2014
+
+import os
+import sys
+import re
+import string
+import argparse
+
+valid_actions = ['remove-prefix',
+ 'replace-var',
+ 'replace-str',
+ 'remove-str']
+
+def open_file(filename, mode):
+ if sys.version_info[0] < 3:
+ return open(filename, mode=mode)
+ else:
+ return open(filename, mode=mode, encoding='utf-8')
+
+def replace_multi(src, dest, replace_items):
+ with open_file(src, 'r') as s:
+ with open_file(dest, 'w') as d:
+ for line in s:
+ 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:
+ if getattr(args, param, None) is None:
+ raise SystemExit('%s: error: --%s argument is required' % (__file__, param))
+
+def warn_ignored_args(args, params):
+ for param in params:
+ if getattr(args, param, None) is not None:
+ print('%s: warning: --%s argument is ignored' % (__file__, param))
+
+def main(argv):
+
+ parser = argparse.ArgumentParser(description='Process strings in a file.')
+ parser.add_argument('-a',
+ '--action',
+ help='Action to carry out. Can be one of:\n'
+ 'remove-prefix\n'
+ 'replace-var\n'
+ 'replace-str\n'
+ 'remove-str',
+ choices=valid_actions)
+ parser.add_argument('-i', '--input', help='Input file')
+ parser.add_argument('-o', '--output', help='Output file')
+ parser.add_argument('--instring', help='String to replace or remove')
+ parser.add_argument('--var', help='Autotools variable name to replace')
+ parser.add_argument('--outstring',
+ help='New String to replace specified string or variable')
+ parser.add_argument('--removeprefix', help='Prefix of string to remove')
+
+ args = parser.parse_args()
+
+ input_string = ''
+ output_string = ''
+
+ # We must have action, input, output for all operations
+ check_required_args(args, ['action','input','output'])
+
+ # Build the arguments by the operation that is to be done,
+ # to be fed into replace()
+
+ # Get rid of prefixes from a string
+ if args.action == 'remove-prefix':
+ check_required_args(args, ['instring','removeprefix'])
+ warn_ignored_args(args, ['outstring','var'])
+ input_string = args.removeprefix + args.instring
+ output_string = args.instring
+
+ # Replace an m4-style variable (those surrounded by @...@)
+ if args.action == 'replace-var':
+ check_required_args(args, ['var','outstring'])
+ warn_ignored_args(args, ['instring','removeprefix'])
+ input_string = '@' + args.var + '@'
+ output_string = args.outstring
+
+ # Replace a string
+ if args.action == 'replace-str':
+ check_required_args(args, ['instring','outstring'])
+ warn_ignored_args(args, ['var','removeprefix'])
+ input_string = args.instring
+ output_string = args.outstring
+
+ # Remove a string
+ if args.action == 'remove-str':
+ check_required_args(args, ['instring'])
+ warn_ignored_args(args, ['var','outstring','removeprefix'])
+ input_string = args.instring
+ output_string = ''
+
+ replace(args.input, args.output, input_string, output_string)
+
+if __name__ == '__main__':
+ sys.exit(main(sys.argv))