comparison genshi/path.py @ 330:1dbeaef463e7

XPath tests should never return event tuples, just values or booleans.
author cmlenz
date Mon, 06 Nov 2006 18:28:23 +0000
parents 08ada6b4b767
children b70563ade748
comparison
equal deleted inserted replaced
326:08ada6b4b767 330:1dbeaef463e7
119 test = self.test() 119 test = self.test()
120 for event in stream: 120 for event in stream:
121 result = test(event, namespaces, variables) 121 result = test(event, namespaces, variables)
122 if result is True: 122 if result is True:
123 yield event 123 yield event
124 depth = 1 124 if event[0] is START:
125 while depth > 0: 125 depth = 1
126 subevent = stream.next() 126 while depth > 0:
127 if subevent[0] is START: 127 subevent = stream.next()
128 depth += 1 128 if subevent[0] is START:
129 elif subevent[0] is END: 129 depth += 1
130 depth -= 1 130 elif subevent[0] is END:
131 yield subevent 131 depth -= 1
132 test(subevent, namespaces, variables, updateonly=True) 132 yield subevent
133 test(subevent, namespaces, variables,
134 updateonly=True)
133 elif result: 135 elif result:
134 yield result 136 yield result
135 return Stream(_generate()) 137 return Stream(_generate())
136 138
137 def test(self, ignore_context=False): 139 def test(self, ignore_context=False):
158 ('START', (QName(u'child'), Attrs([(QName(u'id'), u'2')])), (None, 1, 34)) 160 ('START', (QName(u'child'), Attrs([(QName(u'id'), u'2')])), (None, 1, 34))
159 """ 161 """
160 paths = [(p, len(p), [0], [], [0] * len(p)) for p in self.paths] 162 paths = [(p, len(p), [0], [], [0] * len(p)) for p in self.paths]
161 163
162 def _test(event, namespaces, variables, updateonly=False): 164 def _test(event, namespaces, variables, updateonly=False):
163 kind, data, pos = event 165 kind, data, pos = event[:3]
164 retval = None 166 retval = None
165 for steps, size, cursors, cutoff, counter in paths: 167 for steps, size, cursors, cutoff, counter in paths:
166 # Manage the stack that tells us "where we are" in the stream 168 # Manage the stack that tells us "where we are" in the stream
167 if kind is END: 169 if kind is END:
168 if cursors: 170 if cursors:
590 592
591 class CommentNodeTest(object): 593 class CommentNodeTest(object):
592 """Node test that matches any comment events.""" 594 """Node test that matches any comment events."""
593 __slots__ = [] 595 __slots__ = []
594 def __call__(self, kind, data, pos, namespaces, variables): 596 def __call__(self, kind, data, pos, namespaces, variables):
595 return kind is COMMENT and (kind, data, pos) 597 return kind is COMMENT
596 def __repr__(self): 598 def __repr__(self):
597 return 'comment()' 599 return 'comment()'
598 600
599 class NodeTest(object): 601 class NodeTest(object):
600 """Node test that matches any node.""" 602 """Node test that matches any node."""
610 """Node test that matches any processing instruction event.""" 612 """Node test that matches any processing instruction event."""
611 __slots__ = ['target'] 613 __slots__ = ['target']
612 def __init__(self, target=None): 614 def __init__(self, target=None):
613 self.target = target 615 self.target = target
614 def __call__(self, kind, data, pos, namespaces, variables): 616 def __call__(self, kind, data, pos, namespaces, variables):
615 if kind is PI and (not self.target or data[0] == self.target): 617 return kind is PI and (not self.target or data[0] == self.target)
616 return (kind, data, pos)
617 def __repr__(self): 618 def __repr__(self):
618 arg = '' 619 arg = ''
619 if self.target: 620 if self.target:
620 arg = '"' + self.target + '"' 621 arg = '"' + self.target + '"'
621 return 'processing-instruction(%s)' % arg 622 return 'processing-instruction(%s)' % arg
622 623
623 class TextNodeTest(object): 624 class TextNodeTest(object):
624 """Node test that matches any text event.""" 625 """Node test that matches any text event."""
625 __slots__ = [] 626 __slots__ = []
626 def __call__(self, kind, data, pos, namespaces, variables): 627 def __call__(self, kind, data, pos, namespaces, variables):
627 return kind is TEXT and (kind, data, pos) 628 return kind is TEXT
628 def __repr__(self): 629 def __repr__(self):
629 return 'text()' 630 return 'text()'
630 631
631 _nodetest_map = {'comment': CommentNodeTest, 'node': NodeTest, 632 _nodetest_map = {'comment': CommentNodeTest, 'node': NodeTest,
632 'processing-instruction': ProcessingInstructionNodeTest, 633 'processing-instruction': ProcessingInstructionNodeTest,
Copyright (C) 2012-2017 Edgewall Software