Mercurial > genshi > mirror
comparison genshi/template/base.py @ 718:d143dd73789b experimental-match-fastpaths
update to trunk through r833
author | aflett |
---|---|
date | Tue, 08 Apr 2008 23:45:32 +0000 |
parents | 0e8b92905741 |
children | ea46fb523485 |
comparison
equal
deleted
inserted
replaced
717:0e8b92905741 | 718:d143dd73789b |
---|---|
1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
2 # | 2 # |
3 # Copyright (C) 2006-2007 Edgewall Software | 3 # Copyright (C) 2006-2008 Edgewall Software |
4 # All rights reserved. | 4 # All rights reserved. |
5 # | 5 # |
6 # This software is licensed as described in the file COPYING, which | 6 # This software is licensed as described in the file COPYING, which |
7 # you should have received as part of this distribution. The terms | 7 # you should have received as part of this distribution. The terms |
8 # are also available at http://genshi.edgewall.org/wiki/License. | 8 # are also available at http://genshi.edgewall.org/wiki/License. |
336 """ | 336 """ |
337 | 337 |
338 serializer = None | 338 serializer = None |
339 _number_conv = unicode # function used to convert numbers to event data | 339 _number_conv = unicode # function used to convert numbers to event data |
340 | 340 |
341 def __init__(self, source, basedir=None, filename=None, loader=None, | 341 def __init__(self, source, filepath=None, filename=None, loader=None, |
342 encoding=None, lookup='strict', allow_exec=True): | 342 encoding=None, lookup='strict', allow_exec=True): |
343 """Initialize a template from either a string, a file-like object, or | 343 """Initialize a template from either a string, a file-like object, or |
344 an already parsed markup stream. | 344 an already parsed markup stream. |
345 | 345 |
346 :param source: a string, file-like object, or markup stream to read the | 346 :param source: a string, file-like object, or markup stream to read the |
347 template from | 347 template from |
348 :param basedir: the base directory containing the template file; when | 348 :param filepath: the absolute path to the template file |
349 loaded from a `TemplateLoader`, this will be the | 349 :param filename: the path to the template file relative to the search |
350 directory on the template search path in which the | 350 path |
351 template was found | |
352 :param filename: the name of the template file, relative to the given | |
353 base directory | |
354 :param loader: the `TemplateLoader` to use for loading included | 351 :param loader: the `TemplateLoader` to use for loading included |
355 templates | 352 templates |
356 :param encoding: the encoding of the `source` | 353 :param encoding: the encoding of the `source` |
357 :param lookup: the variable lookup mechanism; either "strict" (the | 354 :param lookup: the variable lookup mechanism; either "strict" (the |
358 default), "lenient", or a custom lookup class | 355 default), "lenient", or a custom lookup class |
359 :param allow_exec: whether Python code blocks in templates should be | 356 :param allow_exec: whether Python code blocks in templates should be |
360 allowed | 357 allowed |
361 | 358 |
362 :note: Changed in 0.5: Added the `allow_exec` argument | 359 :note: Changed in 0.5: Added the `allow_exec` argument |
363 """ | 360 """ |
364 self.basedir = basedir | 361 self.filepath = filepath or filename |
365 self.filename = filename | 362 self.filename = filename |
366 if basedir and filename: | |
367 self.filepath = os.path.join(basedir, filename) | |
368 else: | |
369 self.filepath = filename | |
370 self.loader = loader | 363 self.loader = loader |
371 self.lookup = lookup | 364 self.lookup = lookup |
372 self.allow_exec = allow_exec | 365 self.allow_exec = allow_exec |
373 | 366 self._init_filters() |
374 self.filters = [self._flatten, self._eval, self._exec] | |
375 if loader: | |
376 self.filters.append(self._include) | |
377 | 367 |
378 if isinstance(source, basestring): | 368 if isinstance(source, basestring): |
379 source = StringIO(source) | 369 source = StringIO(source) |
380 else: | 370 else: |
381 source = source | 371 source = source |
382 try: | 372 try: |
383 self.stream = list(self._prepare(self._parse(source, encoding))) | 373 self.stream = list(self._prepare(self._parse(source, encoding))) |
384 except ParseError, e: | 374 except ParseError, e: |
385 raise TemplateSyntaxError(e.msg, self.filepath, e.lineno, e.offset) | 375 raise TemplateSyntaxError(e.msg, self.filepath, e.lineno, e.offset) |
386 | 376 |
377 def __getstate__(self): | |
378 state = self.__dict__.copy() | |
379 state['filters'] = [] | |
380 return state | |
381 | |
382 def __setstate__(self, state): | |
383 self.__dict__ = state | |
384 self._init_filters() | |
385 | |
387 def __repr__(self): | 386 def __repr__(self): |
388 return '<%s "%s">' % (self.__class__.__name__, self.filename) | 387 return '<%s "%s">' % (self.__class__.__name__, self.filename) |
388 | |
389 def _init_filters(self): | |
390 self.filters = [self._flatten, self._eval, self._exec] | |
391 if self.loader: | |
392 self.filters.append(self._include) | |
389 | 393 |
390 def _parse(self, source, encoding): | 394 def _parse(self, source, encoding): |
391 """Parse the template. | 395 """Parse the template. |
392 | 396 |
393 The parsing stage parses the template and constructs a list of | 397 The parsing stage parses the template and constructs a list of |