Changeset 558

Show
Ignore:
Timestamp:
07/10/08 10:55:15 (5 months ago)
Author:
athomas
Message:
  • s/Set/Defaults/
  • Added unit tests for Defaults.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • cly/trunk/cly/builder.py

    r557 r558  
    3030 
    3131__all__ = [ 
    32     'Node', 'Masquerade', 'Set', 'Alias', 'Group', 'If', 'Apply', 'Action', 
     32    'Node', 'Masquerade', 'Defaults', 'Alias', 'Group', 'If', 'Apply', 'Action', 
    3333    'Variable', 'Grammar', 'XMLGrammar', 'Help', 'LazyHelp', 'Word', 'Keyword', 
    3434    'String', 'URI', 'LDAPDN', 'Integer', 'Float', 'IP', 'Hostname', 'Host', 
     
    566566 
    567567 
    568 class Set(Masquerade): 
     568class Defaults(Masquerade): 
    569569    """Set variables in a branch. 
    570570 
     571    This is primarily useful in XML grammars. 
     572 
    571573    >>> 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()))) 
    573575    >>> parser.parse('baz').vars 
    574576    {'foo': 10, 'bar': 20} 
     577 
     578    In an XML grammar, all attributes are evaluated. This means that strings 
     579    must be double quoted. 
    575580    """ 
    576581    vars = '' 
     
    578583 
    579584    def __init__(self, **kwargs): 
    580         super(Set, self).__init__() 
     585        super(Defaults, self).__init__() 
    581586        self.vars = kwargs 
    582587 
    583588    def follow(self, context): 
    584589        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) 
    586595 
    587596 
  • cly/trunk/cly/test.py

    r557 r558  
    1111from StringIO import StringIO 
    1212from cly.exceptions import InvalidToken 
    13 from cly import Node, XMLGrammar, Parser 
     13from cly import Defaults, Node, XMLGrammar, Parser 
    1414 
    1515 
     
    199199        self.assertTrue(node.waz, '20') 
    200200 
     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'}) 
    201212 
    202213def suite():