annals.requirements#

Optional dependencies, checked where they are needed and explained when they are missing.

annals installs light: a heavy library (WeasyPrint for PDFs, pypdf for joining them, markdownify for html to markdown) is imported only when the conversion that needs it runs, and a missing one is a MissingDependency that says which conversion needed it and the exact command that fixes it, never a bare ImportError from deep inside a library.

>>> check_requirements("json", what="anything")        # present: returns quietly
>>> try:
...     check_requirements("no_such_module_xyz", what="Combining into PDF")
... except MissingDependency as e:
...     print(str(e).splitlines()[0])
Combining into PDF needs no_such_module_xyz, which is not installed.

The pip name and the hint for a module are in PACKAGES, so a new optional library is one entry there.

Module Attributes

DFLT_EXTRA

the extra of annals that installs everything a conversion may need

PACKAGES

module name -> (pip name, system hint or "")

Functions

check_requirements(*modules, what[, extra])

Raise MissingDependency unless every one of modules is installed.

import_lazily(name, *, what[, extra])

The module name, imported now; a failure to import it is a MissingDependency.

missing_modules(modules)

The modules that cannot be imported, found without importing them.

Exceptions

MissingDependency(modules, *, what[, extra])

A library an operation needs is not installed (the message says how to install it).

annals.requirements.DFLT_EXTRA = 'pdf'#

the extra of annals that installs everything a conversion may need

exception annals.requirements.MissingDependency(modules, *, what, extra='pdf')[source]#

Bases: RuntimeError

A library an operation needs is not installed (the message says how to install it).

annals.requirements.PACKAGES: dict[str, tuple[str, str]] = {'PIL': ('pillow', ''), 'markdown_it': ('markdown-it-py', ''), 'markdownify': ('markdownify', ''), 'pdfdol': ('pdfdol', ''), 'playwright': ('playwright', 'Then fetch its browser: playwright install chromium'), 'pypdf': ('pypdf', ''), 'weasyprint': ('weasyprint', 'It also needs the Pango library (Debian/Ubuntu: apt install libpango-1.0-0).')}#

module name -> (pip name, system hint or "")

annals.requirements.check_requirements(*modules, what, extra='pdf')[source]#

Raise MissingDependency unless every one of modules is installed.

Cheap (nothing is imported), so a caller checks everything a job will use before it starts, and fails at once instead of half way.

Return type:

None

annals.requirements.import_lazily(name, *, what, extra='pdf')[source]#

The module name, imported now; a failure to import it is a MissingDependency.

A library that is installed but cannot load (WeasyPrint without Pango raises OSError) is reported the same way.

annals.requirements.missing_modules(modules)[source]#

The modules that cannot be imported, found without importing them.

Return type:

list[str]