speasy.core.codecs package

class speasy.core.codecs.CodecInterface(*args, **kwargs)[source]

Bases: Protocol

Interface for codecs.

Codecs are used to load and save data from different formats. Codecs must implement this interface to be registered in the codecs registry.

load_variable(variable: AnyStr, file: memoryview | bytearray | bytes | str | IOBase, cache_remote_files=True, **kwargs) SpeasyVariable | None[source]

Load a variable from a file. The file can be a local file, a remote file or a file-like object.

Parameters:
variablestr

Variable name to load

fileBuffer or str or io.IOBase

File to load variable from

cache_remote_filesbool

Whether to cache remote files

kwargs

Additional keyword arguments, codec specific arguments

Returns:
Optional[SpeasyVariable]

The variable loaded

Raises:
NotImplementedError

If the method is not implemented

load_variables(variables: List, file: memoryview | bytearray | bytes | str | IOBase, cache_remote_files=True, **kwargs) Mapping[AnyStr, SpeasyVariable] | None[source]

Load variables from a file. The file can be a local file, a remote file or a file-like object.

Parameters:
variablesList[str]

List of variable names to load

fileBuffer or str or io.IOBase

File to load variables from

cache_remote_filesbool

Whether to cache remote files

kwargs

Additional keyword arguments, codec specific arguments

Returns:
Optional[Mapping[str, SpeasyVariable]]

A dictionary with the variables loaded

Raises:
NotImplementedError

If the method is not implemented

property name: str

Codec name. Must be unique.

save_variables(variables: List[SpeasyVariable], file: str | IOBase | None = None, **kwargs) bool | memoryview | bytearray | bytes[source]

Save variables to a file. The file can be a local file, a remote file or a file-like object.

Parameters:
variablesList[SpeasyVariable]

List of variables to save

filestr or io.IOBase

File to save variables to, if None, return the data as bytes

kwargs

Additional keyword arguments, codec specific arguments

Returns:
Union[bool, Buffer]

True if the variables were saved successfully, the data as buffer compatible object if file is None

Raises:
NotImplementedError

If the method is not implemented which is an acceptable behavior for read-only codecs

property supported_extensions: List[str]

List of supported file extensions, without the dot. Do return extensions that could be ambiguous with other codecs.

property supported_mimetypes: List[str]

List of supported mime types.

speasy.core.codecs.get_codec(codec: str) CodecInterface | None[source]

Get a codec by name, extension or mimetype

Parameters:
codecstr

Codec name, extension or mimetype

Returns:
Optional[CodecInterface]

The codec that matches the given name, extension or mimetype, or None if no codec is found

speasy.core.codecs.register_codec(cls)[source]

Decorator to register a codec in the codecs registry

Parameters:
clsCodecInterface

The codec class to register

Returns:
CodecInterface

The codec class that was registered

speasy.core.codecs.user_codecs_dir()[source]

Get the user codecs directory, any codec placed in this directory will be automatically registered

Returns:
str

The user codecs directory

Subpackages

Submodules