Changeset 568

Show
Ignore:
Timestamp:
07/12/08 06:26:09 (5 months ago)
Author:
athomas
Message:

Remove "xml_" prefix from a few functions, and unprotect some useful casting
functions.

Files:

Legend:

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

    r566 r568  
    479479 
    480480    @classmethod 
    481     def xml_cast_attribute(cls, name, value): 
     481    def cast_attribute(cls, name, value): 
    482482        """Define functions for casting attributes to their correct Python type. 
    483483 
     
    487487        """ 
    488488        casts = { 
    489             'traversals': int, 'group': _xml_group_type
    490             'order': int, 'match_candidates': _xml_boolean_type
    491             'with_context': _xml_boolean_type
     489            'traversals': int, 'group': group_cast
     490            'order': int, 'match_candidates': boolean_cast
     491            'with_context': boolean_cast
    492492            } 
    493493        if name in casts: 
     
    496496 
    497497    @classmethod 
    498     def xml_attribute_aliases(cls): 
     498    def attribute_aliases(cls): 
    499499        """Define attribute aliases for this node. 
    500500 
     
    591591 
    592592    @classmethod 
    593     def xml_cast_attribute(cls, name, value): 
     593    def cast_attribute(cls, name, value): 
    594594        return eval(value) 
    595595 
     
    605605 
    606606    @classmethod 
    607     def xml_cast_attribute(cls, name, value): 
     607    def cast_attribute(cls, name, value): 
    608608        if name == 'id': 
    609             return _xml_group_type(value) 
    610         return super(Group, cls).xml_cast_attribute(name, value) 
     609            return group_cast(value) 
     610        return super(Group, cls).cast_attribute(name, value) 
    611611 
    612612 
     
    809809 
    810810    @classmethod 
    811     def xml_attribute_aliases(cls): 
     811    def attribute_aliases(cls): 
    812812        return {'exec': 'callback'} 
    813813 
     
    10511051        aliases = {} 
    10521052        for c in reversed(cls.mro()): 
    1053             if 'xml_attribute_aliases' in c.__dict__: 
    1054                 aliases.update(c.xml_attribute_aliases()) 
     1053            if 'attribute_aliases' in c.__dict__: 
     1054                aliases.update(c.attribute_aliases()) 
    10551055 
    10561056        attributes = dict([(str(aliases.get(k, k)), v) for k, v 
     
    10591059        for k, v in attributes.items(): 
    10601060            # Do type conversion 
    1061             v = cls.xml_cast_attribute(k, v) 
     1061            v = cls.cast_attribute(k, v) 
    10621062            # Is the destination Node attribute callable? Do a lazy eval. 
    10631063            if callable(getattr(cls, k, None)): 
     
    10741074                else: 
    10751075                    args = [] 
    1076                 v = _xml_lazy_attr_evaluator(v, args) 
     1076                v = lazy_attr_evaluator(v, args) 
    10771077 
    10781078            attributes[k] = v 
     
    10801080 
    10811081 
    1082 def _xml_lazy_attr_evaluator(attr, positional_args=None): 
     1082def lazy_attr_evaluator(attr, positional_args=None): 
    10831083    """Return a callable that lazily evaluates an expression. 
    10841084 
     
    10891089    # Extract positional arguments from function object 
    10901090    positional_args = positional_args or [] 
    1091     def xml_attr_evaluator(*args, **kwargs): 
     1091    def attr_evaluator(*args, **kwargs): 
    10921092        locals = dict(kwargs) 
    10931093 
     
    11241124            e.args = e.args + ('while parsing "%s"' % attr,) 
    11251125            raise 
    1126     return xml_attr_evaluator 
    1127  
    1128  
    1129 def _xml_boolean_type(value): 
     1126    return attr_evaluator 
     1127 
     1128 
     1129def boolean_cast(value): 
    11301130    """Converter for boolean attributes.""" 
    11311131    return str(value).lower() in ('true', '1', 'yes') 
    11321132 
    11331133 
    1134 def _xml_group_type(value): 
     1134def group_cast(value): 
    11351135    """Parse a group="n" attribute.""" 
    11361136    try: 
     
    14391439 
    14401440    @classmethod 
    1441     def xml_cast_attribute(cls, name, value): 
     1441    def cast_attribute(cls, name, value): 
    14421442        if name == 'parts': 
    14431443            return int(value) 
    1444         return super(Hostname, cls).xml_cast_attribute(name, value) 
     1444        return super(Hostname, cls).cast_attribute(name, value) 
    14451445 
    14461446 
  • cly/trunk/cly/test.py

    r558 r568  
    160160        self.assertEqual(self._output, (('hello',), {})) 
    161161 
    162     def test_xml_cast_attribute(self): 
     162    def test_cast_attribute(self): 
    163163        class Test(Node): 
    164164            @classmethod 
    165             def xml_cast_attribute(cls, name, value): 
     165            def cast_attribute(cls, name, value): 
    166166                if name == 'test': 
    167167                    return int(value) 
     
    177177        self.assertTrue(isinstance(grammar.find('/test').test, int)) 
    178178 
    179     def test_xml_attribute_aliases(self): 
     179    def test_attribute_aliases(self): 
    180180        class Parent(Node): 
    181181            @classmethod 
    182             def xml_attribute_aliases(cls): 
     182            def attribute_aliases(cls): 
    183183                return {'foo': 'bar'} 
    184184 
    185185        class Test(Parent): 
    186186            @classmethod 
    187             def xml_attribute_aliases(cls): 
     187            def attribute_aliases(cls): 
    188188                return {'baz': 'waz'} 
    189189