FileDocumentLoader¶
Document loader that reads local files for file: URLs.
Accepts file: URLs, scheme-less absolute paths, and pathlib.Path
instances. Any other scheme raises JsonLdError. When root is set,
only paths that resolve under that directory are served.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
str | Path | None
|
optional directory that confines readable paths; when set, paths that resolve outside this directory are refused. |
None
|
FileDocumentLoader accepts file: URLs, scheme-less absolute paths, and
pathlib.Path instances. Its optional root constructor argument confines
the files it may read to a chosen directory.
Example file_basic.py
import json
from pathlib import Path
from pyld import FileDocumentLoader, jsonld
person = Path(__file__).resolve().parent.parent / 'data' / 'person.jsonld'
loader = FileDocumentLoader()
result = jsonld.expand(
person.as_uri(),
options={'documentLoader': loader},
)
print(json.dumps(result, indent=2))
Source person.jsonld
Content Types¶
The content type is chosen based on the file extension as follows:
| Extension | Content type |
|---|---|
.jsonld |
application/ld+json |
.json |
application/json |
.html, .htm |
text/html |
.xhtml |
application/xhtml+xml |
Unsupported extensions raise JsonLdError with code loading document failed.
Root Confinement¶
All requested paths, including symlink targets, must resolve beneath root,
so .. traversal and symlink escapes are rejected:
Example file_root.py
import json
from pathlib import Path
from pyld import FileDocumentLoader, jsonld
data_dir = Path(__file__).resolve().parent.parent / 'data'
person = data_dir / 'person.jsonld'
loader = FileDocumentLoader(root=data_dir)
result = jsonld.expand(
person.as_uri(),
options={'documentLoader': loader},
)
print(json.dumps(result, indent=2))
Source person.jsonld