Mercurial > hg > MakeItSo
comparison makeitso/template.py @ 99:d9c6e26a42ff
fix up interactivity of API template a bit. still doesnt quite work
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 11 Jan 2011 11:53:02 -0800 |
parents | 26b9c3bba04e |
children | b54898f7d8a9 |
comparison
equal
deleted
inserted
replaced
98:37f92ae8f999 | 99:d9c6e26a42ff |
---|---|
35 if self.cast: | 35 if self.cast: |
36 self.value = self.cast(value) | 36 self.value = self.cast(value) |
37 else: | 37 else: |
38 self.value = value | 38 self.value = value |
39 self._set = True | 39 self._set = True |
40 return self.value | |
41 | |
42 def isset(self): | |
43 """whether the variable has been set or not""" | |
44 return self._set | |
40 | 45 |
41 def read(self, fd=sys.stdout): | 46 def read(self, fd=sys.stdout): |
42 """prompt and read the variable from stdin""" | 47 """prompt and read the variable from stdin""" |
43 fd.write(self.display()) | 48 fd.write(self.display()) |
44 self.set(raw_input()) | 49 self.set(raw_input()) |
45 | 50 |
46 def display(self): | 51 def display(self): |
47 description = self.description or self.name | 52 description = self.description or self.name |
48 if self.default: | 53 if self.default: |
49 return 'Enter %s [DEFAULT: %s]:' % (description, repr(self.default)) | 54 return 'Enter %s [DEFAULT: %s]: ' % (description, repr(self.default)) |
50 else: | 55 else: |
51 return 'Enter %s:' % description | 56 return 'Enter %s: ' % description |
52 | 57 |
53 def __repr__(self): | 58 def __repr__(self): |
54 return "Variable(name='%s')" % self.name | 59 return "Variable(name='%s')" % self.name |
55 | 60 |
56 def assemble(*args): | 61 def assemble(*args): |
135 vars = self.get_variables(**variables) | 140 vars = self.get_variables(**variables) |
136 missing = set([]) | 141 missing = set([]) |
137 | 142 |
138 # get known needed variables | 143 # get known needed variables |
139 for var in self.vars: | 144 for var in self.vars: |
140 if var.name not in vars: | 145 if var.name in vars: |
141 missing.add(var) | 146 if var.default is Undefined: |
147 missing.add(var.name) | |
148 continue | |
149 if self.usedefaults and not var.isset(): | |
150 missing.add(var.name) | |
151 else: | |
152 missing.add(var.name) | |
142 | 153 |
143 if self.look: | 154 if self.look: |
144 # scan templates for other variables | 155 # scan templates for other variables |
145 raise NotImplementedError | 156 template = PolyTemplate(self._templates, |
146 | 157 interactive=self.interactive, |
158 variables=vars) | |
159 missing.update(template.missing()) | |
160 | |
147 return missing | 161 return missing |
148 | 162 |
149 def pre(self, variables): | 163 def pre(self, variables): |
150 """do stuff before interpolation""" | 164 """do stuff before interpolation""" |
151 | 165 |
171 def read_variables(self, variables): | 185 def read_variables(self, variables): |
172 """read variables from stdin""" | 186 """read variables from stdin""" |
173 retval = {} | 187 retval = {} |
174 for i in variables: | 188 for i in variables: |
175 if i in self.vardict: | 189 if i in self.vardict: |
176 self.vardict[i].read() | 190 value = self.vardict[i].read() |
191 retval[i] = value | |
177 else: | 192 else: |
178 retval.update(ContentTemplate.read_variables(self, (i,))) | 193 retval.update(ContentTemplate.read_variables(self, (i,))) |
179 return retval | 194 return retval |
180 | 195 |
181 class PasteScriptTemplate(MakeItSoTemplate): | 196 class PasteScriptTemplate(MakeItSoTemplate): |