Exemplo n.º 1
0
 def __init__(self, name, endianity, format):
     if endianity not in (">", "<", "="):
         raise ValueError("endianity must be be '=', '<', or '>'",
                          endianity)
     if len(format) != 1:
         raise ValueError("must specify one and only one format char")
     self.packer = Packer(endianity + format)
     StaticField.__init__(self, name, self.packer.size)
Exemplo n.º 2
0
 def __init__(self, name, endianity, format):
     if endianity not in (">", "<", "="):
         raise ValueError("endianity must be be '=', '<', or '>'", 
             endianity)
     if len(format) != 1:
         raise ValueError("must specify one and only one format char")
     self.packer = Packer(endianity + format)
     StaticField.__init__(self, name, self.packer.size)
Exemplo n.º 3
0
class FormatField(StaticField):
    """
    A field that uses python's built-in struct module to pack/unpack data
    according to a format string.
    Note: this field has been originally implemented as an Adapter, but it 
    was made a construct for performance reasons.
    
    Parameters:
    * name - the name
    * endianity - "<" for little endian, ">" for big endian, or "=" for native
    * format - a single format character
    
    Example:
    FormatField("foo", ">", "L")
    """
    __slots__ = ["packer"]

    def __init__(self, name, endianity, format):
        if endianity not in (">", "<", "="):
            raise ValueError("endianity must be be '=', '<', or '>'",
                             endianity)
        if len(format) != 1:
            raise ValueError("must specify one and only one format char")
        self.packer = Packer(endianity + format)
        StaticField.__init__(self, name, self.packer.size)

    def __getstate__(self):
        attrs = StaticField.__getstate__(self)
        attrs["packer"] = attrs["packer"].format
        return attrs

    def __setstate__(self, attrs):
        attrs["packer"] = Packer(attrs["packer"])
        return StaticField.__setstate__(attrs)

    def _parse(self, stream, context):
        try:
            return self.packer.unpack(_read_stream(stream, self.length))[0]
        except Exception, ex:
            raise FieldError(ex)
Exemplo n.º 4
0
class FormatField(StaticField):
    """
    A field that uses python's built-in struct module to pack/unpack data
    according to a format string.
    Note: this field has been originally implemented as an Adapter, but it 
    was made a construct for performance reasons.
    
    Parameters:
    * name - the name
    * endianity - "<" for little endian, ">" for big endian, or "=" for native
    * format - a single format character
    
    Example:
    FormatField("foo", ">", "L")
    """
    __slots__ = ["packer"]
    def __init__(self, name, endianity, format):
        if endianity not in (">", "<", "="):
            raise ValueError("endianity must be be '=', '<', or '>'", 
                endianity)
        if len(format) != 1:
            raise ValueError("must specify one and only one format char")
        self.packer = Packer(endianity + format)
        StaticField.__init__(self, name, self.packer.size)
    def __getstate__(self):
        attrs = StaticField.__getstate__(self)
        attrs["packer"] = attrs["packer"].format
        return attrs
    def __setstate__(self, attrs):
        attrs["packer"] = Packer(attrs["packer"])
        return StaticField.__setstate__(attrs)
    def _parse(self, stream, context):
        try:
            return self.packer.unpack(_read_stream(stream, self.length))[0]
        except Exception, ex:
            raise FieldError(ex)
Exemplo n.º 5
0
 def __setstate__(self, attrs):
     attrs["packer"] = Packer(attrs["packer"])
     return StaticField.__setstate__(attrs)