Mercurial > hg > MakeItSo
comparison makeitso/template.py @ 65:0152741621c1
check in a failing test wrt location
| author | Jeff Hammel <jhammel@mozilla.com> |
|---|---|
| date | Fri, 07 Jan 2011 10:17:48 -0800 |
| parents | c20277dbf8fa |
| children | 7821c82772f5 |
comparison
equal
deleted
inserted
replaced
| 64:c20277dbf8fa | 65:0152741621c1 |
|---|---|
| 7 from makeitso import ContentTemplate | 7 from makeitso import ContentTemplate |
| 8 from makeitso import PolyTemplate | 8 from makeitso import PolyTemplate |
| 9 | 9 |
| 10 class Undefined(object): | 10 class Undefined(object): |
| 11 """marker class for variables""" | 11 """marker class for variables""" |
| 12 def __int__(self): | 12 def __nonzero__(self): |
| 13 return 0 | 13 return False |
| 14 Undefined = Undefined() # singleton | 14 Undefined = Undefined() # singleton |
| 15 | 15 |
| 16 class Variable(object): | 16 class Variable(object): |
| 17 """variable object for MakeItSo templates""" | 17 """variable object for MakeItSo templates""" |
| 18 | 18 |
| 73 - usedefaults : try to use the default values if not specified | 73 - usedefaults : try to use the default values if not specified |
| 74 """ | 74 """ |
| 75 | 75 |
| 76 # boilerplate | 76 # boilerplate |
| 77 assert self.templates | 77 assert self.templates |
| 78 variables = variables or {} | |
| 78 self.output = output | 79 self.output = output |
| 79 self.interactive = interactive | 80 self.interactive = interactive |
| 80 self.location = os.path.dirname(os.path.abspath(__file__)) | 81 self.location = os.path.dirname(os.path.abspath(__file__)) |
| 81 self.defaults = variables.copy() | 82 self.defaults = variables.copy() |
| 82 self.usedefaults = usedefaults | 83 self.usedefaults = usedefaults |
| 85 self.vardict = {} | 86 self.vardict = {} |
| 86 for i in self.vars: | 87 for i in self.vars: |
| 87 self.vardict[i.name] = i | 88 self.vardict[i.name] = i |
| 88 | 89 |
| 89 # ensure all of these templates exist | 90 # ensure all of these templates exist |
| 91 self._templates = [] | |
| 90 for template in self.templates: | 92 for template in self.templates: |
| 91 if template.startswith('http://') or template.startswith('https://'): | 93 if template.startswith('http://') or template.startswith('https://'): |
| 94 self._templates.append(template) | |
| 92 continue | 95 continue |
| 93 if os.path.isabs(template): | 96 if os.path.isabs(template): |
| 94 path = template | 97 path = template |
| 95 else: | 98 else: |
| 96 path = os.path.join(self.location, template) | 99 path = os.path.join(self.location, template) |
| 97 assert os.path.exists(template) | 100 assert os.path.exists(path), "%s does not exist" % path |
| 101 self._templates.append(path) | |
| 98 | 102 |
| 99 def get_variables(self, **variables): | 103 def get_variables(self, **variables): |
| 100 # XXX could do this in the ctor | 104 # XXX could do this in the ctor |
| 101 vars = ContentTemplate.get_variables(self, **variables) | 105 vars = ContentTemplate.get_variables(self, **variables) |
| 102 if self.usedefaults: | 106 if self.usedefaults: |
| 125 def pre(self, **variables): | 129 def pre(self, **variables): |
| 126 """do stuff before interpolation""" | 130 """do stuff before interpolation""" |
| 127 | 131 |
| 128 def substitute(self, **variables): | 132 def substitute(self, **variables): |
| 129 """do the substitution""" | 133 """do the substitution""" |
| 134 | |
| 130 vars = self.get_variables(**variables) | 135 vars = self.get_variables(**variables) |
| 131 self.pre(**vars) | 136 self.pre(**vars) |
| 132 self.check_missing(vars) | 137 self.check_missing(vars) |
| 133 | 138 |
| 134 # do the substitution | 139 # do the substitution |
| 135 PolyTemplate(self.templates, | 140 PolyTemplate(self.templates, |
| 136 output=self.output, | 141 output=self.output, |
| 137 interactive=self.interactive, | 142 interactive=self.interactive, |
| 138 variables = vars | 143 variables=vars) |
| 139 | |
| 140 | 144 |
| 141 self.post(**variables) | 145 self.post(**variables) |
| 142 | 146 |
| 143 def post(self, **variables): | 147 def post(self, **variables): |
| 144 """do stuff after interpolation""" | 148 """do stuff after interpolation""" |
