comparison makeitso/template.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 712a6d358083
children d9c6e26a42ff
comparison
equal deleted inserted replaced
89:e055447376ab 90:26b9c3bba04e
83 vars = [] 83 vars = []
84 84
85 # inspect the templates for more variables 85 # inspect the templates for more variables
86 look = False 86 look = False
87 87
88 def __init__(self, output=None, interactive=True, usedefaults=True, 88 def __init__(self, interactive=True, usedefaults=True, variables=None):
89 variables=None):
90 """ 89 """
91 - output : output file or directory 90 - output : output file or directory
92 - interactive : whether tointeractively get variables 91 - interactive : whether tointeractively get variables
93 - usedefaults : try to use the default values if not specified 92 - usedefaults : try to use the default values if not specified
94 """ 93 """
95 94
96 # boilerplate 95 # boilerplate
97 variables = variables or {} 96 variables = variables or {}
98 self.output = output
99 if not self.description and hasattr(self, '__doc__'): 97 if not self.description and hasattr(self, '__doc__'):
100 self.description = self.__doc__ 98 self.description = self.__doc__
101 self.interactive = interactive 99 self.interactive = interactive
102 _file = sys.modules[self.__class__.__module__].__file__ 100 _file = sys.modules[self.__class__.__module__].__file__
103 self.location = os.path.dirname(os.path.abspath(_file)) 101 self.location = os.path.dirname(os.path.abspath(_file))
149 return missing 147 return missing
150 148
151 def pre(self, variables): 149 def pre(self, variables):
152 """do stuff before interpolation""" 150 """do stuff before interpolation"""
153 151
154 def substitute(self, output, **variables): 152 def substitute(self, variables, output):
155 """do the substitution""" 153 """do the substitution"""
156 154
157 vars = self.get_variables(**variables) 155 vars = self.get_variables(**variables)
158 self.pre(vars) 156 self.pre(vars)
159 self.check_missing(vars) 157 self.check_missing(vars)
161 # do the substitution 159 # do the substitution
162 template = PolyTemplate(self._templates, 160 template = PolyTemplate(self._templates,
163 interactive=self.interactive, 161 interactive=self.interactive,
164 variables=vars) 162 variables=vars)
165 template.check_output(output) 163 template.check_output(output)
166 template.substitute(output) 164 template.substitute({}, output)
167 165
168 self.post(vars) 166 self.post(vars)
169 167
170 def post(self, variables): 168 def post(self, variables):
171 """do stuff after interpolation""" 169 """do stuff after interpolation"""