diff makeitso/makeitso.py @ 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 d5da38fabdf7
line wrap: on
line diff
--- 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