diff makeitso/template.py @ 44:6e08cca7d656

do API variable reading and stubbing a bit for control flow
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 04 Jan 2011 18:07:18 -0800
parents 73dac34d2692
children 728cae02a6ed
line wrap: on
line diff
--- a/makeitso/template.py	Tue Jan 04 17:40:21 2011 -0800
+++ b/makeitso/template.py	Tue Jan 04 18:07:18 2011 -0800
@@ -7,10 +7,14 @@
 from makeitso import ContentTemplate
 from makeitso import PolyTemplate
 
+class Undefined(object):
+    """marker class for variables"""
+Undefined = Undefined() # singleton
+
 class Variable(object):
     """variable object for MakeItSo templates"""
     
-    def __init__(self, name, default=None, description=None,
+    def __init__(self, name, default=Undefined, description=None,
                  cast=None):
         self.name = name
         self.default = default
@@ -56,13 +60,24 @@
     # inspect the templates for more variables
     look = False
 
-    def __init__(self, output=None, interactive=True, **variables):
+    def __init__(self, output=None, interactive=True, usedefaults=False, **variables):
+        """
+        - output : output file or directory
+        - interactive : whether tointeractively get variables
+        - usedefaults : try to use the default values if not specified
+        """
+        
         assert self.templates
         self.output = output
         self.interactive = interactive
         self.location = os.path.dirnme(os.path.abspath(__file__))
         self.defaults = variables.copy
 
+        # make a dictionary of the variables
+        self.vardict = {}
+        for i in self.vars:
+            self.vardict[i.name] = i
+
         # ensure all of these templates exist
         for template in self.templates:
             if template.startswith('http://') or template.startswith('https://'):
@@ -73,12 +88,35 @@
                 path = os.path.join(self.location, template)
             assert os.path.exists(template)
 
+    def missing(self, **variables):
+        if self.look:
+            pass
+        else:
+            if self.usedefaults:
+                pass
+
     def pre(self, **variables):
         """do stuff before interpolation"""
 
+    def substitute(self, **variables):
+        """do the substitution"""
+        vars = self.get_variables(**variables)
+        self.pre(**variables)
+        self.check_missing(vars)
+        self.post(**variables)
+
     def post(self, **variables):
         """do stuff after interpolation"""
         
+    def read_variables(self, variables):
+        """read variables from stdin"""
+        retval = {}
+        for i in variables:
+            if i in self.vardict:
+                self.vardict[i].read()
+            else:
+                retval.update(ContentTemplate.read_variables(self, (i,)))
+        return retval
 
 class PasteScriptTemplate(MakeItSoTemplate):
     """template for backwards compatability with PasteScript"""