Skip to content

Add ChoiceBySchemeDocumentLoader for scheme-based document loading #300

Description

@anatoly-scherbakov

Problem

FileDocumentLoader serves file: URLs only, and RequestsDocumentLoader / AioHttpDocumentLoader serve http/https only. A local JSON-LD document whose @context is a remote https: URL therefore cannot be processed by either loader alone.

Proposal

Add ChoiceBySchemeDocumentLoader, a DocumentLoader subclass that dispatches by URL scheme:

from pyld import (
    ChoiceBySchemeDocumentLoader,
    FileDocumentLoader,
    RequestsDocumentLoader,
)

http = RequestsDocumentLoader()
loader = ChoiceBySchemeDocumentLoader(
    file=FileDocumentLoader(),
    http=http,
    https=http,
)

Behavior

  • Constructed as ChoiceBySchemeDocumentLoader(**loaders), storing the keyword mapping as a scheme-to-loader dict.
  • __call__(url, options) delegates to self.loaders[urlparse(url).scheme or 'file'].
  • An empty / missing scheme is treated as file.
  • Values may be DocumentLoader instances or plain callables, per the loader contract in lib/pyld/documentloader/base.py.
  • An unregistered scheme raises JsonLdError with code loading document failed and details naming the schemes that are registered.

Out of scope

Do not change _default_document_loader in lib/pyld/jsonld.py. Serving file: by default would let any document with "@context": "file:///etc/passwd" read local files. Callers should opt in by passing the composed loader as documentLoader per operation.

Deliverables

  • lib/pyld/documentloader/choice_by_scheme.py
  • Export from lib/pyld/__init__.py
  • tests/test_choice_by_scheme_document_loader.py
  • docs/reference/document-loaders/choice-by-scheme.md following the shape of docs/reference/document-loaders/frozen.md, plus a card in docs/reference/document-loaders/index.md
  • CHANGELOG.md entry under the unreleased section

Related

  • Prerequisite: FileDocumentLoader (see branch file-document-loader)
  • Enables a future CLI for command-line tool #151

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions