Changeset 3143

Show
Ignore:
Timestamp:
07/12/08 13:35:20 (5 years ago)
Author:
getxsick
Message:

First draft of struct wrapper. It needs more improvement.
There is one issue unsupported: if length of field is not byte-like (so it's not multiple by 8) then it doesn't work. So that why _stick_fields() method will be for.

Files:
1 modified

Legend:

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

    r3140 r3143  
    2020# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  
    2121 
     22import struct 
     23 
    2224from umpa import utils 
    2325 
     
    2527    def __init__(self, value=None, bits=None): 
    2628        if bits is not None: 
    27             self._bits = bits 
     29            self.bits = bits 
    2830        self._value = value 
    2931 
     
    3840 
    3941    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 
    4563 
    4664    def fillout(self): 
     
    6381        # we overwrite an attribute self._value 
    6482        # 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) ] 
    6684        self._value = dict(zip(self._ordered_fields, false_list)) 
    6785 
     
    163181 
    164182    def get_raw(self): 
    165         """Return raw bit of the protocol's object""" 
     183        """Return raw bits of the protocol's object.""" 
    166184        #XXX: i will try to write general get_raw method for all subclasses 
    167185        # 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        """ 
    170198 
    171199    def _is_valid(self, field):