Skip to content

base

Class info

Classes

Name Children Inherits
ConfigFile
mkdocs_mknodes.appconfig.base
Base class for config files.

🛈 DocStrings

ConfigFile

Bases: BaseModel

Base class for config files.

A class for loading and managing configuration files in YAML format. Supports inheritance between config files and provides methods for loading from files or YAML text.

Parameters:

Name Type Description Default
config_file_path str | None
None
inherit str | None
None
Source code in mkdocs_mknodes/appconfig/base.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class ConfigFile(BaseModel):
    """Base class for config files.

    A class for loading and managing configuration files in YAML format.
    Supports inheritance between config files and provides methods for
    loading from files or YAML text.
    """

    config_file_path: str | None = Field(None, exclude=True)
    # Either exclude here or add inherit field to MkNodesConfig?
    inherit: str | None = Field(None, exclude=True, alias="INHERIT")
    """Define the parent for a configuration file.

    The path must be relative to the location of the primary file.
    """

    @classmethod
    def from_yaml_file(cls, yaml_path: str | os.PathLike[str], **overrides: Any) -> Self:
        """Create a ConfigFile instance from a YAML file.

        Args:
            yaml_path: Path to the YAML configuration file
            **overrides: Additional key-value pairs to override values from the file

        Returns:
            A new instance of the ConfigFile class initialized with the YAML data
        """
        cfg = yamling.load_yaml_file(yaml_path)
        vals = {"config_file_path": str(yaml_path), **cfg, **overrides}
        return cls(**vals)

    @classmethod
    def from_yaml(cls, text: yamling.YAMLInput, **overrides: Any) -> Self:
        """Create a ConfigFile instance from YAML text.

        Args:
            text: YAML content as text or file-like object
            **overrides: Additional key-value pairs to override values from the YAML

        Returns:
            A new instance of the ConfigFile class initialized with the YAML data
        """
        cfg = yamling.load_yaml(text, resolve_inherit=True)
        path = {"config_file_path": str(text.name)} if getattr(text, "name", None) else {}  # type: ignore
        vals = {**path, **cfg, **overrides}
        return cls(**vals)

    def to_yaml(self) -> str:
        """Convert the configuration to YAML format.

        Returns:
            A string containing the YAML representation of the configuration
        """
        cfg = self.model_dump()
        return yamling.dump_yaml(cfg)

inherit class-attribute instance-attribute

inherit: str | None = Field(None, exclude=True, alias='INHERIT')

Define the parent for a configuration file.

The path must be relative to the location of the primary file.

from_yaml classmethod

from_yaml(text: YAMLInput, **overrides: Any) -> Self

Create a ConfigFile instance from YAML text.

Parameters:

Name Type Description Default
text YAMLInput

YAML content as text or file-like object

required
**overrides Any

Additional key-value pairs to override values from the YAML

{}

Returns:

Type Description
Self

A new instance of the ConfigFile class initialized with the YAML data

Source code in mkdocs_mknodes/appconfig/base.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
@classmethod
def from_yaml(cls, text: yamling.YAMLInput, **overrides: Any) -> Self:
    """Create a ConfigFile instance from YAML text.

    Args:
        text: YAML content as text or file-like object
        **overrides: Additional key-value pairs to override values from the YAML

    Returns:
        A new instance of the ConfigFile class initialized with the YAML data
    """
    cfg = yamling.load_yaml(text, resolve_inherit=True)
    path = {"config_file_path": str(text.name)} if getattr(text, "name", None) else {}  # type: ignore
    vals = {**path, **cfg, **overrides}
    return cls(**vals)

from_yaml_file classmethod

from_yaml_file(yaml_path: str | PathLike[str], **overrides: Any) -> Self

Create a ConfigFile instance from a YAML file.

Parameters:

Name Type Description Default
yaml_path str | PathLike[str]

Path to the YAML configuration file

required
**overrides Any

Additional key-value pairs to override values from the file

{}

Returns:

Type Description
Self

A new instance of the ConfigFile class initialized with the YAML data

Source code in mkdocs_mknodes/appconfig/base.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@classmethod
def from_yaml_file(cls, yaml_path: str | os.PathLike[str], **overrides: Any) -> Self:
    """Create a ConfigFile instance from a YAML file.

    Args:
        yaml_path: Path to the YAML configuration file
        **overrides: Additional key-value pairs to override values from the file

    Returns:
        A new instance of the ConfigFile class initialized with the YAML data
    """
    cfg = yamling.load_yaml_file(yaml_path)
    vals = {"config_file_path": str(yaml_path), **cfg, **overrides}
    return cls(**vals)

to_yaml

to_yaml() -> str

Convert the configuration to YAML format.

Returns:

Type Description
str

A string containing the YAML representation of the configuration

Source code in mkdocs_mknodes/appconfig/base.py
57
58
59
60
61
62
63
64
def to_yaml(self) -> str:
    """Convert the configuration to YAML format.

    Returns:
        A string containing the YAML representation of the configuration
    """
    cfg = self.model_dump()
    return yamling.dump_yaml(cfg)