Mercurial > hg > MakeItSo
comparison makeitso/template.py @ 41:9956e13558dd
stub out what API templates will look like; put these in a separate file as theres no reason to clutter up the command line entry point any further
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 03 Jan 2011 14:18:59 -0800 |
parents | |
children | 73dac34d2692 |
comparison
equal
deleted
inserted
replaced
40:6b4c8f23192f | 41:9956e13558dd |
---|---|
1 """ | |
2 basic API template class | |
3 """ | |
4 | |
5 import sys | |
6 from makeitso import ContentTemplate | |
7 | |
8 class Variable(object): | |
9 """variable object for MakeItSo templates""" | |
10 | |
11 def __init__(self, name, default=None, description=None, | |
12 cast=None): | |
13 self.name = name | |
14 self.default = default | |
15 self.description = description | |
16 | |
17 # TODO (maybe): get cast from default variable type if not None | |
18 self.cast = cast | |
19 | |
20 self._set = False | |
21 | |
22 def set(self, value): | |
23 if self.cast: | |
24 self.value = self.cast(value) | |
25 else: | |
26 self.value = value | |
27 self._set = True | |
28 | |
29 def read(self, fd=sys.stdout): | |
30 """prompt and read the variable from stdin""" | |
31 fd.write(self.display()) | |
32 self.set(raw_input()) | |
33 | |
34 def display(self): | |
35 description = self.description or self.name | |
36 if self.default: | |
37 return 'Enter %s [DEFAULT: %s]:' % (description, repr(self.default)) | |
38 else: | |
39 return 'Enter %s:' % description | |
40 | |
41 class MakeItSoTemplate(object): | |
42 | |
43 def pre(self, **variables): | |
44 """do stuff before interpolation""" | |
45 | |
46 def post(self, **variables): | |
47 """do stuff after interpolation""" | |
48 | |
49 |