comparison makeitso/template.py @ 113:c3b8ce33d3ad

make variable getting logic a little less horrible
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 17 Jan 2011 14:32:07 -0800
parents 51c9cb49edec
children b2152efec89a
comparison
equal deleted inserted replaced
112:51c9cb49edec 113:c3b8ce33d3ad
135 if variable.default is not Undefined: 135 if variable.default is not Undefined:
136 vars[variable.name] = variable.default 136 vars[variable.name] = variable.default
137 return vars 137 return vars
138 138
139 def missing(self, **variables): 139 def missing(self, **variables):
140
141 # boilerplate
140 vars = self.get_variables(**variables) 142 vars = self.get_variables(**variables)
141 missing = set([]) 143 missing = set([])
142 144
143 # get known needed variables 145 # get known needed variables
144 for var in self.vars: 146 for var in self.vars:
145 if var.name in vars: 147 if var.name not in vars:
146 if var.default is Undefined: 148 if var.default is Undefined:
147 missing.add(var.name) 149 missing.add(var.name)
148 continue 150 continue
149 if (not self.usedefaults) and (not var.isset()): 151 if (not self.usedefaults) and (not var.isset()):
150 missing.add(var.name) 152 missing.add(var.name)
151 else:
152 missing.add(var.name)
153 153
154 # scan templates for other variables
154 if self.look: 155 if self.look:
155 # scan templates for other variables
156 template = PolyTemplate(self._templates, 156 template = PolyTemplate(self._templates,
157 interactive=False, 157 interactive=False,
158 variables=vars) 158 variables=vars)
159 missing.update(template.missing()) 159 missing.update(template.missing())
160 160
163 def pre(self, variables, output): 163 def pre(self, variables, output):
164 """do stuff before interpolation""" 164 """do stuff before interpolation"""
165 165
166 def substitute(self, variables, output=None): 166 def substitute(self, variables, output=None):
167 """do the substitution""" 167 """do the substitution"""
168 168
169 # get the variables
169 vars = self.get_variables(**variables) 170 vars = self.get_variables(**variables)
170 self.pre(vars, output) 171 self.pre(vars, output)
171 self.check_missing(vars) 172 self.check_missing(vars)
172 173
173 # do the substitution 174 # do the substitution
174 template = PolyTemplate(self._templates, 175 template = PolyTemplate(self._templates,
175 interactive=self.interactive, 176 interactive=self.interactive,
176 variables=vars) 177 variables=vars)
177 template.check_output(output) 178 template.check_output(output)
178 template.substitute({}, output) 179 template.substitute({}, output)
179 180
181 # do whatever you need to do afterwards
180 self.post(vars, output) 182 self.post(vars, output)
181 183
182 def post(self, variables, output): 184 def post(self, variables, output):
183 """do stuff after interpolation""" 185 """do stuff after interpolation"""
184 186