dolor.types.type¶
Base code for types of packet fields.
- class TypeContext(instance=None, ctx=None)[source]¶
Bases:
objectThe context for a
Type.- Parameters
instance (
Packet, optional) – The packet instance that’s being marshaled.ctx (
PacketContextorVersionorstrorint, optional) – The context for the packet or the version to use for marshaling.
- class Type[source]¶
Bases:
abc.ABCBase 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
Stringfor 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
Typeobject, use thedescriptor()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]'>
See also
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
_defaultattribute is adict, then it changes it to aVersionSwitcher.It also sets
__new__()to_call(). If you want to initialize aType, seedescriptor().
- 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
- classmethod __class_getitem__(index)[source]¶
Gets an
Arrayof the type.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
_defaultattribute 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
_defaultattribute is aVersionSwitcher, then the value for the corresponding version will be returned.Otherwise, if the
_defaultattribute is any value other thanNone, that value will be returned.- Parameters
ctx (
TypeContext, optional) –- Returns
The default value.
- Return type
any
- Raises
NotImplementedError – If the
_defaultattribute isNone.
- 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
bytesorbytearray) – 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"becauseTypeinherits fromabc.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