epublib.source module

class SourceProtocol(*args, **kwargs)

Bases: Protocol

Protocol for a source of EPUB data.

getinfo(name: str) ZipInfo

Get a ZipInfo object for the given name.

Parameters:

name – The name of the item to get info for.

Returns:

A ZipInfo object for the given name.

open(
name: str | ZipInfo,
mode: Literal['r', 'w'] = 'r',
pwd: bytes | None = None,
*,
force_zip64: bool = False,
) IO[bytes]

Open a file in the source.

Parameters:
  • name – The name of the item to open, or a ZipInfo object.

  • mode – The mode to open the file in..

  • pwd – The password to use for encrypted files.

  • force_zip64 – Whether to force ZIP64 extensions.

Returns:

A file-like object for the opened item.

infolist() list[ZipInfo]

Returns a list of all ZipInfo objects in the source.

close() None

Close this source.

property closed: bool

Returns whether this source is closed.

class SinkProtocol(*args, **kwargs)

Bases: Protocol

Protocol for a sink of EPUB data.

writestr(
zinfo_or_arcname: str | ZipInfo,
data: bytes | str,
compress_type: int | None = None,
compresslevel: int | None = None,
) None

Write a string or bytes to the sink.

Parameters:
  • zinfo_or_arcname – The name of the item to write, or a ZipInfo object.

  • data – The data to write.

  • compress_type – The compression type to use.

  • compresslevel – The compression level to use.

class DirectorySource(path: str | Path)

Bases: SourceProtocol

An EPUB source that reads the book from a directory on the filesystem (an ‘unzipped’ EPUB).

Parameters:

path – The path to the directory containing the EPUB files.

infolist() list[ZipInfo]

Returns a list of all ZipInfo objects in the source.

getinfo(name: str | Path) ZipInfo

Get a ZipInfo object for the given name.

Parameters:

name – The name of the item to get info for.

Returns:

A ZipInfo object for the given name.

open(
name: str | Path | ZipInfo,
mode: Literal['r', 'w'] = 'r',
pwd: bytes | None = None,
*,
force_zip64: bool = False,
) IO[bytes]

Open a file in the source.

Parameters:
  • name – The name of the item to open, or a ZipInfo object.

  • mode – The mode to open the file in..

  • pwd – The password to use for encrypted files.

  • force_zip64 – Whether to force ZIP64 extensions.

Returns:

A file-like object for the opened item.

close() None

Close this source.

property closed: bool

Returns whether this source is closed.

class DirectorySink(path: str | Path)

Bases: SinkProtocol

An EPUB sink that writes the book to a directory on the filesystem (as an ‘unzipped’ EPUB).

Parameters:

path – The path to the directory to write the EPUB files to.

writestr(
zinfo_or_arcname: str | Path | ZipInfo,
data: bytes | str,
compress_type: int | None = None,
compresslevel: int | None = None,
) None

Write a string or bytes to the sink.

Parameters:
  • zinfo_or_arcname – The name of the item to write, or a ZipInfo object.

  • data – The data to write.

  • compress_type – The compression type to use.

  • compresslevel – The compression level to use.

class ZipFile(
file,
mode='r',
compression=0,
allowZip64=True,
compresslevel=None,
*,
strict_timestamps=True,
metadata_encoding=None,
)

Bases: ZipFile

A ZipFile subclass that implements SourceProtocol