Changeset 3143
- Timestamp:
- 07/12/08 13:35:20 (5 years ago)
- Files:
-
- 1 modified
-
branch/UMPA/umpa/protocols/base.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branch/UMPA/umpa/protocols/base.py
r3140 r3143 20 20 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 21 22 import struct 23 22 24 from umpa import utils 23 25 … … 25 27 def __init__(self, value=None, bits=None): 26 28 if bits is not None: 27 self. _bits = bits29 self.bits = bits 28 30 self._value = value 29 31 … … 38 40 39 41 def _is_valid(self, val): 40 """Should be overload by sub-classes. 41 42 Otherwise always return true. 43 """ 44 return True 42 """Check if a value is not bigger than expected. 43 """ 44 if 2**self.bits > val: 45 return True 46 else: 47 return False 48 49 def _pack(self): 50 val = self.get() 51 52 if self.bits <= 8: 53 bits = struct.pack('!B', val) 54 elif self.bits > 8 and self.bits <= 16: 55 bits = struct.pack('!H', val) 56 elif self.bits > 16 and self.bits <= 32: 57 bits = struct.pack('!I', val) 58 else: 59 raise UMPAException, 'Fields with ' + self.bits + \ 60 ' bits are not supported' 61 62 return bits 45 63 46 64 def fillout(self): … … 63 81 # we overwrite an attribute self._value 64 82 # because we need a list instead of simple var here 65 false_list = [ False for i in xrange(self. _bits) ]83 false_list = [ False for i in xrange(self.bits) ] 66 84 self._value = dict(zip(self._ordered_fields, false_list)) 67 85 … … 163 181 164 182 def get_raw(self): 165 """Return raw bit of the protocol's object"""183 """Return raw bits of the protocol's object.""" 166 184 #XXX: i will try to write general get_raw method for all subclasses 167 185 # i mean that it should be unnecessary to overwrite it by subclasses 168 print "Not implemented yet." 169 return False 186 187 # get all fields in binary mode 188 header_unpack = [ self._fields[field].fillout() 189 for field in self._ordered_fields ] 190 # pack all binaries fields together 191 header_pack = "".join(header_pack) 192 193 return header_pack 194 195 def _stick_fields(self, *fields): 196 """Join fields together if length is not byte compatible. 197 """ 170 198 171 199 def _is_valid(self, field):
