dolor.types.type

Base code for types of packet fields.

class TypeContext(instance=None, ctx=None)[source]

Bases: object

The context for a Type.

Parameters
  • instance (Packet, optional) – The packet instance that’s being marshaled.

  • ctx (PacketContext or Version or str or int, optional) – The context for the packet or the version to use for marshaling.

instance

The packet instance that’s being marshaled.

Type

Packet or None

version

The version to use for marshaling.

Type

Version

class Type[source]

Bases: abc.ABC

Base class for types of packet fields.

This class is used for marshaling raw data (typically in packets) to and from values, and it has some interesting quirks.

For one, calling their constructor doesn’t really construct them, but rather make new types. Take String for instance: It can take an argument for the string’s maximum length:

>>> import dolor
>>> dolor.types.String.max_length
32767
>>> s = dolor.types.String(20)
>>> s
<class 'dolor.types.string.String(20)'>
>>> s.max_length
20

To really get a Type object, use the descriptor() method, like so:

>>> import dolor
>>> dolor.types.VarInt.descriptor("attr_name") 
<dolor.types.numeric.VarInt object at 0x7fe5ad4e5490>

You can also generate arrays of types like so:

>>> import dolor
>>> dolor.types.Short[1]
<class 'dolor.types.array.Short[1]'>

To marshal data to a value, see the unpack() method.

To marshal a value to data, see the pack() method.

classmethod __init_subclass__(**kwargs)[source]

Spiffs up subclasses.

If the _default attribute is a dict, then it changes it to a VersionSwitcher.

It also sets __new__() to _call(). If you want to initialize a Type, see descriptor().

classmethod descriptor(name)[source]

Gets the descriptor form of the type.

Parameters

name (str) – The name of the packet field.

Returns

The descriptor form of the type.

Return type

Type

classmethod __class_getitem__(index)[source]

Gets an Array of the type.

Parameters

index (int or subclass of Type or None) – The size argument passed to Array.

Examples

>>> import dolor
>>> dolor.types.VarInt[1]
<class 'dolor.types.array.VarInt[1]'>
classmethod default(*, ctx=None)[source]

Gets the default value of the type.

If the _default attribute is a classmethod, then it should look like this:

@classmethod
def _default(cls, *, ctx=None):
    return my_default_value

The return value of the classmethod will be returned from this method.

If the _default attribute is a VersionSwitcher, then the value for the corresponding version will be returned.

Otherwise, if the _default attribute is any value other than None, that value will be returned.

Parameters

ctx (TypeContext, optional) –

Returns

The default value.

Return type

any

Raises

NotImplementedError – If the _default attribute is None.

classmethod unpack(buf, *, ctx=None)[source]

Gets the corresponding value from the buffer.

Warning

Do not override this method. Instead override _unpack().

Parameters
  • buf (file object or bytes or bytearray) – The buffer containing the raw data.

  • ctx (TypeContext, optional) –

Returns

The corresponding value from the buffer.

Return type

any

classmethod pack(value, *, ctx=None)[source]

Packs a value into raw data.

Warning

Do not override this method. Instead override pack().

Parameters
  • value – The value to pack.

  • ctx (TypeContext, optional) –

Returns

The corresponding raw data.

Return type

bytes

abstract classmethod _unpack(buf, *, ctx=None)[source]

Gets the corresponding value from the buffer.

To be overridden by subclasses.

Warning

Do not use this method directly, always use unpack() instead.

Parameters
  • buf (file object) – The buffer containing the raw data.

  • ctx (TypeContext, optional) –

Returns

The corresponding value from the buffer.

Return type

any

abstract classmethod _pack(value, *, ctx=None)[source]

Packs a value into raw data.

To be overridden by subclasses.

Warning

Do not use this method directly, always use pack() instead.

Parameters
  • value – The value to pack.

  • ctx (TypeContext, optional) –

Returns

The corresponding raw data.

Return type

bytes

classmethod make_type(name, bases=None, **namespace)[source]

Utility for generating new types.

The generated type’s __module__ attribute is set to be the same as the origin type’s. This is done to get around an issue where generated types would have their __module__ attribute be "abc" because Type inherits from abc.ABC.

Parameters
  • name (str) – The generated type’s name.

  • bases (tuple, optional) – The generated type’s base classes. If unspecified, the origin type is the sole base class.

  • **namespace – The attributes and corresponding values of the generated type.

Returns

The generated type.

Return type

subclass of Type