Mercurial > hg > MakeItSo
diff makeitso/makeitso.py @ 55:b087a14a664b
fix several issues involving variable interpolation
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 06 Jan 2011 14:15:19 -0800 |
parents | d3e1e5745f24 |
children | 728cae02a6ed |
line wrap: on
line diff
--- a/makeitso/makeitso.py Thu Jan 06 13:14:12 2011 -0800 +++ b/makeitso/makeitso.py Thu Jan 06 14:15:19 2011 -0800 @@ -123,11 +123,11 @@ defaults = {'include': include} - def __init__(self, content, name=None, interactive=True, **variables): + def __init__(self, content, name=None, interactive=True, variables=None): # default variables self.defaults = self.__class__.defaults.copy() - self.defaults.update(variables) + self.defaults.update(variables or {}) # TODO: automagically tell if the program is interactive or not self.interactive = interactive @@ -136,7 +136,7 @@ def missing(self, **variables): """return additional variables needed""" - vars = variables.copy() + vars = self.get_variables(**variables) missing = set([]) while True: try: @@ -192,7 +192,7 @@ class URITemplate(ContentTemplate): """template for a file or URL""" - def __init__(self, uri, output=None, interactive=True, **variables): + def __init__(self, uri, output=None, interactive=True, variables=None): self.output = output or sys.stdout content = include(uri) @@ -200,10 +200,10 @@ if shebang_re.match(content): content = os.linesep.join(content.splitlines()[1:]) - variables['here'] = parent_uri(uri) + variables['here'] = parent_uri(uri) ContentTemplate.__init__(self, content, name=uri, interactive=interactive, - **variables) + variables=variables) def substitute(self, **variables): output = ContentTemplate.substitute(self, **variables) @@ -221,7 +221,7 @@ class DirectoryTemplate(ContentTemplate): """template for a directory structure""" - def __init__(self, directory, output=None, interactive=True, **variables): + def __init__(self, directory, output=None, interactive=True, variables=None): """ - output : output directory; if None will render in place """ @@ -233,17 +233,18 @@ if os.path.exists(output): assert os.path.isdir(output), "%s: Must be a directory" % self.name self.defaults = ContentTemplate.defaults.copy() - self.defaults.update(variables) + self.defaults.update(variables or {}) def missing(self, **variables): - variables = variables.copy() + vars = self.defaults.copy() + vars.update(variables) missing = set([]) for dirpath, dirnames, filenames in os.walk(self.name): # find variables from directory names for d in dirnames: - missed = ContentTemplate(d).missing(**variables) + missed = ContentTemplate(d).missing(**vars) missing.update(missed) variables.update(dict([(i, '') for i in missed])) @@ -251,7 +252,7 @@ for f in filenames: path = os.path.join(dirpath, f) template = URITemplate(path, interactive=self.interactive) - missed = template.missing(**variables) + missed = template.missing(**vars) missing.update(missed) variables.update(dict([(i, '') for i in missed])) @@ -298,7 +299,7 @@ class PolyTemplate(ContentTemplate): """template for several files/directories""" - def __init__(self, templates, output=None, interactive=True, **variables): + def __init__(self, templates, output=None, interactive=True, variables=None): assert templates, "No templates given!" @@ -309,9 +310,9 @@ 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)) + self.templates.append(DirectoryTemplate(template, interactive=self.interactive, output=output, variables=variables)) else: - self.templates.append(URITemplate(template, interactive=self.interactive, output=output, **variables)) + self.templates.append(URITemplate(template, interactive=self.interactive, output=output, variables=variables)) def missing(self, **variables): vars = variables.copy()