comparison tincan.py @ 22:f6a1492fe56e draft

Template loading (includes) pass all tests.
author David Barts <n5jrn@me.com>
date Tue, 21 May 2019 16:01:53 -0700
parents ca2029ce95c7
children e8b6ee7e5b6b 4c6d3704e51f
comparison
equal deleted inserted replaced
21:ca2029ce95c7 22:f6a1492fe56e
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['load'] = \
211 _LoaderFactory(base, subdir, encoding) 211 _LoaderFactory(base, subdir, encoding)
212 self.expression_types['lload'] = \ 212 self.expression_types['lload'] = \
213 _LoaderFactory(os.path.join(base,_WINF,"tlib"), None, 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.
218 """ 218 """
233 raise ValueError("loaded templates must end in .pspx or .pt") 233 raise ValueError("loaded templates must end in .pspx or .pt")
234 self.path = string 234 self.path = string
235 self.params = based_on 235 self.params = based_on
236 236
237 def __call__(self, target, engine): 237 def __call__(self, target, engine):
238 if self.params.subdir is None: 238 try:
239 npath = os.path.join(self.params.base, self.path.lstrip('/')) 239 normalized = _normpath(self.params.subdir, self.path)
240 else: 240 except IndexError:
241 try: 241 raise ValueError("invalid path: {0!s}".format(self.path))
242 normalized = _normpath(self.params.subdir, self.path) 242 npath = os.path.join(self.params.base, *normalized)
243 except IndexError:
244 raise ValueError("invalid path: {0!s}".format(self.path))
245 npath = os.path.join(self.params.base, *normalized)
246 with open(npath, "r", encoding=self.params.encoding) as fp: 243 with open(npath, "r", encoding=self.params.encoding) as fp:
247 contents = fp.read() 244 contents = fp.read()
248 value = ast.Str(contents) 245 value = ast.Str(contents)
249 return [ast.Assign(targets=[target], value=value)] 246 return [ast.Assign(targets=[target], value=value)]
250 247