Changeset 3128

Show
Ignore:
Timestamp:
07/10/08 16:21:22 (5 years ago)
Author:
getxsick
Message:

Refactoring of protocol API.
No more need to use 2-lists. Also ordered-dict idea is rejected.
Instead there are a tuple with names and related to the tuple dict.

get_item_by_name() from util module is removed.

Location:
branch/UMPA/umpa
Files:
4 modified

Legend:

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

    r3125 r3128  
    2222import base 
    2323from umpa.utils.my_exceptions import UMPAAttributeException 
    24 from umpa.utils import get_item_by_name 
    2524 
    2625class HVersion(base.Field): 
     
    8382 
    8483class IP(base.Protocol): 
    85     valid_fields = ['_version', '_ihl', 'type_of_service', '_total_length', 
    86                     'identification', 'flags', '_fragment_offset', 'time_to_live', 
    87                     'protocol', '_header_checksum', 'source_address', 
    88                     'destination_address', 'options', '_padding',] 
     84    _ordered_fields = ('_version', '_ihl', 'type_of_service', '_total_length', 
     85                        'identification', 'flags', '_fragment_offset', 
     86                        'time_to_live', 'protocol', '_header_checksum', 
     87                        'source_address', 'destination_address', 'options', 
     88                        '_padding',) 
    8989 
    9090    def __init__(self, **kw): 
    91         base.Protocol.__init__(self,kw) 
     91        base.Protocol.__init__(self, kw) 
    9292 
    93         # attributes listed below shouldn't be modifed by user 
    94         # they will be generated automatically 
    95         self._fields = [ HVersion(4, True), HIHL(4, True), 
     93        fields_list = [ HVersion(4, True), HIHL(4, True), 
    9694                        HTypeOfService(8), HTotalLength(16, True), 
    9795                        HIdentification(16, True), HFlags(3, True), # add names to flags later 
     
    10199                        HOptions(0), HPadding(0, True), ] 
    102100 
     101        # we pack objects of header's fields to the dict 
     102        self._fields = dict(zip(self._ordered_list, fields_list)) 
     103 
    103104        # setting up passed fields 
    104105        for field in kw: 
     
    107108    def _is_valid(self, name): 
    108109        """Check if attribute is allowed.""" 
    109         if name in valid_fields: 
    110             return True 
    111         return False 
     110        return self._fields.has_key(name) 
    112111 
    113112    def __setattr__(self, attr, val): 
    114113        """Set value of the field.""" 
     114 
     115        # we can do the same without _is_valid() with just try/except section 
     116        # but Francesco asked me about this method 
    115117        if self._is_valid(attr): 
    116             get_item_by_name(self._fields, self.valid_fields, attr).set(val) 
     118            self._fields[attr].set(val) 
    117119        else: 
    118120            raise UMPAAttributeException, attr + ' not allowed' 
     
    121123        """Return value of the field.""" 
    122124        if self._is_valid(attr): 
    123             return get_item_by_name(self._fields, self.valid_fields, 
    124                                     attr).get() 
     125            return self._fields[attr].get() 
    125126        else: 
    126127            raise UMPAAttributeException, attr + ' not allowed' 
  • branch/UMPA/umpa/protocols/base.py

    r3125 r3128  
    3333 
    3434    def set(self, value): 
    35         if self.is_valid(value): 
    36             self.value = value 
     35        if self._is_valid(value): 
     36            self._value = value 
     37        else: 
     38            raise 
    3739 
    3840    def get(self): 
    39         return self.value 
     41        return self._value 
    4042 
    41     def is_valid(self, val): 
     43    def _is_valid(self, val): 
    4244        """Should be overload by sub-classes. 
    4345 
     
    5658        # we overwrite an attribute self._value 
    5759        # because we need a list instead of simple var here 
    58         self._value = [] 
    59         self.valid_fields = list(names) 
     60        self._value = {} 
     61        self._ordered_fields = list(names) 
    6062 
    61     def is_valid(self, val): 
    62         if val in self.valid_fields: 
    63             return True 
    64         else: 
    65             return False 
     63    def _is_valid(self, name): 
     64        return self._value.has_key(name) 
    6665 
    6766    def set(self, kw): 
    6867        for flag_name in kw: 
    69             if self.is_valid(self, flag_name): 
    70                 utils.get_item_by_name(self._value, self.valid_fields, 
    71                                                     flag_name) = kw[flag_name] 
     68            if self._is_valid(flag_name): 
     69                self._value[flag_name] = kw[flag_name] 
     70            else: 
     71                raise UMPAAttributeException, attr + ' not allowed' 
    7272 
    7373    def get(self, *names): 
    7474        # we check if name of the field in the flag is correct 
    75         result = [ utils.get_item_by_name(self._value, self.valid_fields, val) 
    76                 for val in names if val in self.valid_fields ] 
     75        result = [ self._value[val] for val in name if self._is_valid(val) ] 
     76 
    7777        # if no results above we return whole list of values 
    7878        if len(result) < 1: 
     
    8181 
    8282class Protocol(object): 
     83    _ordered_fields = () 
     84 
    8385    def __init__(self, **kw): 
    84         # TODO 
    85         # ok, there is an ugly implementation of this. 
    86         # because there isn't ordered dict type. 
    87         # so the fact is, that we use 2 lists 
    88         # first with objects (fields) 
    89         # and second with valid names of objects 
    90  
    91         # there is some implementation in PEP372 
    92         # and it should be implemtented 
    93  
    94         # also there is other wrong now, because this mechanism isn't 
    95         # only this class but also in sub-classes 
    96         # it means that it spreads out and also means about bad API design 
    97         self._fields = [] 
     86        self._fields = {} 
    9887    # XXX chyba trzeba dodac jakies get flags czy cos 
    9988    def set_fields(self, *args, **kwargs): 
  • branch/UMPA/umpa/utils/__init__.py

    r3124 r3128  
    3030    """Return a dictionary based on a sequence.""" 
    3131    return dict(_pairwise(seq)) 
    32  
    33 def get_item_by_name(seq, seq_with_name, name): 
    34     """Return an item from the sequence based on the another sequence 
    35     and position of the name there. 
    36     """ 
    37     return seq[seq_with_name.index(name)] 
  • branch/UMPA/umpa/utils/my_exceptions.py

    r3122 r3128  
    2828class UMPAAttributeException(UMPAException): 
    2929    pass 
     30 
     31class UMPAAssignException(UMPAException): 
     32    pass