comparison makeitso/makeitso.py @ 35:7e47ff4b0cd3

started writing tests; what a surprise, everything is broken ;)
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 01 Jan 2011 21:58:34 -0800
parents 46c2d0a7335a
children 0cba953a03ca
comparison
equal deleted inserted replaced
34:46c2d0a7335a 35:7e47ff4b0cd3
153 153
154 def substitute(self, **variables): 154 def substitute(self, **variables):
155 """interactive (for now) substitution""" 155 """interactive (for now) substitution"""
156 vars = self.defaults.copy() 156 vars = self.defaults.copy()
157 vars.update(variables) 157 vars.update(variables)
158 missing = self.missing(vars) 158 missing = self.missing(**vars)
159 if missing: 159 if missing:
160 if self.interactive: 160 if self.interactive:
161 vars.update(self.read_variables(missing)) 161 vars.update(self.read_variables(missing))
162 else: 162 else:
163 raise MissingVariablesException(missing) 163 raise MissingVariablesException(missing)
178 178
179 class URITemplate(ContentTemplate): 179 class URITemplate(ContentTemplate):
180 """template for a file or URL""" 180 """template for a file or URL"""
181 181
182 def __init__(self, uri, output=None, interactive=True, **variables): 182 def __init__(self, uri, output=None, interactive=True, **variables):
183 self.output = output or sys.stdout 183 self.output = output or sys.stdout
184
185 content = include(uri) 184 content = include(uri)
186 185
187 # remove makeitso shebang if it has one 186 # remove makeitso shebang if it has one
188 if shebang_re.match(content): 187 if shebang_re.match(content):
189 content = os.linesep.join(content.splitlines()[1:]) 188 content = os.linesep.join(content.splitlines()[1:])
268 """ 267 """
269 268
270 def __init__(self, templates, output=None, interactive=True, **variables): 269 def __init__(self, templates, output=None, interactive=True, **variables):
271 270
272 assert templates, "No templates given!" 271 assert templates, "No templates given!"
273 272
273 self._templates = templates[:]
274 self.templates = [] 274 self.templates = []
275 self.output = output 275 self.output = output
276 for template in templates: 276 for template in templates:
277 if os.path.isdir(template): 277 if os.path.isdir(template):
278 self.templates.append(DirectoryTemplate(template, output=output, **variables)) 278 self.templates.append(DirectoryTemplate(template, output=output, **variables))
290 290
291 def _substitute(self, **variables): 291 def _substitute(self, **variables):
292 292
293 # determine where the hell to put these things 293 # determine where the hell to put these things
294 if self.output is None: 294 if self.output is None:
295 dirs = [i for i in templates if os.path.isdir(i)] 295 dirs = [i for i in self._templates if os.path.isdir(i)]
296 if not ((len(dirs) == 0) or len(dirs) == len(templates)): 296 if not ((len(dirs) == 0) or len(dirs) == len(templates)):
297 raise AssertionError("Must specify output when mixing directories and URIs") 297 raise AssertionError("Must specify output when mixing directories and URIs")
298 298
299 # TODO: check for missing 299 # TODO: check for missing
300 if len(self.templates) > 1 and not os.path.exists(self.output): 300 if len(self.templates) > 1 and not os.path.exists(self.output):
353 for variable in sorted(variables): 353 for variable in sorted(variables):
354 print variable 354 print variable
355 return 355 return
356 356
357 # template variables 357 # template variables
358 _vars = [] 358 variables = {}
359 _args = [] 359 _args = []
360 for arg in args: 360 for arg in args:
361 if '=' in arg: 361 if '=' in arg:
362 key, value = arg.split('=') 362 key, value = arg.split('=')
363 variables[key] = value 363 variables[key] = value