-
Notifications
You must be signed in to change notification settings - Fork 0
/
substitute_file.py
43 lines (28 loc) · 1.11 KB
/
substitute_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
# Runs template substitutions over a file.
import argparse
import string
class CustomTemplate(string.Template):
delimiter = '@'
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--source')
parser.add_argument('--dest')
parser.add_argument('--var', nargs=2, action='append', default=[], dest='vars')
parser.add_argument('--file-var', nargs=2, action='append', default=[], dest='file_vars')
args = parser.parse_args()
substitutions = {}
for var, value in args.vars:
substitutions[var] = value
for var, path in args.file_vars:
with open(path) as fp:
substitutions[var] = fp.read()
with open(args.source) as source, open(args.dest, 'w') as dest:
for line in source:
if line.strip():
line = CustomTemplate(line).substitute(substitutions)
dest.write(line)
if __name__ == '__main__':
main()