Skip to content

ContextResolver

ContextResolver resolves remote @context documents and caches the resolved contexts used by JSON-LD operations.

Resolves and caches remote contexts.

__init__

__init__(shared_cache, document_loader, max_context_urls: int = MAX_CONTEXT_URLS)

Creates a ContextResolver.

Parameters:

Name Type Description Default
shared_cache

the shared document cache.

required
document_loader

the document loader.

required
max_context_urls int

the maximum number of times to recusively fetch contexts. (default MAX_CONTEXT_URLS).

MAX_CONTEXT_URLS

resolve

resolve(active_ctx, context, base, cycles=None)

Resolve a context.

Parameters:

Name Type Description Default
active_ctx

the current active context.

required
context

the context to resolve.

required
base

the absolute URL to use for making url absolute.

required
cycles

the set to store fetched contexts and detect cycles. (default None).

None

Pass a custom ContextResolver with the contextResolver option when you need to provide your own resolved-context cache or adjust the remote-context recursion limit. The resolver still uses a DocumentLoader to fetch remote context documents, so pass the same loader with documentLoader.

Example context_resolver.py

import json

from cachetools import LRUCache

from pyld import ContextResolver, DocumentLoader, jsonld

CONTEXT_URL = "context://my-app/vocab"

DOCUMENT_CACHE = {
    CONTEXT_URL: {
        "contentType": "application/ld+json",
        "contextUrl": None,
        "documentUrl": CONTEXT_URL,
        "document": {
            "@context": {
                "name": "https://schema.org/name",
                "homepage": {"@id": "https://schema.org/url", "@type": "@id"},
            }
        },
    }
}


class CachedContextLoader(DocumentLoader):
    def __init__(self, documents):
        self.documents = documents
        self.load_count = 0

    def __call__(self, url, options):
        self.load_count += 1
        return self.documents[url]


loader = CachedContextLoader(DOCUMENT_CACHE)
resolved_context_cache = LRUCache(maxsize=1000)
resolver = ContextResolver(
    resolved_context_cache,
    loader,
    max_context_urls=20,
)

doc = {
    "@context": CONTEXT_URL,
    "name": "Example Person",
    "homepage": "https://example.com/",
}
options = {"documentLoader": loader, "contextResolver": resolver}

expanded = jsonld.expand(doc, options=options)
jsonld.expand(doc, options=options)

print(json.dumps({"contextLoads": loader.load_count, "expanded": expanded}, indent=2))
Output
{
  "contextLoads": 1,
  "expanded": [
    {
      "https://schema.org/url": [
        {
          "@id": "https://example.com/"
        }
      ],
      "https://schema.org/name": [
        {
          "@value": "Example Person"
        }
      ]
    }
  ]
}

The shared_cache object must behave like a mutable mapping. PyLD uses cachetools.LRUCache by default, but applications can provide another mapping when they need a different eviction policy.

Use max_context_urls to change how many remote contexts may be fetched while resolving nested or imported contexts.