π οΈ Core#
Abstract core classes for reading and writing binary formats.
BaseFile#
Binary stream adapter for file-like sources.
Wraps any binary source (file path, bytes, or IO stream) into a unified interface for reading and writing structured binary data.
- class BaseFile(stream, mode='rb')[source]#
Bases:
StructIO,ABCUnified binary stream handler.
-
format:
FileFormat= ''# Associated file format.
-
signature:
Optional[bytes] = None# Expected file signature.
- property suffix: str#
- property location: str#
- property closed: bool#
- seek(pos, whence=0)[source]#
Change the stream position to the given byte offset.
- offset
The stream position, relative to βwhenceβ.
- whence
The relative position to seek from.
The offset is interpreted relative to the position indicated by whence. Values for whence are:
os.SEEK_SET or 0 β start of stream (the default); offset should be zero or positive
os.SEEK_CUR or 1 β current stream position; offset may be negative
os.SEEK_END or 2 β end of stream; offset is usually negative
Return the new absolute position.
- Return type:
int
- readable()[source]#
Return whether object was opened for reading.
If False, read() will raise OSError.
- Return type:
bool
- writable()[source]#
Return whether object was opened for writing.
If False, write() will raise OSError.
- Return type:
bool
- seekable()[source]#
Return whether object supports random access.
If False, seek(), tell() and truncate() will raise OSError. This method may need to do a test seek().
- Return type:
bool
- flush()[source]#
Flush write buffers, if applicable.
This is not implemented for read-only and non-blocking streams.
- Return type:
None
-
format:
Content#
Shared content data containers for handlers. Defines data structures that hold parsed file contents.
- class ModelContent(type=FileType.MODEL, version=0.0, flags=<factory>, scene=<factory>)[source]#
Bases:
BaseContentContent container for 3D models.
-
version:
float= 0.0#
-
scene:
ModelScene#
-
version:
- class TextureContent(type=FileType.TEXTURE, width=0, height=0, mipmap_count=0, format=<factory>, texture=<factory>)[source]#
Bases:
BaseContent,Generic[TextureType]Content container for textures (2D or cubemap).
-
width:
int= 0#
-
height:
int= 0#
-
mipmap_count:
int= 0#
-
format:
bytes#
- property is_cubemap: bool#
- property is_compressed: bool#
- property fourcc: bytes#
-
width:
- class ImageContent(type=FileType.IMAGE, image=<factory>)[source]#
Bases:
BaseContentContent container for images.
-
image:
bytes#
-
image:
- class TexarrContent(type=FileType.TEXARR, count=0, textures=<factory>)[source]#
Bases:
BaseContentContent container for texture arrays.
-
count:
int= 0#
-
textures:
list[tuple[str,bytes]]#
-
count:
- class NbtContent(type=FileType.NBT, value=None)[source]#
Bases:
BaseContentContent container for NBT (Named Binary Tag) data.
-
value:
None|int|float|bytes|str|list[int] |list[None|int|float|bytes|str|list[int] |list[NbtValue] |dict[str, NbtValue]] |dict[str,None|int|float|bytes|str|list[int] |list[NbtValue] |dict[str, NbtValue]] = None#
-
value:
- class RegionContent(type=FileType.REGION, rx=0, rz=0, offsets=<factory>, counts=<factory>, uuid=<factory>, chunks=<factory>)[source]#
Bases:
BaseContentContent container for regions (world terrain).
-
rx:
int= 0#
-
rz:
int= 0#
-
offsets:
list[int]#
-
counts:
list[int]#
-
uuid:
list[UUID]#
-
chunks:
list[RegionChunk]#
-
rx:
Decoder#
Base class for file format decoders.
Defines the contract for parsing binary data into structured content.
- class FileDecoder(stream, options=None)[source]#
Bases:
BaseFile,Generic[ContentType],ABCBase class for decoding binary data into structured content.
Subclasses define format-specific parsing logic.
- decode(seek=True)[source]#
Runs decoding pipeline.
- Parameters:
seek (
bool) β Reset stream position to the beginning after parsing.- Return type:
TypeVar(ContentType, bound=BaseContent)- Returns:
Parsed content data.
- convert_to(encoder, options=None, output=None)[source]#
Decode and convert to given encoder format.
- Parameters:
encoder (
Type[TypeVar(EncoderType, bound=FileEncoder)]) β Encoder class to use for conversion.options (optional) β Shared handlers options.
output (optional) β File path or binary IO stream. Defaults to in-memory buffer.
- Return type:
TypeVar(EncoderType, bound=FileEncoder)- Returns:
Clear encoder instance.
- convert(encoder, options=None, output=None)[source]#
Decode and convert to given encoder format.
- Parameters:
encoder (
Type[TypeVar(EncoderType, bound=FileEncoder)]) β Encoder class to use for conversion.options (optional) β Shared handlers options.
output (optional) β File path or binary IO stream. Defaults to in-memory buffer.
- Return type:
bytes- Returns:
Encoded file content as bytes.
- abstractmethod parse()[source]#
Parse file content into
self.data. Called bydecode().- Return type:
None
- validate_signature()[source]#
Validate file signature.
- Raises:
EmptyFileError β
- Return type:
None
Encoder#
Base class for file format encoders.
Defines the contract for serializing structured content into binary data.
- class FileEncoder(data, options=None, output=None)[source]#
Bases:
BaseFile,Generic[ContentType],ABCBase class for encoding structured content into binary data.
Subclasses define the format-specific serialization logic.
-
transforms:
Optional[list[Callable[[ModelScene],ModelScene]]] = None# Format-specific transforms applied to model data before serialization.
- encode(transforms=None)[source]#
Runs encoding pipeline.
- Parameters:
transforms (
Optional[list[Callable[[ModelScene],ModelScene]]]) β Override the default transforms for this call.- Return type:
Self- Returns:
Self (chaining).
- abstractmethod serialize()[source]#
Write
self.datato the output stream. Called byencode().- Return type:
None
- save_as(path, mode='wb')[source]#
Write encoded data to file by name. Keeps encoder open.
- Parameters:
path (
str|Path|PathLike[str]) β Output file path.mode (
Literal['rb','rb+','wb','wb+','ab','ab+']) β File mode (binary).
- Return type:
Self
- export_as(path, mode='wb')[source]#
Write encoded data to file by stem. Format suffix appended. Keeps the encoder open.
- Parameters:
path (
str|Path|PathLike[str]) β Output file path.mode (
Literal['rb','rb+','wb','wb+','ab','ab+']) β File mode (binary).
- Return type:
Self
- save(path, mode='wb')[source]#
Write encoded data to file by name. Closes encoder.
- Parameters:
path (
str|Path|PathLike[str]) β Output file path.mode (
Literal['rb','rb+','wb','wb+','ab','ab+']) β File mode (binary).
- Return type:
None
-
transforms:
Options#
Shared options for handlers.
- class Options(model_formats=None, skeleton=False, animation=False, raw_blocks=False, full_chunk=False, on_conflict='overwrite')[source]#
Shared handlers options.
-
model_formats:
Optional[Sequence[FileFormat]] = None# Preferred output formats for models,
default_model_formats()used on unset.
-
skeleton:
bool= False# Handle skeleton bones from models.
-
animation:
bool= False# Handle built-in animation clips from models.
-
raw_blocks:
bool= False# Keep raw block IDs in chunks without lookup table replacement.
-
full_chunk:
bool= False# Handle full chunk data including metadata (no export).
-
on_conflict:
Literal['overwrite','rename','skip'] = 'overwrite'# Action on output file name conflict (if already exists).
βoverwriteβ Replace the existing file.
βskipβ Keep the existing file.
βrenameβ Add a numeric suffix (e.g. model (1).obj).
- property default_model_formats: Sequence[FileFormat]#
Default output formats for models based on current options.
-
model_formats:
StructIO#
Low-level binary I/O with structured packing and unpacking operations.
Provides a base class for reading and writing binary data in various formats
using struct and numpy. Designed to be subclassed by format-specific I/O classes.