OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
epoger
2014/01/08 19:40:22
Add copyright and documentation header?
| |
2 | |
3 import argparse | |
4 import os | |
5 import sys | |
6 | |
7 parser = argparse.ArgumentParser() | |
8 parser.add_argument("-i", "--input") | |
epoger
2014/01/08 19:40:22
Please be consistent with either single- or double
vandebo (ex-Chrome)
2014/01/08 21:24:15
Done.
| |
9 parser.add_argument("-o", "--output") | |
10 parser.add_argument( | |
epoger
2014/01/08 19:40:22
I would suggest naming this arg --keyword_substitu
vandebo (ex-Chrome)
2014/01/08 21:24:15
Done.
| |
11 "-s", "--substitution", action="append", nargs=2, metavar=('KEY', 'VALUE'), help="Changes @KEY@ to VALUE in the template.") | |
bungeman-skia
2014/01/07 21:13:29
nit: this line is a bit long, looks like the next
vandebo (ex-Chrome)
2014/01/08 21:24:15
Done.
| |
12 parser.add_argument( | |
13 "-p", "--path_substitution", action="append", nargs=2, | |
14 metavar=('KEY', 'PATH'), | |
15 help="Makes PATH absolute then changes @KEY@ to PATH in the template.") | |
16 | |
17 args = parser.parse_args() | |
18 | |
19 input = sys.stdin | |
20 if args.input: | |
21 try: | |
22 input = open(args.input, 'r') | |
23 except IOError: | |
epoger
2014/01/08 19:40:22
Why the manual try/except handling? If you just d
vandebo (ex-Chrome)
2014/01/08 21:24:15
Done.
| |
24 sys.exit(1) | |
25 | |
26 output = sys.stdout | |
27 if args.output: | |
28 try: | |
29 output = open(args.output, 'w') | |
30 except IOError: | |
31 sys.exit(1) | |
32 | |
33 path_subs = None | |
34 if args.path_substitution: | |
35 path_subs = [ | |
36 [sub[0], os.path.abspath(sub[1])] for sub in args.path_substitution | |
37 ] | |
38 | |
39 for line in input: | |
40 if args.substitution: | |
41 for (key, value) in args.substitution: | |
42 line = line.replace('@' + key + '@', value) | |
epoger
2014/01/08 19:40:22
Rather than mandating the @ delimiters, why not ju
vandebo (ex-Chrome)
2014/01/08 21:24:15
Done.
| |
43 if path_subs: | |
44 for (key, path) in path_subs: | |
45 line = line.replace('@' + key + '@', path) | |
46 output.write(line) | |
47 | |
48 input.close() | |
49 output.close() | |
OLD | NEW |