changeset 183:55b34a5a2e5f

basic form now works
author Jeff Hammel <k0scist@gmail.com>
date Sun, 23 Mar 2014 16:31:37 -0700
parents 215a71ac0eff
children be33152d7d0c
files makeitso/file2template.py setup.py
diffstat 2 files changed, 28 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/makeitso/file2template.py	Sun Mar 23 11:53:11 2014 -0700
+++ b/makeitso/file2template.py	Sun Mar 23 16:31:37 2014 -0700
@@ -6,10 +6,9 @@
 """
 # TODO: shell script extension
 
+import argparse
 import os
-import subprocess
 import sys
-from argparse import ArgumentParser
 
 here = os.path.dirname(os.path.realpath(__file__))
 string = (str, unicode)
@@ -23,11 +22,9 @@
 import argparse
 import sys
 
-variables = {
-{variables}
-}
+variables = dict({variables})
 
-template = """{content}"""
+template = """{template}"""
 
 def main(args=sys.argv[1:]):
     """CLI"""
@@ -43,25 +40,44 @@
 '''
 
 
-class File2TemplateParser(ArgumentParser):
+class File2TemplateParser(argparse.ArgumentParser):
     """argument parser for `%(prog)s`"""
 
     def __init__(self):
-        ArgumentParser.__init__(self, description=__doc__)
-        parser.add_argument('input', nargs='?',
+        argparse.ArgumentParser.__init__(self, description=__doc__)
+        self.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='*',
+        self.add_argument('variables', nargs='*',
                             help="variables to use")
-        parser.add_argument('-o', '--output', dest='output',
+        self.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:]):
+    """CLI"""
 
+    # parse command line
     parser = File2TemplateParser()
     options = parser.parse_args(args)
 
+    # get variable values
+    lines = []
+    for v in options.variables:
+        if '=' in v:
+            key, value = v.split('=', 1)
+        else:
+            key = v
+            value = 'None'
+        lines.append('{}: {},'.format(repr(key), value))
+    varstring = '\n'.join(lines)
+
+    # read the content
+
+    # interpolate the template
+    output = template.format(variables=varstring, template='')
+    options.output.write(output)
+
 if __name__ == '__main__':
     main()
--- a/setup.py	Sun Mar 23 11:53:11 2014 -0700
+++ b/setup.py	Sun Mar 23 16:31:37 2014 -0700
@@ -34,6 +34,7 @@
       make-python-package = makeitso.python:main
       mkpydir = makeitso.mkpydir:main
       script2package = makeitso.script2package:main
+      file2template = makeitso.file2template:main
 
       [makeitso.templates]
       python-package = makeitso.python:PythonPackageTemplate