comparison tincan.py @ 28:4c6d3704e51f draft chameleon-load

Fails; the thing apparently wants strings and not objects.
author David Barts <n5jrn@me.com>
date Sun, 26 May 2019 18:14:14 -0700
parents f6a1492fe56e
children
comparison
equal deleted inserted replaced
27:cf0e52076476 28:4c6d3704e51f
205 def __init__(self, body, base=None, subdir=None, **config): 205 def __init__(self, body, base=None, subdir=None, **config):
206 super(TinCanChameleon, self).__init__(body, **config) 206 super(TinCanChameleon, self).__init__(body, **config)
207 if base is not None: 207 if base is not None:
208 encoding = config.get("encoding", "utf-8") 208 encoding = config.get("encoding", "utf-8")
209 if subdir is not None: 209 if subdir is not None:
210 self.expression_types['load'] = \ 210 self.expression_types['lload'] = \
211 _LoaderFactory(base, subdir, encoding) 211 _LoaderFactory(base, subdir, encoding)
212 self.expression_types['lload'] = \ 212 self.expression_types['load'] = \
213 _LoaderFactory(os.path.join(base,_WINF,"tlib"), [], encoding) 213 _LoaderFactory(os.path.join(base,_WINF,"tlib"), [], encoding)
214 214
215 class _LoaderFactory(object): 215 class _LoaderFactory(object):
216 """ 216 """
217 One of two helper classes for the above. 217 One of two helper classes for the above.
226 226
227 class _Loader(object): 227 class _Loader(object):
228 """ 228 """
229 Two of two helper classes for the above. 229 Two of two helper classes for the above.
230 """ 230 """
231 _EXTEN = ".pt"
231 def __init__(self, based_on, string): 232 def __init__(self, based_on, string):
232 if not (string.endswith(".pspx") or string.endswith(".pt")): 233 if not string.endswith(self._EXTEN):
233 raise ValueError("loaded templates must end in .pspx or .pt") 234 raise ValueError("loaded templates must end in {0}".format(self._EXTEN))
234 self.path = string 235 self.path = string
235 self.params = based_on 236 self.params = based_on
236 237
237 def __call__(self, target, engine): 238 def __call__(self, target, engine):
238 try: 239 try:
240 except IndexError: 241 except IndexError:
241 raise ValueError("invalid path: {0!s}".format(self.path)) 242 raise ValueError("invalid path: {0!s}".format(self.path))
242 npath = os.path.join(self.params.base, *normalized) 243 npath = os.path.join(self.params.base, *normalized)
243 with open(npath, "r", encoding=self.params.encoding) as fp: 244 with open(npath, "r", encoding=self.params.encoding) as fp:
244 contents = fp.read() 245 contents = fp.read()
245 value = ast.Str(contents) 246 value = ast.Call(func=TinCanChameleon, args=[contents],
247 kwargs={"base": self.params.base, "subdir": self.params.subdir,
248 "encoding": self.params.encoding})
246 return [ast.Assign(targets=[target], value=value)] 249 return [ast.Assign(targets=[target], value=value)]
247 250
248 # U t i l i t i e s 251 # U t i l i t i e s
249 252
250 def _normpath(base, unsplit): 253 def _normpath(base, unsplit):