# HG changeset patch # User Jeff Hammel # Date 1395600767 25200 # Node ID 7ac8571788e2a7622a3a024d8ec06368ca70bb60 # Parent 9b78f52afe4e923f52580e8944a81cba56a2d432 add script to make templates from files diff -r 9b78f52afe4e -r 7ac8571788e2 makeitso/file2template.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/makeitso/file2template.py Sun Mar 23 11:52:47 2014 -0700 @@ -0,0 +1,67 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +convert a file to a template +""" + + +import os +import subprocess +import sys +from argparse import ArgumentParser + +here = os.path.dirname(os.path.realpath(__file__)) +string = (str, unicode) + +template = '''#!/usr/bin/env python + +""" +template +""" + +import argparse +import sys + +variables = { +{variables} +} + +template = """{content}""" + +def main(args=sys.argv[1:]): + """CLI""" + + # parse command line + parser = argparse.ArgumentParser(description=__doc__) + for variable in variables: + pass + options = parser.parse_args(args) + +if __name__ == '__main__': + main() +''' + + +class File2TemplateParser(ArgumentParser): + """argument parser for `%(prog)s`""" + + def __init__(self): + ArgumentParser.__init__(self, description=__doc__) + parser.add_argument('input', nargs='?', + type=argparse.FileType('r'), default=sys.stdin, + help='input file, or read from stdin if ommitted') + parser.add_argument('variables', nargs='*', + help="variables to use") + parser.add_argument('-o', '--output', dest='output', + type=argparse.FileType('r'), default=sys.stdout, + help="output file, or stdout if ommitted'") + + +def main(args=sys.argv[1:]): + + parser = File2TemplateParser() + options = parser.parse_args(args) + +if __name__ == '__main__': + main()