Skip to content

yamlconfig

Class info

Classes

Name Children Inherits
Tagged
mkdocs_mknodes.generator.yamlconfig
A YAML node with a custom tag, kept verbatim.

    🛈 DocStrings

    Round-trip helpers for MkDocs-style YAML config files.

    MkDocs configs may contain custom tags (!ENV, !!python/name:...). The generator never executes them: it loads them as Tagged values and dumps them back unchanged, so a generated config can safely repeat values of the config it inherits from.

    Tagged dataclass

    A YAML node with a custom tag, kept verbatim.

    Parameters:

    Name Type Description Default
    tag str
    required
    value Any
    required
    Source code in mkdocs_mknodes/generator/yamlconfig.py
    @dataclasses.dataclass
    class Tagged:
        """A YAML node with a custom tag, kept verbatim."""
    
        tag: str
        value: Any
    

    dumps

    dumps(data: Any) -> str
    

    Dump data to YAML (Tagged values and callables become tags again).

    Source code in mkdocs_mknodes/generator/yamlconfig.py
    def dumps(data: Any) -> str:
        """Dump data to YAML (`Tagged` values and callables become tags again)."""
        return yaml.dump(data, Dumper=_Dumper, sort_keys=False, allow_unicode=True, width=100)
    

    load_file

    load_file(path: str | PathLike[str]) -> dict[str, Any]
    

    Load a MkDocs YAML config file and resolve INHERIT chains.

    Source code in mkdocs_mknodes/generator/yamlconfig.py
    def load_file(path: str | os.PathLike[str]) -> dict[str, Any]:
        """Load a MkDocs YAML config file and resolve `INHERIT` chains."""
        path = pathlib.Path(path)
        data = loads(path.read_text(encoding="utf-8")) or {}
        if parent_path := data.pop("INHERIT", None):
            parent = load_file(path.parent / str(parent_path))
            data = merge_inherited(parent, data)
        return data
    

    loads

    loads(text: str) -> Any
    

    Load YAML text, keeping custom tags as Tagged values.

    Source code in mkdocs_mknodes/generator/yamlconfig.py
    def loads(text: str) -> Any:
        """Load YAML text, keeping custom tags as `Tagged` values."""
        return yaml.load(text, Loader=_Loader)
    

    merge_inherited

    merge_inherited(parent: dict[str, Any], child: dict[str, Any]) -> dict[str, Any]
    

    Merge configs the way MkDocs INHERIT does: mappings merge, the rest is replaced.

    Source code in mkdocs_mknodes/generator/yamlconfig.py
    def merge_inherited(parent: dict[str, Any], child: dict[str, Any]) -> dict[str, Any]:
        """Merge configs the way MkDocs `INHERIT` does: mappings merge, the rest is replaced."""
        for key, value in child.items():
            if isinstance(parent.get(key), dict) and isinstance(value, dict):
                parent[key] = merge_inherited(parent[key], value)
            else:
                parent[key] = value
        return parent
    

    resolve

    resolve(value: Any) -> Any
    

    Resolve !ENV tags in given value (recursively) to plain Python values.

    Source code in mkdocs_mknodes/generator/yamlconfig.py
    def resolve(value: Any) -> Any:
        """Resolve `!ENV` tags in given value (recursively) to plain Python values."""
        match value:
            case Tagged(tag="!ENV", value=val):
                names = val if isinstance(val, list) else [val]
                default = names[-1] if isinstance(val, list) and len(names) > 1 else None
                if isinstance(val, list) and len(names) > 1:
                    names = names[:-1]
                for name in names:
                    if name in os.environ:
                        return yaml.safe_load(os.environ[name])
                return default
            case dict():
                return {k: resolve(v) for k, v in value.items()}
            case list():
                return [resolve(v) for v in value]
            case _:
                return value
    
    Page info
    Resources
    Resources(css=[],
              markdown_extensions={'tables': {}},
              plugins=[Plugin('mkdocstrings')],
              js=[],
              assets=[],
              packages=[])
    
    Metadata
    created:
      source_filename: /home/runner/work/mkdocs-mknodes/mkdocs-mknodes/.venv/lib/python3.13/site-packages/mknodes/pages/mkmodulepage/__init__.py
      source_function: MkModulePage.__init__
      source_line_no: 52
    title: yamlconfig