Debian Codegraph @

scripts/mkdocs_hooks.py

raw
1 # Generate a JSON schema of the Python handler configuration.
2
3 import json
4 from dataclasses import dataclass, fields
5 from os.path import join
6 from typing import Any
7
8 from mkdocs.config.defaults import MkDocsConfig
9 from mkdocs.plugins import get_plugin_logger
10
11 from mkdocstrings_handlers.python import PythonInputConfig, PythonInputOptions
12
13 # TODO: Update when Pydantic supports Python 3.14 (sources and duties as well).
14 try:
15 from pydantic import TypeAdapter
16 except ImportError:
17 TypeAdapter = None # type: ignore[assignment,misc]
18
19
20 _logger = get_plugin_logger(__name__)
21
22
23 def on_post_build(config: MkDocsConfig, **kwargs: Any) -> None: # noqa: ARG001
24 """Write `schema.json` to the site directory."""
25 if TypeAdapter is None:
26 _logger.info("Pydantic is not installed, skipping JSON schema generation")
27 return
28
29 @dataclass
30 class PythonHandlerSchema:
31 python: PythonInputConfig
32
33 adapter = TypeAdapter(PythonHandlerSchema)
34 schema = adapter.json_schema()
35 schema["$schema"] = "https://json-schema.org/draft-07/schema"
36 with open(join(config.site_dir, "schema.json"), "w") as file:
37 json.dump(schema, file, indent=2)
38 _logger.debug("Generated JSON schema")
39
40 autorefs = config["plugins"]["autorefs"]
41 for field in fields(PythonInputConfig):
42 if f"setting-{field.name}" not in autorefs._primary_url_map:
43 _logger.warning(f"Handler setting `{field.name}` is not documented")
44 for field in fields(PythonInputOptions):
45 if f"option-{field.name}" not in autorefs._primary_url_map:
46 _logger.warning(f"Configuration option `{field.name}` is not documented")