Release 2.23.4
[atk.git] / win32 / replace.py
bloba81bab942601deeb7a21bdc1926f46c7d80c2a7f
1 #!/usr/bin/python
3 # Simple utility script to manipulate
4 # certain types of strings in a file
6 # This can be used in various projects where
7 # there is the need to replace strings in files,
8 # and is copied from GLib's $(srcroot)/build/win32
10 # Author: Fan, Chun-wei
11 # Date: September 03, 2014
13 import os
14 import sys
15 import re
16 import string
17 import argparse
19 valid_actions = ['remove-prefix',
20 'replace-var',
21 'replace-str',
22 'remove-str']
24 def replace_multi(src, dest, replace_items):
25 with open(src, 'r') as s:
26 with open(dest, 'w') as d:
27 for line in s:
28 replace_dict = dict((re.escape(key), value) \
29 for key, value in replace_items.items())
30 replace_pattern = re.compile("|".join(replace_dict.keys()))
31 d.write(replace_pattern.sub(lambda m: \
32 replace_dict[re.escape(m.group(0))], line))
34 def replace(src, dest, instring, outstring):
35 replace_item = {instring: outstring}
36 replace_multi(src, dest, replace_item)
38 def check_required_args(args, params):
39 for param in params:
40 if getattr(args, param, None) is None:
41 raise SystemExit('%s: error: --%s argument is required' % (__file__, param))
43 def warn_ignored_args(args, params):
44 for param in params:
45 if getattr(args, param, None) is not None:
46 print('%s: warning: --%s argument is ignored' % (__file__, param))
48 def main(argv):
50 parser = argparse.ArgumentParser(description='Process strings in a file.')
51 parser.add_argument('-a',
52 '--action',
53 help='Action to carry out. Can be one of:\n'
54 'remove-prefix\n'
55 'replace-var\n'
56 'replace-str\n'
57 'remove-str',
58 choices=valid_actions)
59 parser.add_argument('-i', '--input', help='Input file')
60 parser.add_argument('-o', '--output', help='Output file')
61 parser.add_argument('--instring', help='String to replace or remove')
62 parser.add_argument('--var', help='Autotools variable name to replace')
63 parser.add_argument('--outstring',
64 help='New String to replace specified string or variable')
65 parser.add_argument('--removeprefix', help='Prefix of string to remove')
67 args = parser.parse_args()
69 input_string = ''
70 output_string = ''
72 # We must have action, input, output for all operations
73 check_required_args(args, ['action','input','output'])
75 # Build the arguments by the operation that is to be done,
76 # to be fed into replace()
78 # Get rid of prefixes from a string
79 if args.action == 'remove-prefix':
80 check_required_args(args, ['instring','removeprefix'])
81 warn_ignored_args(args, ['outstring','var'])
82 input_string = args.removeprefix + args.instring
83 output_string = args.instring
85 # Replace an m4-style variable (those surrounded by @...@)
86 if args.action == 'replace-var':
87 check_required_args(args, ['var','outstring'])
88 warn_ignored_args(args, ['instring','removeprefix'])
89 input_string = '@' + args.var + '@'
90 output_string = args.outstring
92 # Replace a string
93 if args.action == 'replace-str':
94 check_required_args(args, ['instring','outstring'])
95 warn_ignored_args(args, ['var','removeprefix'])
96 input_string = args.instring
97 output_string = args.outstring
99 # Remove a string
100 if args.action == 'remove-str':
101 check_required_args(args, ['instring'])
102 warn_ignored_args(args, ['var','outstring','removeprefix'])
103 input_string = args.instring
104 output_string = ''
106 replace(args.input, args.output, input_string, output_string)
108 if __name__ == '__main__':
109 sys.exit(main(sys.argv))