Changeset 4603

Show
Ignore:
Timestamp:
04/29/09 03:12:17 (4 years ago)
Author:
getxsick
Message:

refactoring and bug fixes for Protocol class
- remove validation checking for getattr and setattr

(it's checked anyway by calling get_field())

- better arg checking for set_fields:

+ parity of *args
+ validation of keys before transaction have begun

- rewrite get_flags() internally
- parity check for set_flags()
- bug fixing for concat strings with other objects
- bug fixing for self.name which doesn't work because of getattr()
- replace of some methods in the class structure

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branch/UMPA/umpa/protocols/_protocols.py

    r4598 r4603  
    8484        """ 
    8585         
    86         if self._is_valid(attr): 
    87             return self.get_field(attr).get() 
    88         else: 
    89             raise UMPAAttributeException(attr + ' is not allowed') 
     86        return self.get_field(attr).get() 
    9087 
    9188    def __setattr__(self, attr, value): 
     
    9996        """ 
    10097 
    101         if self._is_valid(attr): 
    102             self.get_field(attr).set(value) 
    103         else: 
    104             raise UMPAAttributeException(attr + ' is not allowed') 
     98        self.get_field(attr).set(value) 
    10599 
    106100    def __str__(self): 
     
    121115        return super(Protocol, self).__str__() 
    122116 
     117    def _is_valid(self, field): 
     118        """ 
     119        Validate if the filed is allowed. 
     120 
     121        @param field: requested field. 
     122 
     123        @rtype: C{bool} 
     124        @return: result of the validation. 
     125        """ 
     126 
     127        return field in self._fields 
     128 
    123129    @classmethod 
    124130    def get_fields_keys(cls): 
     
    171177        """ 
    172178         
     179        if len(args) % 2: 
     180            raise UMPAAttributeException('wrong amount of passed arguments') 
    173181        # converting args list to the dict and update our kwargs 
    174182        kwargs.update(_dict_from_sequence(args)) 
    175183 
     184        # first check if all keys are valid to avoid partial-changing 
    176185        for key in kwargs: 
    177             if self._is_valid(key): 
    178                 setattr(self, key, kwargs[key]) 
     186            if not self._is_valid(key): 
     187                raise UMPAAttributeException(key + ' is not allowed; ' 
     188                                            'nothing has changed') 
     189 
     190        for key in kwargs: 
     191            setattr(self, key, kwargs[key]) 
     192 
     193    def get_flags(self, name, *args): 
     194        """ 
     195        Return flags of the field. 
     196 
     197        @type name: C{str} 
     198        @param name: name of the field. 
     199 
     200        @param args: names of flags. 
     201 
     202        @rtype: C{list} 
     203        @return: list of flags. 
     204        """ 
     205 
     206        flag_field = self.get_field(name) 
     207        if not isinstance(flag_field, Flags): 
     208            raise UMPAAttributeException("No Flags instance for " + name) 
     209        return flag_field.get(*args) 
    179210 
    180211    def set_flags(self, name, *args, **kwargs): 
     
    194225        """ 
    195226 
     227        if len(args) % 2: 
     228            raise UMPAAttributeException('wrong amount of passed arguments') 
    196229        # converting args list to the dict and update our kwargs 
    197230        kwargs.update(_dict_from_sequence(args)) 
     
    207240            raise UMPAAttributeException("No Flags instance for " + name) 
    208241 
    209     def get_flags(self, name, *args): 
    210         """ 
    211         Return flags of the field. 
    212  
    213         @type name: C{str} 
    214         @param name: name of the field. 
    215  
    216         @param args: names of flags. 
    217  
    218         @rtype: C{list} 
    219         @return: list of flags. 
    220         """ 
    221  
    222         flag_field = self.get_field(name) 
    223         if isinstance(flag_field, Flags): 
    224             return flag_field.get(*args) 
    225         else: 
    226             raise UMPAAttributeException("No Flags instance for " + name) 
    227  
    228242    def get_offset(self, field): 
    229243        """ 
     
    245259            field_list = [ f for f in self.get_fields() ] 
    246260        else: 
    247             raise UMPAException(type(field) + ' unsupported') 
     261            raise UMPAException(str(type(field)) + ' unsupported') 
    248262     
    249263        if field not in field_list: 
    250             raise UMPAAttributeException(field + ' is not allowed') 
     264            raise UMPAAttributeException(repr(field) + ' is not allowed') 
    251265 
    252266        offset = 0 
     
    380394        # protocol should return byte-compatible length 
    381395        if bit % BYTE != 0: 
    382             raise UMPAException('odd number of bits in ' + self.__name__) 
     396            raise UMPAException('odd number of bits in ' + str(self.name)) 
    383397 
    384398        self.__dict__['__raw_value'] = raw_value 
    385399        return raw_value, bit 
    386  
    387     def _is_valid(self, field): 
    388         """ 
    389         Validate if the filed is allowed. 
    390  
    391         @param field: requested field. 
    392  
    393         @rtype: C{bool} 
    394         @return: result of the validation. 
    395         """ 
    396  
    397         return field in self._fields