OLD | NEW |
| (Empty) |
1 #!/usr/bin/python | |
2 | |
3 # Copyright 2014 Google Inc. | |
4 # | |
5 # Use of this source code is governed by a BSD-style license that can be | |
6 # found in the LICENSE file. | |
7 | |
8 # A simple template processing script. | |
9 | |
10 import optparse | |
11 import os | |
12 import sys | |
13 | |
14 parser = optparse.OptionParser() | |
15 parser.add_option('-i', '--input') | |
16 parser.add_option('-o', '--output') | |
17 parser.add_option( | |
18 '-k', '--keyword_substitution', action='append', nargs=2, | |
19 metavar=('KEY', 'VALUE'), help='Changes KEY to VALUE in the template.') | |
20 parser.add_option( | |
21 '-p', '--path_substitution', action='append', nargs=2, | |
22 metavar=('KEY', 'PATH'), | |
23 help='Makes PATH absolute then changes KEY to PATH in the template.') | |
24 | |
25 (args, _) = parser.parse_args() | |
26 | |
27 input = sys.stdin | |
28 if args.input: | |
29 input = open(args.input, 'r') | |
30 | |
31 output = sys.stdout | |
32 if args.output: | |
33 output = open(args.output, 'w') | |
34 | |
35 path_subs = None | |
36 if args.path_substitution: | |
37 path_subs = [ | |
38 [sub[0], os.path.abspath(sub[1])] for sub in args.path_substitution | |
39 ] | |
40 | |
41 for line in input: | |
42 if args.keyword_substitution: | |
43 for (key, value) in args.keyword_substitution: | |
44 line = line.replace(key, value) | |
45 if path_subs: | |
46 for (key, path) in path_subs: | |
47 line = line.replace(key, path) | |
48 output.write(line) | |
49 | |
50 input.close() | |
51 output.close() | |
OLD | NEW |