Changeset 3128
- Timestamp:
- 07/10/08 16:21:22 (5 years ago)
- Location:
- branch/UMPA/umpa
- Files:
-
- 4 modified
-
protocols/IP.py (modified) (5 diffs)
-
protocols/base.py (modified) (3 diffs)
-
utils/__init__.py (modified) (1 diff)
-
utils/my_exceptions.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branch/UMPA/umpa/protocols/IP.py
r3125 r3128 22 22 import base 23 23 from umpa.utils.my_exceptions import UMPAAttributeException 24 from umpa.utils import get_item_by_name25 24 26 25 class HVersion(base.Field): … … 83 82 84 83 class 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',) 89 89 90 90 def __init__(self, **kw): 91 base.Protocol.__init__(self, kw)91 base.Protocol.__init__(self, kw) 92 92 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), 96 94 HTypeOfService(8), HTotalLength(16, True), 97 95 HIdentification(16, True), HFlags(3, True), # add names to flags later … … 101 99 HOptions(0), HPadding(0, True), ] 102 100 101 # we pack objects of header's fields to the dict 102 self._fields = dict(zip(self._ordered_list, fields_list)) 103 103 104 # setting up passed fields 104 105 for field in kw: … … 107 108 def _is_valid(self, name): 108 109 """Check if attribute is allowed.""" 109 if name in valid_fields: 110 return True 111 return False 110 return self._fields.has_key(name) 112 111 113 112 def __setattr__(self, attr, val): 114 113 """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 115 117 if self._is_valid(attr): 116 get_item_by_name(self._fields, self.valid_fields, attr).set(val)118 self._fields[attr].set(val) 117 119 else: 118 120 raise UMPAAttributeException, attr + ' not allowed' … … 121 123 """Return value of the field.""" 122 124 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() 125 126 else: 126 127 raise UMPAAttributeException, attr + ' not allowed' -
branch/UMPA/umpa/protocols/base.py
r3125 r3128 33 33 34 34 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 37 39 38 40 def get(self): 39 return self. value41 return self._value 40 42 41 def is_valid(self, val):43 def _is_valid(self, val): 42 44 """Should be overload by sub-classes. 43 45 … … 56 58 # we overwrite an attribute self._value 57 59 # 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) 60 62 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) 66 65 67 66 def set(self, kw): 68 67 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' 72 72 73 73 def get(self, *names): 74 74 # 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 77 77 # if no results above we return whole list of values 78 78 if len(result) < 1: … … 81 81 82 82 class Protocol(object): 83 _ordered_fields = () 84 83 85 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 = {} 98 87 # XXX chyba trzeba dodac jakies get flags czy cos 99 88 def set_fields(self, *args, **kwargs): -
branch/UMPA/umpa/utils/__init__.py
r3124 r3128 30 30 """Return a dictionary based on a sequence.""" 31 31 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 sequence35 and position of the name there.36 """37 return seq[seq_with_name.index(name)] -
branch/UMPA/umpa/utils/my_exceptions.py
r3122 r3128 28 28 class UMPAAttributeException(UMPAException): 29 29 pass 30 31 class UMPAAssignException(UMPAException): 32 pass
