本文整理汇总了Python中genshi.path.Path类的典型用法代码示例。如果您正苦于以下问题:Python Path类的具体用法?Python Path怎么用?Python Path使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Path类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_wildcard_with_namespace
def test_wildcard_with_namespace(self):
xml = XML('<root xmlns:f="FOO"><f:foo>bar</f:foo></root>')
path = Path('f:*')
self.assertEqual('<Path "child::f:*">', repr(path))
namespaces = {'f': 'FOO'}
self.assertEqual('<foo xmlns="FOO">bar</foo>',
path.select(xml, namespaces=namespaces).render())
示例2: test_predicate_attr_equality
def test_predicate_attr_equality(self):
xml = XML('<root><item/><item important="notso"/></root>')
path = Path('item[@important="very"]')
self.assertEqual('', path.select(xml).render())
path = Path('item[@important!="very"]')
self.assertEqual('<item/><item important="notso"/>',
path.select(xml).render())
示例3: test_attr_selection
def test_attr_selection(self):
xml = XML('<root><foo bar="abc"></foo></root>')
path = Path('foo/@bar')
result = path.select(xml)
self.assertEqual(list(result), [
Attrs([(QName('bar'), u'abc')])
])
示例4: test_1step_self
def test_1step_self(self):
xml = XML('<root><elem/></root>')
path = Path('.')
self.assertEqual('<Path "self::node()">', repr(path))
self.assertEqual('<root><elem/></root>', path.select(xml).render())
path = Path('self::node()')
self.assertEqual('<Path "self::node()">', repr(path))
self.assertEqual('<root><elem/></root>', path.select(xml).render())
示例5: test_attr_selection_with_namespace
def test_attr_selection_with_namespace(self):
xml = XML(
'<root xmlns:ns1="http://example.com">'
'<foo ns1:bar="abc"></foo>'
'</root>')
path = Path('foo/@ns1:bar')
result = path.select(xml, namespaces={'ns1': 'http://example.com'})
self.assertEqual(list(result), [
Attrs([(QName('http://example.com}bar'), u'abc')])
])
示例6: test_predicate_termination
def test_predicate_termination(self):
"""
Verify that a patch matching the self axis with a predicate doesn't
cause an infinite loop. See <http://genshi.edgewall.org/ticket/82>.
"""
xml = XML('<ul flag="1"><li>a</li><li>b</li></ul>')
path = Path('.[@flag="1"]/*')
self.assertEqual('<li>a</li><li>b</li>', path.select(xml).render())
xml = XML('<ul flag="1"><li>a</li><li>b</li></ul>')
path = Path('.[@flag="0"]/*')
self.assertEqual('', path.select(xml).render())
示例7: test_3step_complex
def test_3step_complex(self):
xml = XML('<root><foo><bar/></foo></root>')
path = Path('*/bar')
self.assertEqual('<Path "child::*/child::bar">', repr(path))
self.assertEqual('<bar/>', path.select(xml).render())
xml = XML('<root><foo><bar id="1"/></foo><bar id="2"/></root>')
path = Path('//bar')
self.assertEqual('<Path "descendant-or-self::node()/child::bar">',
repr(path))
self.assertEqual('<bar id="1"/><bar id="2"/>',
path.select(xml).render())
示例8: test_1step_attribute
def test_1step_attribute(self):
path = Path('@foo')
self.assertEqual('<Path "attribute::foo">', repr(path))
xml = XML('<root/>')
self.assertEqual('', path.select(xml).render())
xml = XML('<root foo="bar"/>')
self.assertEqual('bar', path.select(xml).render())
path = Path('./@foo')
self.assertEqual('<Path "self::node()/attribute::foo">', repr(path))
self.assertEqual('bar', path.select(xml).render())
示例9: test_node_type_processing_instruction
def test_node_type_processing_instruction(self):
xml = XML('<?python x = 2 * 3 ?><root><?php echo("x") ?></root>')
path = Path('processing-instruction()')
self.assertEqual('<Path "child::processing-instruction()">',
repr(path))
self.assertEqual('<?python x = 2 * 3 ?><?php echo("x") ?>',
path.select(xml).render())
path = Path('processing-instruction("php")')
self.assertEqual('<Path "child::processing-instruction(\"php\")">',
repr(path))
self.assertEqual('<?php echo("x") ?>', path.select(xml).render())
示例10: MatchDirective
class MatchDirective(Directive):
"""Implementation of the ``py:match`` template directive.
>>> from genshi.template import MarkupTemplate
>>> tmpl = MarkupTemplate('''<div xmlns:py="http://genshi.edgewall.org/">
... <span py:match="greeting">
... Hello ${select('@name')}
... </span>
... <greeting name="Dude" />
... </div>''')
>>> print tmpl.generate()
<div>
<span>
Hello Dude
</span>
</div>
"""
__slots__ = ['path', 'namespaces']
ATTRIBUTE = 'path'
def __init__(self, value, template, namespaces=None, lineno=-1, offset=-1):
Directive.__init__(self, None, template, namespaces, lineno, offset)
self.path = Path(value, template.filepath, lineno)
self.namespaces = namespaces or {}
def __call__(self, stream, ctxt, directives):
ctxt._match_templates.append((self.path.test(ignore_context=True),
self.path, list(stream), self.namespaces,
directives))
return []
def __repr__(self):
return '<%s "%s">' % (self.__class__.__name__, self.path.source)
示例11: _test_eval
def _test_eval(self, path, equiv=None, input=None, output='',
namespaces=None, variables=None):
path = Path(path)
if equiv is not None:
self.assertEqual(equiv, repr(path))
if input is None:
return
rendered = path.select(input, namespaces=namespaces,
variables=variables).render(encoding=None)
msg = 'Bad output using whole path'
msg += '\nExpected:\t%r' % output
msg += '\nRendered:\t%r' % rendered
self.assertEqual(output, rendered, msg)
if len(path.paths) == 1:
self._test_strategies(input, path.paths[0], output,
namespaces=namespaces, variables=variables)
示例12: test_predicate_number_function
def test_predicate_number_function(self):
xml = XML('<root><foo>bar</foo></root>')
path = Path('*[number("3.0")=3]')
self.assertEqual('<foo>bar</foo>', path.select(xml).render())
path = Path('*[number("3.0")=3.0]')
self.assertEqual('<foo>bar</foo>', path.select(xml).render())
path = Path('*[number("0.1")=.1]')
self.assertEqual('<foo>bar</foo>', path.select(xml).render())
示例13: MatchDirective
class MatchDirective(Directive):
"""Implementation of the ``py:match`` template directive.
>>> from genshi.template import MarkupTemplate
>>> tmpl = MarkupTemplate('''<div xmlns:py="http://genshi.edgewall.org/">
... <span py:match="greeting">
... Hello ${select('@name')}
... </span>
... <greeting name="Dude" />
... </div>''')
>>> print(tmpl.generate())
<div>
<span>
Hello Dude
</span>
</div>
"""
__slots__ = ['path', 'namespaces', 'hints']
def __init__(self, value, template, hints=None, namespaces=None,
lineno=-1, offset=-1):
Directive.__init__(self, None, template, namespaces, lineno, offset)
self.path = Path(value, template.filepath, lineno)
self.namespaces = namespaces or {}
self.hints = hints or ()
@classmethod
def attach(cls, template, stream, value, namespaces, pos):
hints = []
if type(value) is dict:
if value.get('buffer', '').lower() == 'false':
hints.append('not_buffered')
if value.get('once', '').lower() == 'true':
hints.append('match_once')
if value.get('recursive', '').lower() == 'false':
hints.append('not_recursive')
value = value.get('path')
return cls(value, template, frozenset(hints), namespaces, *pos[1:]), \
stream
def __call__(self, stream, directives, ctxt, **vars):
ctxt._match_templates.append((self.path.test(ignore_context=True),
self.path, list(stream), self.hints,
self.namespaces, directives))
return []
def __repr__(self):
return '<%s "%s">' % (type(self).__name__, self.path.source)
示例14: test_1step
def test_1step(self):
xml = XML('<root><elem/></root>')
path = Path('elem')
self.assertEqual('<Path "child::elem">', repr(path))
self.assertEqual('<elem/>', path.select(xml).render())
path = Path('child::elem')
self.assertEqual('<Path "child::elem">', repr(path))
self.assertEqual('<elem/>', path.select(xml).render())
path = Path('//elem')
self.assertEqual('<Path "descendant-or-self::node()/child::elem">',
repr(path))
self.assertEqual('<elem/>', path.select(xml).render())
path = Path('descendant::elem')
self.assertEqual('<Path "descendant::elem">', repr(path))
self.assertEqual('<elem/>', path.select(xml).render())
示例15: test_2step_complex
def test_2step_complex(self):
xml = XML('<root><foo><bar/></foo></root>')
path = Path('foo/bar')
self.assertEqual('<Path "child::foo/child::bar">', repr(path))
self.assertEqual('<bar/>', path.select(xml).render())
path = Path('./bar')
self.assertEqual('<Path "self::node()/child::bar">', repr(path))
self.assertEqual('', path.select(xml).render())
path = Path('foo/*')
self.assertEqual('<Path "child::foo/child::*">', repr(path))
self.assertEqual('<bar/>', path.select(xml).render())
xml = XML('<root><foo><bar id="1"/></foo><bar id="2"/></root>')
path = Path('./bar')
self.assertEqual('<Path "self::node()/child::bar">', repr(path))
self.assertEqual('<bar id="2"/>', path.select(xml).render())