How to use the aiozk.protocol.primitives.Primitive function in aiozk

To help you get started, we’ve selected a few aiozk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github micro-fan / aiozk / aiozk / protocol / primitives.py View on Github external
consume to extract the value.
        """
        size, offset = cls.size_primitive.parse(buff, offset)
        if size == -1:
            return None, offset

        var_struct = struct.Struct("!%ds" % size)

        value = var_struct.unpack_from(buff, offset)[0]
        value = cls.parse_value(value)
        offset += var_struct.size

        return value, offset


class Bool(Primitive):
    """
    Represents a boolean (true or false) value.

    Renders as an unsigned char (1 byte).
    """
    fmt = "?"


class Byte(Primitive):
    """
    Represents a single 8-bit byte.
    """
    fmt = "b"


class Int(Primitive):
github micro-fan / aiozk / aiozk / protocol / primitives.py View on Github external
"""
        Creates a composite ``struct`` format and the data to render with it.

        The format and data are prefixed with a 32-bit integer denoting the
        number of elements, after which each of the items in the array value
        are ``render()``-ed and added to the format and data as well.
        """
        value = self.value
        if value is None:
            value = []

        fmt = [Int.fmt]
        data = [len(value)]

        for item_value in value:
            if issubclass(self.item_class, Primitive):
                item = self.item_class(item_value)
            else:
                item = item_value

            item_format, item_data = item.render()
            fmt.extend(item_format)
            data.extend(item_data)

        return "".join(fmt), data
github micro-fan / aiozk / aiozk / protocol / primitives.py View on Github external
value = cls.parse_value(value)
        offset += var_struct.size

        return value, offset


class Bool(Primitive):
    """
    Represents a boolean (true or false) value.

    Renders as an unsigned char (1 byte).
    """
    fmt = "?"


class Byte(Primitive):
    """
    Represents a single 8-bit byte.
    """
    fmt = "b"


class Int(Primitive):
    """
    Represents an 32-bit signed integer.
    """
    fmt = "i"


class Long(Primitive):
    """
    Represents an 64-bit signed integer.
github micro-fan / aiozk / aiozk / protocol / part.py View on Github external
Returns a two-element tuple with the ``struct`` format and values.

        Iterates over the applicable sub-parts and calls `render()` on them,
        accumulating the format string and values.

        Optionally takes a subset of parts to render, default behavior is to
        render all sub-parts belonging to the class.
        """
        if not parts:
            parts = self.parts

        fmt = []
        data = []

        for name, part_class in parts:
            if issubclass(part_class, Primitive):
                part = part_class(getattr(self, name, None))
            else:
                part = getattr(self, name, None)

            part_format, part_data = part.render()

            fmt.extend(part_format)
            data.extend(part_data)

        return "".join(fmt), data
github micro-fan / aiozk / aiozk / protocol / primitives.py View on Github external
value = primitive_struct.unpack_from(buff, offset)[0]
        offset += primitive_struct.size

        return value, offset

    def __eq__(self, other):
        """
        Basic equality method that tests equality of the ``value`` attributes.
        """
        return self.value == other.value

    def __str__(self):
        return "%s(%s)" % (self.__class__.__name__, self.value)


class VariablePrimitive(Primitive):
    """
    Base primitive for variable-length scalar primitives (strings and bytes).
    """
    size_primitive = None

    def render_value(self, value):
        raise NotImplementedError

    @classmethod
    def parse_value(cls, value):
        raise NotImplementedError

    def render(self):
        """
        Returns the ``struct`` format and list of the size and value.
github micro-fan / aiozk / aiozk / protocol / primitives.py View on Github external
class Byte(Primitive):
    """
    Represents a single 8-bit byte.
    """
    fmt = "b"


class Int(Primitive):
    """
    Represents an 32-bit signed integer.
    """
    fmt = "i"


class Long(Primitive):
    """
    Represents an 64-bit signed integer.
    """
    fmt = "q"


class Float(Primitive):
    """
    Represents a single-precision floating poing conforming to IEEE 754.
    """
    fmt = "f"


class Double(Primitive):
    """
    Represents a double-precision floating poing conforming to IEEE 754.
github micro-fan / aiozk / aiozk / protocol / primitives.py View on Github external
class Int(Primitive):
    """
    Represents an 32-bit signed integer.
    """
    fmt = "i"


class Long(Primitive):
    """
    Represents an 64-bit signed integer.
    """
    fmt = "q"


class Float(Primitive):
    """
    Represents a single-precision floating poing conforming to IEEE 754.
    """
    fmt = "f"


class Double(Primitive):
    """
    Represents a double-precision floating poing conforming to IEEE 754.
    """
    fmt = "d"


class UString(VariablePrimitive):
    """
    Represents a unicode string value, length denoted by a 32-bit integer.
github micro-fan / aiozk / aiozk / protocol / primitives.py View on Github external
"""
    Represents a boolean (true or false) value.

    Renders as an unsigned char (1 byte).
    """
    fmt = "?"


class Byte(Primitive):
    """
    Represents a single 8-bit byte.
    """
    fmt = "b"


class Int(Primitive):
    """
    Represents an 32-bit signed integer.
    """
    fmt = "i"


class Long(Primitive):
    """
    Represents an 64-bit signed integer.
    """
    fmt = "q"


class Float(Primitive):
    """
    Represents a single-precision floating poing conforming to IEEE 754.
github micro-fan / aiozk / aiozk / protocol / primitives.py View on Github external
Represents a bytestring value, length denoted by a 32-bit signed integer.
    """
    size_primitive = Int

    def render_value(self, value):
        if isinstance(value, str):
            return value.encode()
        # return bytes(value)
        return value

    @classmethod
    def parse_value(cls, value):
        return value


class Vector(Primitive):
    """
    Represents an array of any arbitrary `Primitive` or ``Part``.

    Not used directly but rather by its ``of()`` classmethod to denote an
    ``Vector.of()``.
    """
    item_class = None

    @classmethod
    def of(cls, part_class):
        """
        Creates a new class with the ``item_class`` attribute properly set.
        """
        copy = type(
            "VectorOf%s" % part_class.__name__,
            cls.__bases__, dict(cls.__dict__)
github micro-fan / aiozk / aiozk / protocol / primitives.py View on Github external
class Long(Primitive):
    """
    Represents an 64-bit signed integer.
    """
    fmt = "q"


class Float(Primitive):
    """
    Represents a single-precision floating poing conforming to IEEE 754.
    """
    fmt = "f"


class Double(Primitive):
    """
    Represents a double-precision floating poing conforming to IEEE 754.
    """
    fmt = "d"


class UString(VariablePrimitive):
    """
    Represents a unicode string value, length denoted by a 32-bit integer.
    """
    size_primitive = Int

    def render_value(self, value):
        return bytes(str(value).encode("utf-8"))

    @classmethod