dolor.nbt¶
NBT marshaling.
See https://wiki.vg/NBT for a specification of the format.
- class Tag(value=None, *, root_name=None)[source]¶
Bases:
abc.ABCAn NBT tag.
- Parameters
value (any, optional) – The tag’s value. If unspecified, then the default value will be used.
root_name (
str, optional) – The root name of the tag. If specified, then the tag will be treated as a root tag.
- id¶
The tag’s id. If
None, then the tag will not be used in marshaling.- Type
intorNone
- type¶
The tag’s underlying type. Used to automatically pack and unpack the raw data of the tag. Must be able to be used without a specified
TypeContext.- Type
subclass of
Type
- value¶
The tag’s value.
- Type
any
- root_name¶
The tag’s root name. If not
None, then the tag will be treated as a root tag.- Type
strorNone
- classmethod from_id(id)[source]¶
Gets the tag whose id is id.
Will search through the subclasses of
Tag, ignoring subclasses whoseidattribute isNone.- Parameters
id (
int) – The id to look for.- Returns
The tag whose id is
id.- Return type
subclass of
Tag
Examples
>>> import dolor >>> dolor.nbt.Tag.from_id(0) <class 'dolor.nbt.End'>
- pack()[source]¶
Packs the tag.
Does not include the id. Use
dump()for a complete dump.- Returns
The packed tag.
- Return type
bytes
- classmethod unpack(buf, *, root=False)[source]¶
Unpacks a buffer into a tag.
Does not include the id. Use
load()to properly unpack NBT data.- Parameters
buf (file object or
bytesorbytearray) – The buffer to unpack.root (
bool, optional) – Whether the tag is a root tag.
- Returns
The unpacked tag.
- Return type
- class End(value=None, *, root_name=None)[source]¶
Bases:
dolor.nbt.Tag- type¶
alias of
dolor.types.misc.EmptyType
- class Byte(value=None, *, root_name=None)[source]¶
Bases:
dolor.nbt.Tag- type¶
alias of
dolor.types.numeric.Byte
- class Short(value=None, *, root_name=None)[source]¶
Bases:
dolor.nbt.Tag- type¶
alias of
dolor.types.numeric.Short
- class Int(value=None, *, root_name=None)[source]¶
Bases:
dolor.nbt.Tag- type¶
alias of
dolor.types.numeric.Int
- class Long(value=None, *, root_name=None)[source]¶
Bases:
dolor.nbt.Tag- type¶
alias of
dolor.types.numeric.Long
- class Float(value=None, *, root_name=None)[source]¶
Bases:
dolor.nbt.Tag- type¶
alias of
dolor.types.numeric.Float
- class Double(value=None, *, root_name=None)[source]¶
Bases:
dolor.nbt.Tag- type¶
alias of
dolor.types.numeric.Double
- class ByteArray(value=None, *, root_name=None)[source]¶
Bases:
dolor.nbt.Tag- type¶
alias of
dolor.types.array.Byte[Int]
- class String(value=None, *, root_name=None)[source]¶
Bases:
dolor.nbt.Tag- type¶
alias of
dolor.types.string.String(65535)
- class List(tag_or_value=None, *args, **kwargs)[source]¶
Bases:
dolor.nbt.TagA List tag.
- Parameters
- class Compound(value=None, *, root_name=None)[source]¶
Bases:
dolor.nbt.TagA Compound tag.
- class IntArray(value=None, *, root_name=None)[source]¶
Bases:
dolor.nbt.Tag- type¶
alias of
dolor.types.array.Int[Int]
- class LongArray(value=None, *, root_name=None)[source]¶
Bases:
dolor.nbt.Tag- type¶
alias of
dolor.types.array.Long[Int]
- load(f)[source]¶
Loads a complete NBT dump into a
Tag.- Parameters
f (pathlike or
bytesorbytearrayor file object) –If a pathlike, then the path to the file to read from. Otherwise the data to load.
The data may be uncompressed, gzip’d, or zlib’d.
- Returns
The loaded tag.
- Return type
Examples
>>> import dolor >>> dolor.nbt.load(b"\x08\x00\x04test\x00\x0eThis is a test") String(root_name='test', 'This is a test')
- dump(obj, f=None, *, compression=None)[source]¶
Dumps a root tag into a binary dump.
- Parameters
obj (
Tag) – The root tag to dump.f (file object, optional) – The file object to dump to. If unspecified, the dumped data will instead be returned.
compression (
functionor any, optional) –The compression used to compress the dumped data. If unspecified, the data won’t be compressed.
If a
function, then the data will be passed to it and the return value will be treated as the new, compressed data.Otherwise, the
compressattribute of compression will be used as the compression function, allowing you to pass a module likegzipas compression.
- Returns
If
fis unspecified, thenbyteswill be returned. Otherwise how many bytes were written tofwill be returned.- Return type
bytesorint- Raises
ValueError – If
objis not a root tag.
Examples
>>> import dolor >>> import gzip >>> import io >>> tag = dolor.nbt.String("This is a test", root_name="test") >>> dolor.nbt.dump(tag) b'\x08\x00\x04test\x00\x0eThis is a test' >>> dolor.nbt.dump(tag, compression=gzip) b'\x1f\x8b\x08\x00u;\xfa_\x02\xff\xe3``)I-.a\xe0\x0b\xc9\xc8,V\x00\xa2D\x05\x10\x1f\x00(\x9a)|\x17\x00\x00\x00' >>> f = io.BytesIO() >>> dolor.nbt.dump(tag, f) 23 >>> f.getvalue() b'\x08\x00\x04test\x00\x0eThis is a test'