# HG changeset patch # User Jeff Hammel # Date 1294193238 28800 # Node ID 6e08cca7d656090d11083b19a06d01acd71a4bd1 # Parent 554f916cef13c56c7749d8e38d3f7da362a6d385 do API variable reading and stubbing a bit for control flow diff -r 554f916cef13 -r 6e08cca7d656 TODO.txt --- a/TODO.txt Tue Jan 04 17:40:21 2011 -0800 +++ b/TODO.txt Tue Jan 04 18:07:18 2011 -0800 @@ -89,6 +89,8 @@ template like that and have each template applied e.g. increment dependencies. So the latter are like variables. +- ability to output to a POST request to a URL + Tempita: - Parsing variables out of tempita should be workable. Even if a diff -r 554f916cef13 -r 6e08cca7d656 makeitso/makeitso.py --- a/makeitso/makeitso.py Tue Jan 04 17:40:21 2011 -0800 +++ b/makeitso/makeitso.py Tue Jan 04 18:07:18 2011 -0800 @@ -89,7 +89,7 @@ return os.path.basename(uri) def include(uri): - f, headers = urllib.urlretrieve(uri) + f, headers = urllib.urlretrieve(uri) # XXX -> urllib2 for timeout return file(f).read() ### things that deal with variables @@ -146,6 +146,24 @@ missing.add(missed) vars[missed] = '' return missing + + def get_variables(self, **variables): + vars = self.defaults.copy() + vars.update(variables) + return vars + + def check_missing(self, vars): + """ + check for missing variables and, if applicable, + update them from the command line + """ + missing = self.missing(**vars) + if missing: + if self.interactive: + vars.update(self.read_variables(missing)) + else: + raise MissingVariablesException(missing) + def variables(self): """return the variables needed for a template""" @@ -153,14 +171,8 @@ def substitute(self, **variables): """interactive (for now) substitution""" - vars = self.defaults.copy() - vars.update(variables) - missing = self.missing(**vars) - if missing: - if self.interactive: - vars.update(self.read_variables(missing)) - else: - raise MissingVariablesException(missing) + vars = self.get_variables() + self.check_missing(vars) return self._substitute(**vars) def _substitute(self, **variables): @@ -294,6 +306,7 @@ self.templates = [] self.output = output for template in templates: + # TODO: check if the template is a [e.g] PasteScript.template entry point if os.path.isdir(template): self.templates.append(DirectoryTemplate(template, interactive=self.interactive, output=output, **variables)) else: diff -r 554f916cef13 -r 6e08cca7d656 makeitso/template.py --- 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""" diff -r 554f916cef13 -r 6e08cca7d656 setup.py --- a/setup.py Tue Jan 04 17:40:21 2011 -0800 +++ b/setup.py Tue Jan 04 18:07:18 2011 -0800 @@ -19,7 +19,8 @@ zip_safe=False, install_requires=[ # -*- Extra requirements: -*- - 'tempita' + 'tempita', + 'webob', ], entry_points=""" # -*- Entry points: -*-