Changeset 558
- Timestamp:
- 07/10/08 10:55:15 (5 months ago)
- Files:
-
- cly/trunk/cly/builder.py (modified) (3 diffs)
- cly/trunk/cly/test.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
cly/trunk/cly/builder.py
r557 r558 30 30 31 31 __all__ = [ 32 'Node', 'Masquerade', ' Set', 'Alias', 'Group', 'If', 'Apply', 'Action',32 'Node', 'Masquerade', 'Defaults', 'Alias', 'Group', 'If', 'Apply', 'Action', 33 33 'Variable', 'Grammar', 'XMLGrammar', 'Help', 'LazyHelp', 'Word', 'Keyword', 34 34 'String', 'URI', 'LDAPDN', 'Integer', 'Float', 'IP', 'Hostname', 'Host', … … 566 566 567 567 568 class Set(Masquerade):568 class Defaults(Masquerade): 569 569 """Set variables in a branch. 570 570 571 This is primarily useful in XML grammars. 572 571 573 >>> from cly import * 572 >>> parser = Parser(Grammar( Set(foo=10, bar=20)(baz=Node())))574 >>> parser = Parser(Grammar(Defaults(foo=10, bar=20)(baz=Node()))) 573 575 >>> parser.parse('baz').vars 574 576 {'foo': 10, 'bar': 20} 577 578 In an XML grammar, all attributes are evaluated. This means that strings 579 must be double quoted. 575 580 """ 576 581 vars = '' … … 578 583 579 584 def __init__(self, **kwargs): 580 super( Set, self).__init__()585 super(Defaults, self).__init__() 581 586 self.vars = kwargs 582 587 583 588 def follow(self, context): 584 589 context.vars.update(self.vars) 585 return super(Set, self).follow(context) 590 return super(Defaults, self).follow(context) 591 592 @classmethod 593 def xml_cast_attribute(cls, name, value): 594 return eval(value) 586 595 587 596 cly/trunk/cly/test.py
r557 r558 11 11 from StringIO import StringIO 12 12 from cly.exceptions import InvalidToken 13 from cly import Node, XMLGrammar, Parser13 from cly import Defaults, Node, XMLGrammar, Parser 14 14 15 15 … … 199 199 self.assertTrue(node.waz, '20') 200 200 201 def test_set_cast(self): 202 xml = StringIO("""<?xml version="1.0"?> 203 <grammar> 204 <defaults baz="10" foo="20" waz="'waz'"> 205 <node name="test"></node> 206 </defaults> 207 </grammar> 208 """) 209 parser = Parser(XMLGrammar(xml)) 210 self.assertEqual(parser.parse('test').vars, 211 {'foo': 20, 'baz': 10, 'waz': 'waz'}) 201 212 202 213 def suite():
