Skip to main content

agrag.loaders.corpus.registry.LoaderRegistry

LoaderRegistry() -> None

Maps a source extension to the loader that reads it.

The registry picks a loader by file extension first. When more than one loader claims the same extension, the loader registered with prefer=True wins; when several loaders are preferred, the last preferred registration wins.

Attributes:

  • _by_extension (dict[str, list[_Entry]]) – The registered loaders for each extension, in registration order.

Functions:

  • for_source – Return the default loader for a source.
  • register – Add a loader to the registry.

for_source​

for_source(source:SourceRef) -> Loader

Return the default loader for a source.

Parameters:

  • source (SourceRef) – The source to find a loader for.

Returns:

  • Loader – The registered loader with the highest precedence for the source's extension.

When the top-precedence loader needs a package extra that is not installed, the first non-preferred loader for the extension whose extra (if any) is installed is used instead, so an optional loader's absence falls back to the core reader rather than always failing the source.

Raises:

  • UnsupportedFormatError – No loader claims the source's extension.
  • MissingExtraError – A loader is mapped to the extension, but its package extra failed to import, and no fallback loader is available either.

register​

register(loader:Loader, *, prefer:bool = False, extensions:set[str] | frozenset[str] | None = None) -> None

Add a loader to the registry.

Registering the same loader for the same extension more than once is a no-op, so importing a package that registers loaders repeatedly stays safe.

Parameters:

  • loader (Loader) – The loader to register.
  • prefer (bool) – Set this to True to make the loader the default for its extensions. Leave it False to register the loader only as an explicit, named option.
  • extensions (set[str] | frozenset[str] | None) – Only register loader for these extensions. Defaults to every extension the loader advertises. A caller that wants different precedence per extension registers the same loader twice with different extensions sets.