changeset 90:26b9c3bba04e

make the api for substitute() variables, output
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 10 Jan 2011 21:33:03 -0800
parents e055447376ab
children 672d2d3ee322
files examples/doctest.txt makeitso/makeitso.py makeitso/template.py
diffstat 3 files changed, 27 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/examples/doctest.txt	Mon Jan 10 19:58:21 2011 -0800
+++ b/examples/doctest.txt	Mon Jan 10 21:33:03 2011 -0800
@@ -17,14 +17,14 @@
     >>> template = makeitso.PolyTemplate([example], interactive=False)
     >>> template.missing()
     set(['name'])
-    >>> template.substitute(output=None, name='foo')
+    >>> template.substitute(dict(name='foo'))
     Hello foo
     
 Substitute to a buffer::
 
     >>> buffer = StringIO()
     >>> template = makeitso.PolyTemplate([example], interactive=False)
-    >>> template.substitute(output=buffer, name='bar')
+    >>> template.substitute(dict(name='bar'), output=buffer)
     >>> buffer.getvalue().strip()
     'Hello bar'
 
@@ -32,7 +32,7 @@
 
     >>> buffer = tempfile.mktemp()
     >>> template = makeitso.PolyTemplate([example], interactive=False)
-    >>> template.substitute(buffer, name='fleem')
+    >>> template.substitute(dict(name='fleem'), buffer)
     >>> file(buffer).read().strip()
     'Hello fleem'
     >>> os.remove(buffer)
@@ -44,7 +44,7 @@
     >>> template = makeitso.PolyTemplate([exampledir], interactive=False)
     >>> sorted(template.missing())
     ['bar', 'foo', 'subdir']
-    >>> template.substitute(tempdir, foo='It', bar='life', subdir='mysubdir')
+    >>> template.substitute(dict(foo='It', bar='life', subdir='mysubdir'), output=tempdir)
     >>> sorted(os.listdir(tempdir))
     ['foo.txt', 'mysubdir']
     >>> file(os.path.join(tempdir, 'foo.txt')).read().strip()
@@ -66,7 +66,7 @@
 
     >>> variables = dict([(i, i.title()) for i in variables])
     >>> try:
-    ...    template.substitute(output=None, **variables)
+    ...    template.substitute(variables)
     ... except AssertionError, e:
     ...    pass
     >>> e
@@ -75,7 +75,7 @@
 Provide an output::
 
     >>> template = makeitso.PolyTemplate([example, exampledir], interactive=False)
-    >>> template.substitute(tempdir, **variables)
+    >>> template.substitute(variables, tempdir)
     >>> sorted(os.listdir(tempdir))
     ['Subdir', 'example.txt', 'foo.txt']
     >>> shutil.rmtree(tempdir)
@@ -89,7 +89,7 @@
     ...    vars = [Variable(name='name', default='bar')]
     >>> buffer = tempfile.mktemp()
     >>> apitemplate = MyTemplate(interactive=False)
-    >>> apitemplate.substitute(buffer)
+    >>> apitemplate.substitute({}, buffer)
     >>> file(buffer).read().strip()
     'Hello bar'
     >>> os.remove(buffer)
@@ -102,7 +102,7 @@
     >>> f.close()
     >>> os.chmod(buffer, 0755)
     >>> uritemplate = makeitso.URITemplate(example, interactive=False)
-    >>> uritemplate.substitute(buffer, name='bar')
+    >>> uritemplate.substitute(dict(name='bar'), buffer)
     >>> ('%o' % os.stat(buffer).st_mode).endswith('755')
     True
     >>> os.remove(buffer)
--- a/makeitso/makeitso.py	Mon Jan 10 19:58:21 2011 -0800
+++ b/makeitso/makeitso.py	Mon Jan 10 21:33:03 2011 -0800
@@ -175,10 +175,7 @@
         """interactive (for now) substitution"""
         vars = self.get_variables(**variables)
         self.check_missing(vars)
-        return self._substitute(**vars)
-
-    def _substitute(self, **variables):
-        return tempita.Template.substitute(self, **variables)
+        return tempita.Template.substitute(self, **vars)
 
     def read_variables(self, variables):
         """read variables from stdin"""
@@ -210,10 +207,11 @@
                                  interactive=interactive,
                                  variables=variables)
 
-    def substitute(self, output=None, **variables):
+    def substitute(self, variables, output=None):
         content = ContentTemplate.substitute(self, **variables)
+
+        # write output
         output = output or sys.stdout
-
         if isinstance(output, basestring):
             path = output
             if os.path.isdir(output):
@@ -265,6 +263,10 @@
 
             # find variables from files
             for f in filenames:
+                missed = ContentTemplate(d).missing(**vars)
+                missing.update(missed)
+                variables.update(dict([(i, '') for i in missed]))
+                
                 path = os.path.join(dirpath, f)
                 template = URITemplate(path, interactive=self.interactive)
                 missed = template.missing(**vars)
@@ -273,7 +275,7 @@
 
         return missing
 
-    def substitute(self, output, **variables):
+    def substitute(self, variables, output):
         self.check_output(output)
         vars = self.get_variables(**variables)
         self.check_missing(vars)
@@ -302,16 +304,18 @@
 
             # interpolate files
             for filename in filenames:
+
+                # interpolate filenames
                 path = os.path.join(dirname, filename)
                 interpolated = ContentTemplate(path).substitute(**variables)
                 target = os.path.join(output, interpolated.split(self.name, 1)[-1].strip(os.path.sep))
-                
+
+                # interpolate their contents
                 if os.path.exists(target):
                     # ensure its a directory
-                    # TODO: check this first before interpolation is in progress
                     assert os.path.isfile(target), "Can't substitute a file on top of a directory"
                 template = URITemplate(path, interactive=False)
-                template.substitute(target, **variables)
+                template.substitute(variables, target)
 
 
 class PolyTemplate(ContentTemplate):
@@ -345,7 +349,7 @@
             if hasattr(template, 'check_output'):
                 template.check_output(output)
 
-    def substitute(self, output=None, **variables):
+    def substitute(self, variables, output=None):
 
         # determine where the hell to put these things
         self.check_output(output)
@@ -360,7 +364,7 @@
 
         # do the substitution
         for template in self.templates:
-            template.substitute(output, **vars)
+            template.substitute(vars, output)
         
 ### command line interface
 
--- a/makeitso/template.py	Mon Jan 10 19:58:21 2011 -0800
+++ b/makeitso/template.py	Mon Jan 10 21:33:03 2011 -0800
@@ -85,8 +85,7 @@
     # inspect the templates for more variables
     look = False
 
-    def __init__(self, output=None, interactive=True, usedefaults=True,
-                 variables=None):
+    def __init__(self, interactive=True, usedefaults=True, variables=None):
         """
         - output : output file or directory
         - interactive : whether tointeractively get variables
@@ -95,7 +94,6 @@
 
         # boilerplate
         variables = variables or {}
-        self.output = output
         if not self.description and hasattr(self, '__doc__'):
             self.description = self.__doc__
         self.interactive = interactive
@@ -151,7 +149,7 @@
     def pre(self, variables):
         """do stuff before interpolation"""
 
-    def substitute(self, output, **variables):
+    def substitute(self, variables, output):
         """do the substitution"""
         
         vars = self.get_variables(**variables)
@@ -163,7 +161,7 @@
                                 interactive=self.interactive,
                                 variables=vars)
         template.check_output(output)
-        template.substitute(output)
+        template.substitute({}, output)
                      
         self.post(vars)