syntax Package¶
Contents
syntax Package¶
Utilities relating to low-level NBT syntax.
The package currently consists of a single module, plus a class that used to be a module:
tagsprovides object-oriented NBT encoding and decoding.idsprovides tag ID’s (e.g.ids.TAG_Byteis 1).
-
class
nbtparse.syntax.ids[source]¶ Bases:
enum.IntEnumDefines various tag ID’s.
Formerly a module, now implemented as a class. This provides various benefits while retaining compatibility and a straightforward interface.
-
TAG_Byte= <ids.TAG_Byte: 1>¶ ID of a TAG_Byte
-
TAG_Byte_Array= <ids.TAG_Byte_Array: 7>¶ ID of a TAG_Byte_Array
-
TAG_Compound= <ids.TAG_Compound: 10>¶ ID of a TAG_Compound
-
TAG_Double= <ids.TAG_Double: 6>¶ ID of a TAG_Double
-
TAG_End= <ids.TAG_End: 0>¶ ID of a TAG_End
-
TAG_Float= <ids.TAG_Float: 5>¶ ID of a TAG_Float
-
TAG_Int= <ids.TAG_Int: 3>¶ ID of a TAG_Int
-
TAG_Int_Array= <ids.TAG_Int_Array: 11>¶ ID of a TAG_Int_Array
-
TAG_List= <ids.TAG_List: 9>¶ ID of a TAG_List
-
TAG_Long= <ids.TAG_Long: 4>¶ ID of a TAG_Long
-
TAG_Short= <ids.TAG_Short: 2>¶ ID of a TAG_Short
-
TAG_String= <ids.TAG_String: 8>¶ ID of a TAG_String
-
tags Module¶
Types for the various NBT tags.
Every tag type has a corresponding Pythonic class. Most of these are
subclasses of various built-in classes with additional NBT encoding and
decoding functionality. Generally speaking, TAG_Foo‘s counterpart will be
called FooTag. Most tags are immutable and hashable, and use a
values-based definition of equality. They generally mimic the behavior of the
corresponding built-in class closely if not actually inheriting from it.
The tags all inherit from AbstractTag, an abstract mixin class.
AbstractTag provides some method implementations and a great deal of
high-level documentation; if a particular tag’s documentation is unclear,
consult AbstractTag as well.
This module will also log the encoding and decoding process via
logging; the logger has the same name as the module. Since encoding
and decoding are generally very low-level processes, nearly everything is
logged at the DEBUG level; some irregularities when decoding are logged at
WARNING, and irregularities while encoding will instead generate ordinary
warnings (i.e. warnings.warn()). See the logging documentation
for instructions on how to access this data or ignore it.
Bases:
objectAbstract mixin class for tags.
All NBT tags inherit from AbstractTag.
Encode this tag with a name (e.g. in a
TAG_Compound).Name should be a
unicodeobject, not a string.errorswill be used in encoding the name and payload of this tag.Note
output.write()must perform any buffering that may be necessary to the underlying I/O; it should write its entire argument, unless something has gone wrong.io.BufferedIOBaseand its subclasses satisfy this requirement when in non-interactive mode, but may raiseBlockingIOErrorif in non-blocking mode. If you want to use non-blocking I/O here, consider usingasyncio.run_in_executor()to run the blocking I/O on a separate thread or in a separate process.
The ID of this tag (e.g. 1 for a TAG_Byte).
Bases:
nbtparse.syntax.tags.AbstractTag,bytesRepresents a
TAG_Byte_Array.Derives from
bytes, and can be used anywhere thatbyteswould be valid.Note that this is generally not used to represent text because it lacks encoding information; see
StringTagfor that.Equal to
ids.TAG_Byte_Array.
Bases:
nbtparse.syntax.tags._IntegralTagRepresents a
TAG_Byte.Acts like a numerical type in every respect except that it doesn’t pass explicit type checks.
Equal to
ids.TAG_Byte.
Bases:
nbtparse.syntax.tags.AbstractTag,dictRepresents a
TAG_Compound.Unlike most other tags, this tag is mutable and unhashable.
Derives from
dictand may be used in place of one.Keys are names, values are tags.
The terminating
TAG_Endis handled automatically; you do not need to worry about it.This implementation does not preserve the order of the tags; this is explicitly permitted under the NBT standard.
Equal to
ids.TAG_Compound.
Bases:
nbtparse.syntax.tags._RealTagRepresents a
TAG_Double.Acts like a numerical type in every respect except that it doesn’t pass explicit type checks.
Equal to
ids.TAG_Double.
Bases:
nbtparse.syntax.tags.AbstractTagRepresents a
TAG_End.EndTags always compare equal to one another, are immutable and hashable, and are consideredFalsebybool(). Subclassing it is probably not a great idea.For all practical purposes, you can think of
EndTag()as the tag equivalent ofNone.You probably won’t need this very often; TAG_End mostly only shows up as the terminating sentinel value for
TAG_Compound, andCompoundTaghandles that automatically. It’s here if you need it, though.Writes a single null byte to
output.
Equal to
ids.TAG_End.
Bases:
nbtparse.syntax.tags._RealTagRepresents a
TAG_Float.Acts like a numerical type in every respect except that it doesn’t pass explicit type checks.
Equal to
ids.TAG_Float.
Bases:
nbtparse.syntax.tags.AbstractTag,listRepresents a
TAG_Int_Array.Unlike most other tags, this tag is mutable and unhashable.
Derives from
listand may be used in place of one.Equal to
ids.TAG_Int_Array.
Bases:
nbtparse.syntax.tags._IntegralTagRepresents a
TAG_Int.Acts like a numerical type in every respect except that it doesn’t pass explicit type checks.
Equal to
ids.TAG_Int.
Bases:
nbtparse.syntax.tags.AbstractTag,listRepresents a
TAG_List.Unlike most other tags, this tag is mutable and unhashable.
instance.content_ididentifies the type of the tags listed in this tag. During initialization, ListTag will attempt to guess content_id if it is not provided. If the list is empty, it defaults to None and the list will not be encodable.Identifies the tag id of the tags listed in this
TAG_List.Starts at
Noneif the list was initially empty and a content_id was not provided. While this isNone, the tag cannot be encoded.
Equal to
ids.TAG_List.
Bases:
nbtparse.syntax.tags._IntegralTagRepresents a
TAG_Long.Acts like a numerical type in every respect except that it doesn’t pass explicit type checks.
Equal to
ids.TAG_Long.
Bases:
nbtparse.syntax.tags._IntegralTagRepresents a
TAG_Short.Acts like a numerical type in every respect except that it doesn’t pass explicit type checks.
Equal to
ids.TAG_Short.
Bases:
nbtparse.syntax.tags.AbstractTag,strRepresents a
TAG_String.Derives from
strand can be used anywhere thatstris valid.Equal to
ids.TAG_String.
Decode a named tag from input and returns (name, tag).
Errors will be passed to the Unicode decoder when decoding the name and payload.
Note
input.read()must perform any buffering that may be necessary to the underlying I/O; if it returns less data than was requested, that will be interpreted as EOF.io.BufferedIOBaseand its subclasses satisfy this requirement when in non-interactive mode, but may raiseBlockingIOErrorif in non-blocking mode. If you want to use non-blocking I/O here, consider usingasyncio.run_in_executor()to run the blocking I/O on a separate thread or in a separate process.
Decode a payload with tag ID
tag_id.Helper function to look up the appropriate class and call its
_decode_payload()method.