comparison makeitso/makeitso.py @ 44:6e08cca7d656

do API variable reading and stubbing a bit for control flow
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 04 Jan 2011 18:07:18 -0800
parents 73dac34d2692
children 2765a700982e
comparison
equal deleted inserted replaced
43:554f916cef13 44:6e08cca7d656
87 return uri.rsplit('/', 1)[1] 87 return uri.rsplit('/', 1)[1]
88 else: 88 else:
89 return os.path.basename(uri) 89 return os.path.basename(uri)
90 90
91 def include(uri): 91 def include(uri):
92 f, headers = urllib.urlretrieve(uri) 92 f, headers = urllib.urlretrieve(uri) # XXX -> urllib2 for timeout
93 return file(f).read() 93 return file(f).read()
94 94
95 ### things that deal with variables 95 ### things that deal with variables
96 96
97 class MissingVariablesException(Exception): 97 class MissingVariablesException(Exception):
144 except NameError, e: 144 except NameError, e:
145 missed = get_missing(e) 145 missed = get_missing(e)
146 missing.add(missed) 146 missing.add(missed)
147 vars[missed] = '' 147 vars[missed] = ''
148 return missing 148 return missing
149 149
150 def variables(self): 150 def get_variables(self, **variables):
151 """return the variables needed for a template"""
152 return self.missing()
153
154 def substitute(self, **variables):
155 """interactive (for now) substitution"""
156 vars = self.defaults.copy() 151 vars = self.defaults.copy()
157 vars.update(variables) 152 vars.update(variables)
153 return vars
154
155 def check_missing(self, vars):
156 """
157 check for missing variables and, if applicable,
158 update them from the command line
159 """
158 missing = self.missing(**vars) 160 missing = self.missing(**vars)
159 if missing: 161 if missing:
160 if self.interactive: 162 if self.interactive:
161 vars.update(self.read_variables(missing)) 163 vars.update(self.read_variables(missing))
162 else: 164 else:
163 raise MissingVariablesException(missing) 165 raise MissingVariablesException(missing)
166
167
168 def variables(self):
169 """return the variables needed for a template"""
170 return self.missing()
171
172 def substitute(self, **variables):
173 """interactive (for now) substitution"""
174 vars = self.get_variables()
175 self.check_missing(vars)
164 return self._substitute(**vars) 176 return self._substitute(**vars)
165 177
166 def _substitute(self, **variables): 178 def _substitute(self, **variables):
167 return tempita.Template.substitute(self, **variables) 179 return tempita.Template.substitute(self, **variables)
168 180
292 self.interactive = True 304 self.interactive = True
293 self._templates = templates[:] 305 self._templates = templates[:]
294 self.templates = [] 306 self.templates = []
295 self.output = output 307 self.output = output
296 for template in templates: 308 for template in templates:
309 # TODO: check if the template is a [e.g] PasteScript.template entry point
297 if os.path.isdir(template): 310 if os.path.isdir(template):
298 self.templates.append(DirectoryTemplate(template, interactive=self.interactive, output=output, **variables)) 311 self.templates.append(DirectoryTemplate(template, interactive=self.interactive, output=output, **variables))
299 else: 312 else:
300 self.templates.append(URITemplate(template, interactive=self.interactive, output=output, **variables)) 313 self.templates.append(URITemplate(template, interactive=self.interactive, output=output, **variables))
301 314