speasy.core.codecs.codec_interface module

class speasy.core.codecs.codec_interface.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.