diff makeitso/file2template.py @ 181:7ac8571788e2

add script to make templates from files
author Jeff Hammel <k0scist@gmail.com>
date Sun, 23 Mar 2014 11:52:47 -0700
parents
children 215a71ac0eff
line wrap: on
line diff
--- /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()