Mercurial > genshi > mirror
comparison genshi/template/directives.py @ 602:d7b957e92ea9 trunk
Add runtime optimization hints for match templates.
author | cmlenz |
---|---|
date | Thu, 23 Aug 2007 11:35:43 +0000 |
parents | 59fbd7586454 |
children | 416e46209da1 |
comparison
equal
deleted
inserted
replaced
601:59fbd7586454 | 602:d7b957e92ea9 |
---|---|
12 # history and logs, available at http://genshi.edgewall.org/log/. | 12 # history and logs, available at http://genshi.edgewall.org/log/. |
13 | 13 |
14 """Implementation of the various template directives.""" | 14 """Implementation of the various template directives.""" |
15 | 15 |
16 import compiler | 16 import compiler |
17 try: | |
18 frozenset | |
19 except NameError: | |
20 from sets import ImmutableSet as frozenset | |
17 | 21 |
18 from genshi.core import QName, Stream | 22 from genshi.core import QName, Stream |
19 from genshi.path import Path | 23 from genshi.path import Path |
20 from genshi.template.base import TemplateRuntimeError, TemplateSyntaxError, \ | 24 from genshi.template.base import TemplateRuntimeError, TemplateSyntaxError, \ |
21 EXPR, _apply_directives | 25 EXPR, _apply_directives |
421 <span> | 425 <span> |
422 Hello Dude | 426 Hello Dude |
423 </span> | 427 </span> |
424 </div> | 428 </div> |
425 """ | 429 """ |
426 __slots__ = ['path', 'namespaces'] | 430 __slots__ = ['path', 'namespaces', 'hints'] |
427 | 431 |
428 def __init__(self, value, template, namespaces=None, lineno=-1, offset=-1): | 432 def __init__(self, value, template, hints=None, namespaces=None, |
433 lineno=-1, offset=-1): | |
429 Directive.__init__(self, None, template, namespaces, lineno, offset) | 434 Directive.__init__(self, None, template, namespaces, lineno, offset) |
430 self.path = Path(value, template.filepath, lineno) | 435 self.path = Path(value, template.filepath, lineno) |
431 self.namespaces = namespaces or {} | 436 self.namespaces = namespaces or {} |
432 | 437 self.hints = hints or () |
433 def attach(cls, template, stream, value, namespaces, pos): | 438 |
439 def attach(cls, template, stream, value, namespaces, pos): | |
440 hints = [] | |
434 if type(value) is dict: | 441 if type(value) is dict: |
442 if value.get('once', '').lower() == 'true': | |
443 hints.append('match_once') | |
444 if value.get('recursive', '').lower() == 'false': | |
445 hints.append('not_recursive') | |
435 value = value.get('path') | 446 value = value.get('path') |
436 return super(MatchDirective, cls).attach(template, stream, value, | 447 return cls(value, template, frozenset(hints), namespaces, *pos[1:]), \ |
437 namespaces, pos) | 448 stream |
438 attach = classmethod(attach) | 449 attach = classmethod(attach) |
439 | 450 |
440 def __call__(self, stream, ctxt, directives): | 451 def __call__(self, stream, ctxt, directives): |
441 ctxt._match_templates.append((self.path.test(ignore_context=True), | 452 ctxt._match_templates.append((self.path.test(ignore_context=True), |
442 self.path, list(stream), self.namespaces, | 453 self.path, list(stream), self.hints, |
443 directives)) | 454 self.namespaces, directives)) |
444 return [] | 455 return [] |
445 | 456 |
446 def __repr__(self): | 457 def __repr__(self): |
447 return '<%s "%s">' % (self.__class__.__name__, self.path.source) | 458 return '<%s "%s">' % (self.__class__.__name__, self.path.source) |
448 | 459 |