kiel.protocol.primitives

class kiel.protocol.primitives.Primitive(value)[source]

Bases: object

The most basic structure of the protocol. Subclassed, never used directly.

Used as a building block for the various actually-used primitives outlined in the Kafka wire protcol docs:

https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol

fmt = None
render()[source]

Returns a two-element tuple with the struct format and list value.

The value is wrapped in a list, as there are some primitives that deal with multiple values. Any caller of render() should expect a list.

classmethod parse(buff, offset)[source]

Given a buffer and offset, returns the parsed value and new offset.

Uses the fmt class attribute to unpack the data from the buffer and determine the used up number of bytes.

class kiel.protocol.primitives.VariablePrimitive(value)[source]

Bases: kiel.protocol.primitives.Primitive

Base primitive for variable-length scalar primitives (strings and bytes).

size_primitive = None
render()[source]

Returns the struct format and list of the size and value.

The format is derived from the size primitive and the length of the resulting encoded value (e.g. the format for a string of ‘foo’ ends up as ‘h3s’.

Note

The value is expected to be string-able (wrapped in str()) and is then encoded as UTF-8.

classmethod parse(buff, offset)[source]

Given a buffer and offset, returns the parsed value and new offset.

Parses the size_primitive first to determine how many more bytes to consume to extract the value.

class kiel.protocol.primitives.Int8(value)[source]

Bases: kiel.protocol.primitives.Primitive

Represents an 8-bit signed integer.

fmt = 'b'
class kiel.protocol.primitives.Int16(value)[source]

Bases: kiel.protocol.primitives.Primitive

Represents an 16-bit signed integer.

fmt = 'h'
class kiel.protocol.primitives.Int32(value)[source]

Bases: kiel.protocol.primitives.Primitive

Represents an 32-bit signed integer.

fmt = 'i'
class kiel.protocol.primitives.Int64(value)[source]

Bases: kiel.protocol.primitives.Primitive

Represents an 64-bit signed integer.

fmt = 'q'
class kiel.protocol.primitives.String(value)[source]

Bases: kiel.protocol.primitives.VariablePrimitive

Represents a string value, length denoted by a 16-bit signed integer.

size_primitive

alias of Int16

class kiel.protocol.primitives.Bytes(value)[source]

Bases: kiel.protocol.primitives.VariablePrimitive

Represents a bytestring value, length denoted by a 32-bit signed integer.

size_primitive

alias of Int32

class kiel.protocol.primitives.Array(value)[source]

Bases: kiel.protocol.primitives.Primitive

Represents an array of any arbitrary Primitive or Part.

Not used directly but rather by its of() classmethod to denote an Array.of(<something>).

item_class = None
classmethod of(part_class)[source]

Creates a new class with the item_class attribute properly set.

render()[source]

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.

classmethod parse(buff, offset)[source]

Parses a raw buffer at offset and returns the resulting array value.

Starts off by parse()-ing the 32-bit element count, followed by parsing items out of the buffer “count” times.