Skip to content

Instantly share code, notes, and snippets.

@nicky-zs
Created July 11, 2014 06:15
Show Gist options
  • Save nicky-zs/6bc90122feb6d490df36 to your computer and use it in GitHub Desktop.
Save nicky-zs/6bc90122feb6d490df36 to your computer and use it in GitHub Desktop.
A simple python template renderer.
#!/usr/bin/python2.7
import sys
import string
_usage = """
Please redirect stdin to the input file, redirect stdout to the output file.
./subtitute.py ARG1=XXX ARG2=YYY < path_to_input > path_to_output\n
"""
def check_input():
if sys.stdin.isatty():
sys.stderr.write(_usage)
exit(-1)
def parse_command_line():
args = {}
for arg in sys.argv:
kv = arg.split('=')
if len(kv) == 2:
args[kv[0]] = kv[1]
return args
def main():
check_input()
args = parse_command_line()
content = sys.stdin.read()
content = string.Template(content)
sys.stdout.write(content.safe_substitute(args))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment