kilter.protocol.buffer module

Structural type for buffers used by this milter package, plus a simple implementation

This module provides an abstract buffer interface and a simple implementation with poor delete performance.

FixedSizeBuffer is the abstract type for a buffer to be used with FilterProtocol and the pack and unpack methods of Message.

SimpleBuffer is the initial concrete implementation.

class FixedSizeBuffer(*args, **kwargs)[source]

Bases: Protocol

The interface that must be presented by a buffer implementation for protocols

Implementations must provide an item accessor interface that accepts ONLY slices. Methods that “get” data from the buffer return memoryviews, which can be indexed as normal. How empty (unspecified) slice-start and -end values are treated depends on the operation.

Set:

The slice start defaults to the first unset index (i.e. the value of self.filled), slice stop defaults to the start index plus the length of the data being written. The size of the slice MUST be the same shape (length) as the data being written. In practice the stop value will rarely be specified.

>>> from kilter.protocol.buffer import SimpleBuffer
>>> buff = SimpleBuffer(50)
>>> buff[:] = b"spam & "
>>> buff[:] = b"eggs"
>>> data = buff[:].tobytes()
>>> data
b'spam & eggs'

These are equivalent:

>>> buff[:] = data
>>> buff[buff.filled:buff.filled + len(data)] = data

Wrong shape:

>>> buff[0:5] = b"spam"
Traceback (most recent call last):
        ...
ValueError: ...

Insufficient space:

>>> buff = SimpleBuffer(10)
>>> buff[:] = b"spam and eggs"
Traceback (most recent call last):
        ...
InsufficientSpace: ...

Get:

The slice start defaults to the start of the buffer, and the slice stop defaults to the last set index. The length of the returned memoryview will therefore be equal to self.filled.

>>> buff = SimpleBuffer(10)
>>> buff[:] = b"spam"
>>> buff.filled, buff.available
(4, 6)

Delete:

The slice defaults are as for get slices.

property filled: int

The amount of the buffer (in bytes) that has been filled

property available: int

The amount of the buffer (in bytes) that is unfilled and available

get_free(size: int) → memoryview[source]

Return a writable memoryview of a portion of available buffer space

Raises InsufficientSpace if the requested amount of space is not available.

class SimpleBuffer(capacity: int)[source]

Bases: object

A simple implementation of FixedSizeBuffer

This implementation has poor delete performance when deleting from the head.

buffer
filled
property available: int

The amount of the buffer (in bytes) that is unfilled and available

get_free(size: int) → memoryview[source]

Return a writable memoryview of a portion of available buffer space

Raises InsufficientSpace if the requested amount of space is not available.