|
| 1 | +"""Configuration and options dataclasses.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import sys |
| 6 | +from dataclasses import field |
| 7 | +from typing import TYPE_CHECKING, Annotated, Any |
| 8 | + |
| 9 | +# YORE: EOL 3.10: Replace block with line 2. |
| 10 | +if sys.version_info >= (3, 11): |
| 11 | + from typing import Self |
| 12 | +else: |
| 13 | + from typing_extensions import Self |
| 14 | + |
| 15 | +try: |
| 16 | + # When Pydantic is available, use it to validate options (done automatically). |
| 17 | + # Users can therefore opt into validation by installing Pydantic in development/CI. |
| 18 | + # When building the docs to deploy them, Pydantic is not required anymore. |
| 19 | + |
| 20 | + # When building our own docs, Pydantic is always installed (see `docs` group in `pyproject.toml`) |
| 21 | + # to allow automatic generation of a JSON Schema. The JSON Schema is then referenced by mkdocstrings, |
| 22 | + # which is itself referenced by mkdocs-material's schema system. For example in VSCode: |
| 23 | + # |
| 24 | + # "yaml.schemas": { |
| 25 | + # "https://squidfunk.github.io/mkdocs-material/schema.json": "mkdocs.yml" |
| 26 | + # } |
| 27 | + from inspect import cleandoc |
| 28 | + |
| 29 | + from pydantic import Field as BaseField |
| 30 | + from pydantic.dataclasses import dataclass |
| 31 | + |
| 32 | + _base_url = "https://mkdocstrings.github.io/shell/configuration" |
| 33 | + |
| 34 | + def Field( # noqa: N802, D103 |
| 35 | + *args: Any, |
| 36 | + description: str, |
| 37 | + parent: str | None = None, |
| 38 | + **kwargs: Any, |
| 39 | + ) -> None: |
| 40 | + def _add_markdown_description(schema: dict[str, Any]) -> None: |
| 41 | + url = f"{_base_url}/#{parent or schema['title']}" |
| 42 | + schema["markdownDescription"] = f"[DOCUMENTATION]({url})\n\n{schema['description']}" |
| 43 | + |
| 44 | + return BaseField( |
| 45 | + *args, |
| 46 | + description=cleandoc(description), |
| 47 | + field_title_generator=lambda name, _: name, |
| 48 | + json_schema_extra=_add_markdown_description, |
| 49 | + **kwargs, |
| 50 | + ) |
| 51 | +except ImportError: |
| 52 | + from dataclasses import dataclass # type: ignore[no-redef] |
| 53 | + |
| 54 | + def Field(*args: Any, **kwargs: Any) -> None: # type: ignore[misc] # noqa: D103, N802 |
| 55 | + pass |
| 56 | + |
| 57 | + |
| 58 | +if TYPE_CHECKING: |
| 59 | + from collections.abc import MutableMapping |
| 60 | + |
| 61 | + |
| 62 | +# YORE: EOL 3.9: Remove block. |
| 63 | +_dataclass_options = {"frozen": True} |
| 64 | +if sys.version_info >= (3, 10): |
| 65 | + _dataclass_options["kw_only"] = True |
| 66 | + |
| 67 | + |
| 68 | +# YORE: EOL 3.9: Replace `**_dataclass_options` with `frozen=True, kw_only=True` within line. |
| 69 | +@dataclass(**_dataclass_options) # type: ignore[call-overload] |
| 70 | +class ShellInputOptions: |
| 71 | + """Accepted input options.""" |
| 72 | + |
| 73 | + extra: Annotated[ |
| 74 | + dict[str, Any], |
| 75 | + Field(description="Extra options."), |
| 76 | + ] = field(default_factory=dict) |
| 77 | + |
| 78 | + heading_level: Annotated[ |
| 79 | + int, |
| 80 | + Field(description="The initial heading level to use."), |
| 81 | + ] = 2 |
| 82 | + |
| 83 | + show_root_heading: Annotated[ |
| 84 | + bool, |
| 85 | + Field( |
| 86 | + description="""Show the heading of the object at the root of the documentation tree. |
| 87 | +
|
| 88 | + The root object is the object referenced by the identifier after `:::`. |
| 89 | + """, |
| 90 | + ), |
| 91 | + ] = False |
| 92 | + |
| 93 | + show_root_toc_entry: Annotated[ |
| 94 | + bool, |
| 95 | + Field( |
| 96 | + description="If the root heading is not shown, at least add a ToC entry for it.", |
| 97 | + ), |
| 98 | + ] = True |
| 99 | + |
| 100 | + @classmethod |
| 101 | + def coerce(cls, **data: Any) -> MutableMapping[str, Any]: |
| 102 | + """Coerce data.""" |
| 103 | + return data |
| 104 | + |
| 105 | + @classmethod |
| 106 | + def from_data(cls, **data: Any) -> Self: |
| 107 | + """Create an instance from a dictionary.""" |
| 108 | + return cls(**cls.coerce(**data)) |
| 109 | + |
| 110 | + |
| 111 | +# YORE: EOL 3.9: Replace `**_dataclass_options` with `frozen=True, kw_only=True` within line. |
| 112 | +@dataclass(**_dataclass_options) # type: ignore[call-overload] |
| 113 | +class ShellOptions(ShellInputOptions): # type: ignore[override,unused-ignore] |
| 114 | + """Final options passed as template context.""" |
| 115 | + |
| 116 | + |
| 117 | +# YORE: EOL 3.9: Replace `**_dataclass_options` with `frozen=True, kw_only=True` within line. |
| 118 | +@dataclass(**_dataclass_options) # type: ignore[call-overload] |
| 119 | +class ShellInputConfig: |
| 120 | + """Python handler configuration.""" |
| 121 | + |
| 122 | + options: Annotated[ |
| 123 | + ShellInputOptions, |
| 124 | + Field(description="Configuration options for collecting and rendering objects."), |
| 125 | + ] = field(default_factory=ShellInputOptions) |
| 126 | + |
| 127 | + |
| 128 | +# YORE: EOL 3.9: Replace `**_dataclass_options` with `frozen=True, kw_only=True` within line. |
| 129 | +@dataclass(**_dataclass_options) # type: ignore[call-overload] |
| 130 | +class ShellConfig(ShellInputConfig): # type: ignore[override,unused-ignore] |
| 131 | + """Shell handler configuration.""" |
| 132 | + |
| 133 | + options: dict[str, Any] = field(default_factory=dict) # type: ignore[assignment] |
0 commit comments