comparison markup/template.py @ 111:2368c3becc52 trunk

Some fixes and more unit tests for the XPath engine.
author cmlenz
date Mon, 31 Jul 2006 17:25:43 +0000
parents f12e7987d7f4
children c77c113846d6
comparison
equal deleted inserted replaced
110:64ff134868c4 111:2368c3becc52
24 import posixpath 24 import posixpath
25 import re 25 import re
26 from StringIO import StringIO 26 from StringIO import StringIO
27 27
28 from markup.core import Attributes, Namespace, Stream, StreamEventKind 28 from markup.core import Attributes, Namespace, Stream, StreamEventKind
29 from markup.core import START, END, START_NS, END_NS, TEXT, COMMENT 29 from markup.core import _ensure, START, END, START_NS, END_NS, TEXT, COMMENT
30 from markup.eval import Expression 30 from markup.eval import Expression
31 from markup.input import XMLParser 31 from markup.input import XMLParser
32 from markup.path import Path 32 from markup.path import Path
33 33
34 __all__ = ['Context', 'BadDirectiveError', 'TemplateError', 34 __all__ = ['Context', 'BadDirectiveError', 'TemplateError',
846 stream = self.stream 846 stream = self.stream
847 for filter_ in [self._eval, self._match, self._flatten] + self.filters: 847 for filter_ in [self._eval, self._match, self._flatten] + self.filters:
848 stream = filter_(iter(stream), ctxt) 848 stream = filter_(iter(stream), ctxt)
849 return Stream(stream) 849 return Stream(stream)
850 850
851 def _ensure(self, stream, ctxt=None):
852 """Ensure that every item on the stream is actually a markup event."""
853 for event in stream:
854 try:
855 kind, data, pos = event
856 except ValueError:
857 kind, data, pos = event.totuple()
858 yield kind, data, pos
859
860 def _eval(self, stream, ctxt=None): 851 def _eval(self, stream, ctxt=None):
861 """Internal stream filter that evaluates any expressions in `START` and 852 """Internal stream filter that evaluates any expressions in `START` and
862 `TEXT` events. 853 `TEXT` events.
863 """ 854 """
864 filters = (self._ensure, self._eval, self._match) 855 filters = (self._eval, self._match)
865 856
866 for kind, data, pos in stream: 857 for kind, data, pos in stream:
867 858
868 if kind is START and data[1]: 859 if kind is START and data[1]:
869 # Attributes may still contain expressions in start tags at 860 # Attributes may still contain expressions in start tags at
898 yield TEXT, result, pos 889 yield TEXT, result, pos
899 else: 890 else:
900 # Test if the expression evaluated to an iterable, in which 891 # Test if the expression evaluated to an iterable, in which
901 # case we yield the individual items 892 # case we yield the individual items
902 try: 893 try:
903 substream = iter(result) 894 substream = _ensure(result)
904 for filter_ in filters: 895 for filter_ in filters:
905 substream = filter_(substream, ctxt) 896 substream = filter_(substream, ctxt)
906 for event in substream: 897 for event in substream:
907 yield event 898 yield event
908 except TypeError: 899 except TypeError:
Copyright (C) 2012-2017 Edgewall Software