~chiefnoah/pybare

c0eee1a8c6d521dca808c80dcc266892623eb6a9 — Noah Pederson 8 months ago 94d53bc
Adds more docstrings
2 files changed, 25 insertions(+), 2 deletions(-)

M bare/encoder.py
M bare/types.py
M bare/encoder.py => bare/encoder.py +14 -1
@@ 122,6 122,9 @@ class Field(ABC):

    @property
    def value(self):
        """value accesses the wrapped value for this instance

        """
        return self._value

    @value.setter


@@ 139,6 142,11 @@ class Field(ABC):
        pass

    def pack(self, fp=None) -> typing.Optional[bytes]:
        """pack encodes this structure and all nested structures using the BARE format
        if the optional `fp` is specified, this function does not return anything
        :param typing.BinaryIO fp: an optional io stream to write bytes to
        :returns: encoded bytes if fp is None
        """
        buffered = False
        if not fp:
            fp = io.BytesIO()


@@ 148,6 156,9 @@ class Field(ABC):
            return fp.getvalue()

    def unpack(self, fp: typing.BinaryIO):
        """unpacks bytes from fp into an instance of this class

        """
        # If it's a bytes-like, wrap it in a io buffer
        if hasattr(fp, "decode"):
            fp = io.BytesIO(fp)


@@ 212,7 223,9 @@ class Struct(ABC):
    @classmethod
    def unpack(cls, data: typing.Union[typing.BinaryIO, bytes]):
        """
        unpack deserializes data into a type. If
        unpacks data into an instance of this struct
        :param bytes|BinaryIO data: bytes or byte stream to read values from
        :returns: an instance of this class with populated fields
        """
        if hasattr(data, "decode"):
            fp = io.BytesIO(data)

M bare/types.py => bare/types.py +11 -1
@@ 31,6 31,9 @@ class Simple(Field):


class U8(Simple):
    """
    An unsigned 8bit integer
    """

    _type = BareType.U8
    _default = 0


@@ 320,6 323,13 @@ class DataFixed(Field):
    _default = bytes(_length)

    def __init__(self, length=0, value=None):
        """Fixed length raw data type

        :param int length: number in bytes this type should represent.
            Either this arg must be set, or this class subclassed and the
            _length class field overriden
        :param bytes value: an optional value to set
        """
        if length == 0 and self.__class__._length > 0:
            self._length = self.__class__._length
        else:


@@ 357,7 367,7 @@ class Enum(UInt):
        """Enum defines a BARE enum type

        Validate checks whether the provided value is an `int` and a valid enum member
        :param enum (`enum.Enum`): a standard `Enum` type. Values for enum members *must* be positive ints
        :param Enum enum: a standard `Enum` type. Values for enum members *must* be positive ints
        """
        self._enum = enum
        super().__init__(*args, **kwargs)