Python API¶
Use pystac_ext_generator when embedding generation in another Python tool:
import json
from pathlib import Path
from pystac_ext_generator import generate
schema = json.loads(Path("quality-schema.json").read_text(encoding="utf-8"))
source, warnings = generate(schema, include_validation=True)
Path("quality_extension.py").write_text(source, encoding="utf-8")
for warning in warnings:
print(warning)
generate() returns source text and diagnostics. It checks the generated Python's
syntax without executing it, then normalizes imports and formats the source with Ruff.
prefix filters field namespaces. include_validation=True adds a standalone
offline validator; its default is false. The full schema is retained whenever
validation or nested union decoding requires it.
Generation¶
pystac_ext_generator.generate(document: dict[str, Any], *, prefix: str | None = None, include_validation: bool = False, documents: Mapping[str, dict[str, Any]] | None = None) -> tuple[str, list[str]]
¶
Return Python source and diagnostics. No generated code is executed.
The schema title determines the extension class name and its canonical $id determines registration. Fields and applicable STAC objects are discovered from schema paths and references. Embedded offline validation is opt-in.
| Parameters: |
|
|---|
| Raises: |
|
|---|
Source code in src/pystac_ext_generator/generator.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
External resource bundling¶
Call bundle() before generate() when external references are present:
from pystac_ext_generator.bundle import bundle
external = json.loads(Path("common-schema.json").read_text(encoding="utf-8"))
schema = bundle(schema, documents={"https://example.org/common/schema.json": external})
source, warnings = generate(schema)
An optional fetch callback loads resources absent from the mapping. Pass
fetch_http to opt into network access, or provide an application-specific loader.
pystac_ext_generator.bundle.bundle(document: dict[str, Any], *, documents: Mapping[str, dict[str, Any]] | None = None, fetch: Callable[[str], dict[str, Any]] | None = None) -> dict[str, Any]
¶
Return an eagerly dereferenced schema graph without mutating the input.
| Parameters: |
|
|---|
| Raises: |
|
|---|
Source code in src/pystac_ext_generator/bundle.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
pystac_ext_generator.bundle.fetch_http(uri: str) -> dict[str, Any]
¶
Fetch a JSON schema from an HTTP(S) URL with a 30-second timeout.
| Raises: |
|
|---|
Source code in src/pystac_ext_generator/bundle.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
Errors¶
pystac_ext_generator.SchemaError
¶
Bases: ValueError
The input cannot be represented safely by the generator.
Source code in src/pystac_ext_generator/schema.py
28 29 | |
Malformed JSON, file errors, and network errors can also arise while loading input. The CLI presents these as command errors. See generated classes for the runtime API of the emitted module.