comparison makeitso/makeitso.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 e055447376ab
children d5da38fabdf7
comparison
equal deleted inserted replaced
89:e055447376ab 90:26b9c3bba04e
173 173
174 def substitute(self, **variables): 174 def substitute(self, **variables):
175 """interactive (for now) substitution""" 175 """interactive (for now) substitution"""
176 vars = self.get_variables(**variables) 176 vars = self.get_variables(**variables)
177 self.check_missing(vars) 177 self.check_missing(vars)
178 return self._substitute(**vars) 178 return tempita.Template.substitute(self, **vars)
179
180 def _substitute(self, **variables):
181 return tempita.Template.substitute(self, **variables)
182 179
183 def read_variables(self, variables): 180 def read_variables(self, variables):
184 """read variables from stdin""" 181 """read variables from stdin"""
185 # TODO: variables should (optionally) be richer objects 182 # TODO: variables should (optionally) be richer objects
186 retval = {} 183 retval = {}
208 205
209 ContentTemplate.__init__(self, content, name=uri, 206 ContentTemplate.__init__(self, content, name=uri,
210 interactive=interactive, 207 interactive=interactive,
211 variables=variables) 208 variables=variables)
212 209
213 def substitute(self, output=None, **variables): 210 def substitute(self, variables, output=None):
214 content = ContentTemplate.substitute(self, **variables) 211 content = ContentTemplate.substitute(self, **variables)
212
213 # write output
215 output = output or sys.stdout 214 output = output or sys.stdout
216
217 if isinstance(output, basestring): 215 if isinstance(output, basestring):
218 path = output 216 path = output
219 if os.path.isdir(output): 217 if os.path.isdir(output):
220 path = os.path.join(path, basename(self.name)) 218 path = os.path.join(path, basename(self.name))
221 f = file(path, 'w') 219 f = file(path, 'w')
263 missing.update(missed) 261 missing.update(missed)
264 variables.update(dict([(i, '') for i in missed])) 262 variables.update(dict([(i, '') for i in missed]))
265 263
266 # find variables from files 264 # find variables from files
267 for f in filenames: 265 for f in filenames:
266 missed = ContentTemplate(d).missing(**vars)
267 missing.update(missed)
268 variables.update(dict([(i, '') for i in missed]))
269
268 path = os.path.join(dirpath, f) 270 path = os.path.join(dirpath, f)
269 template = URITemplate(path, interactive=self.interactive) 271 template = URITemplate(path, interactive=self.interactive)
270 missed = template.missing(**vars) 272 missed = template.missing(**vars)
271 missing.update(missed) 273 missing.update(missed)
272 variables.update(dict([(i, '') for i in missed])) 274 variables.update(dict([(i, '') for i in missed]))
273 275
274 return missing 276 return missing
275 277
276 def substitute(self, output, **variables): 278 def substitute(self, variables, output):
277 self.check_output(output) 279 self.check_output(output)
278 vars = self.get_variables(**variables) 280 vars = self.get_variables(**variables)
279 self.check_missing(vars) 281 self.check_missing(vars)
280 282
281 # TODO: do this with recursion instead of os.walk so that 283 # TODO: do this with recursion instead of os.walk so that
300 else: 302 else:
301 os.makedirs(target) 303 os.makedirs(target)
302 304
303 # interpolate files 305 # interpolate files
304 for filename in filenames: 306 for filename in filenames:
307
308 # interpolate filenames
305 path = os.path.join(dirname, filename) 309 path = os.path.join(dirname, filename)
306 interpolated = ContentTemplate(path).substitute(**variables) 310 interpolated = ContentTemplate(path).substitute(**variables)
307 target = os.path.join(output, interpolated.split(self.name, 1)[-1].strip(os.path.sep)) 311 target = os.path.join(output, interpolated.split(self.name, 1)[-1].strip(os.path.sep))
308 312
313 # interpolate their contents
309 if os.path.exists(target): 314 if os.path.exists(target):
310 # ensure its a directory 315 # ensure its a directory
311 # TODO: check this first before interpolation is in progress
312 assert os.path.isfile(target), "Can't substitute a file on top of a directory" 316 assert os.path.isfile(target), "Can't substitute a file on top of a directory"
313 template = URITemplate(path, interactive=False) 317 template = URITemplate(path, interactive=False)
314 template.substitute(target, **variables) 318 template.substitute(variables, target)
315 319
316 320
317 class PolyTemplate(ContentTemplate): 321 class PolyTemplate(ContentTemplate):
318 """template for several files/directories""" 322 """template for several files/directories"""
319 323
343 assert os.path.isdir(output), "Must specify a directory for multiple templates" 347 assert os.path.isdir(output), "Must specify a directory for multiple templates"
344 for template in self.templates: 348 for template in self.templates:
345 if hasattr(template, 'check_output'): 349 if hasattr(template, 'check_output'):
346 template.check_output(output) 350 template.check_output(output)
347 351
348 def substitute(self, output=None, **variables): 352 def substitute(self, variables, output=None):
349 353
350 # determine where the hell to put these things 354 # determine where the hell to put these things
351 self.check_output(output) 355 self.check_output(output)
352 356
353 # get the variables 357 # get the variables
358 if output and len(self.templates) > 1 and not os.path.exists(output): 362 if output and len(self.templates) > 1 and not os.path.exists(output):
359 os.makedirs(output) 363 os.makedirs(output)
360 364
361 # do the substitution 365 # do the substitution
362 for template in self.templates: 366 for template in self.templates:
363 template.substitute(output, **vars) 367 template.substitute(vars, output)
364 368
365 ### command line interface 369 ### command line interface
366 370
367 def invocation(url, **variables): 371 def invocation(url, **variables):
368 """returns a string appropriate for TTW invocation""" 372 """returns a string appropriate for TTW invocation"""