~martijnbraam/bare-py

d5a180d9520d0e2ecade600cced3d720381dcc5f — Martijn Braam 8 months ago a7bd850
Added docstrings
3 files changed, 40 insertions(+), 0 deletions(-)

M bare/__init__.py
M bare/lex.py
M bare/parser.py
M bare/__init__.py => bare/__init__.py +30 -0
@@ 6,6 6,13 @@ from bare.bare_ast import StructType, BarePrimitive, TypeKind, NamedType, ArrayT


def _pack_varint(data, signed=False):
    """
    Encode an integer as varint bytes
    :param data: number to encode
    :type data: int
    :param signed: encode as a signed varint with zigzag encoding
    :return: bytes
    """
    result = bytes()
    if signed:
        if data < 0:


@@ 20,6 27,14 @@ def _pack_varint(data, signed=False):


def _unpack_varint(data, offset, signed=False):
    """
    Decode varint encoded bytes to a regular integer
    :param data: input buffer to get the bytes from
    :type data: bytes
    :param offset: offset into the input buffer
    :param signed: decode as a signed varint with zigzag encoding
    :return int
    """
    i = 0
    shift = 0
    result = 0


@@ 250,6 265,12 @@ def _unpack_type(ast_node, instance, module, data, offset):


def pack(instance, member=None):
    """
    Pack an instance of a BARE class into bytes
    :param instance: object to get the schema and data from
    :param member: if the instance is a union, this is the object to get the data and sub-schema from
    :return: bytes
    """
    if member is None:
        ast = instance._ast
        module = sys.modules[instance.__class__.__module__]


@@ 270,6 291,15 @@ def pack(instance, member=None):


def unpack(instance, data, union=False, offset=0, primitive=False):
    """
    Decode a BARE message into an instance of a class with a BARE schema in it.
    :param instance: object to load the data into an to get the schema from
    :param data: the BARE message bytes
    :param union: BARE message is an union, load the right subclass instead
    :param offset: offset into the bytes to start encoding
    :param primitive: bare message doesn't encode a struct, return the raw decoded data instead
    :return: tuple of the unpacked data, and the number of bytes read from the input
    """
    if union:
        module = sys.modules[instance.__module__]
        tag, offset = _unpack_primitive(BarePrimitive(TypeKind.UINT), data, offset)

M bare/lex.py => bare/lex.py +5 -0
@@ 104,6 104,11 @@ def _lex_string(data, line, column):


def lex_schema(data):
    """
    Lex the input schema string into the TOKENS
    :param data: schema as string
    :return: generator that produces LexedToken instances
    """
    pointer = 0
    line = 1
    column = 0

M bare/parser.py => bare/parser.py +5 -0
@@ 42,6 42,11 @@ class PeekableGenerator(object):


def parse(schema):
    """
    Parse a schema string into an AST describing the BARE types
    :param schema: schema as string
    :return: list of BareType and BareEnum instances
    """
    tokens = PeekableGenerator(lex_schema(schema))
    types = []
    while True: