Skip to content

tomllib_provider

Class info

Re-exported classes

Class Module Description
TomlLibProvider configz.toml_tools.tomllib_provider.provider TOMLLIB implementation of the TOML provider interface.

🛈 DocStrings

TOMLLIB provider package.

TomlLibProvider

Bases: TomlProviderBase

TOMLLIB implementation of the TOML provider interface.

Source code in src/configz/toml_tools/tomllib_provider/provider.py
class TomlLibProvider(TomlProviderBase):
    """TOMLLIB implementation of the TOML provider interface."""

    @staticmethod
    def load_toml(data: str | bytes | TextIOWrapper | Path | UPath) -> Any:
        """Load TOML using tomllib."""
        import tomllib

        try:
            source_content: str | None = None
            match data:
                case Path() | UPath():
                    content = data.read_bytes()
                    source_content = content.decode(errors="replace")
                    return tomllib.load(BytesIO(content))
                case TextIOWrapper():
                    text_content = data.read()
                    source_content = text_content
                    return tomllib.loads(text_content)
                case bytes():
                    source_content = data.decode(errors="replace")
                    return tomllib.loads(data.decode())
                case str():
                    source_content = data
                    return tomllib.loads(data)
        except tomllib.TOMLDecodeError as exc:
            msg, line, column = _extract_tomllib_error_info(exc, source_content)
            source_path: str | Path | None = (
                str(data) if isinstance(data, UPath) else (data if isinstance(data, Path) else None)
            )
            raise TomlLoadError(
                f"Invalid TOML: {msg}",
                line=line,
                column=column,
                source_path=source_path,
                source_content=source_content,
            ) from exc

    @staticmethod
    def dump_toml(data: Any, *, pretty: bool = False) -> str:
        """Dump data to TOML string using tomllib."""
        # tomllib is read-only, so we need to fallback to another library
        # or raise an error
        msg = "tomllib does not support writing TOML files (read-only library)"
        raise TomlDumpError(msg)

dump_toml staticmethod

dump_toml(data: Any, *, pretty: bool = False) -> str

Dump data to TOML string using tomllib.

Source code in src/configz/toml_tools/tomllib_provider/provider.py
@staticmethod
def dump_toml(data: Any, *, pretty: bool = False) -> str:
    """Dump data to TOML string using tomllib."""
    # tomllib is read-only, so we need to fallback to another library
    # or raise an error
    msg = "tomllib does not support writing TOML files (read-only library)"
    raise TomlDumpError(msg)

load_toml staticmethod

load_toml(data: str | bytes | TextIOWrapper | Path | UPath) -> Any

Load TOML using tomllib.

Source code in src/configz/toml_tools/tomllib_provider/provider.py
@staticmethod
def load_toml(data: str | bytes | TextIOWrapper | Path | UPath) -> Any:
    """Load TOML using tomllib."""
    import tomllib

    try:
        source_content: str | None = None
        match data:
            case Path() | UPath():
                content = data.read_bytes()
                source_content = content.decode(errors="replace")
                return tomllib.load(BytesIO(content))
            case TextIOWrapper():
                text_content = data.read()
                source_content = text_content
                return tomllib.loads(text_content)
            case bytes():
                source_content = data.decode(errors="replace")
                return tomllib.loads(data.decode())
            case str():
                source_content = data
                return tomllib.loads(data)
    except tomllib.TOMLDecodeError as exc:
        msg, line, column = _extract_tomllib_error_info(exc, source_content)
        source_path: str | Path | None = (
            str(data) if isinstance(data, UPath) else (data if isinstance(data, Path) else None)
        )
        raise TomlLoadError(
            f"Invalid TOML: {msg}",
            line=line,
            column=column,
            source_path=source_path,
            source_content=source_content,
        ) from exc