Skip to content

rtoml_provider

Class info

Re-exported classes

Class Module Description
RtomlProvider configz.toml_tools.rtoml_provider.provider RTOML implementation of the TOML provider interface.

🛈 DocStrings

RTOML provider package.

RtomlProvider

Bases: TomlProviderBase

RTOML implementation of the TOML provider interface.

Source code in src/configz/toml_tools/rtoml_provider/provider.py
class RtomlProvider(TomlProviderBase):
    """RTOML implementation of the TOML provider interface."""

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

        try:
            source_content: str | None = None
            source_path: Path | UPath | None = None
            match data:
                case Path():
                    source_path = data
                    source_content = data.read_text(encoding="utf-8")
                    return rtoml.load(data)
                case UPath():
                    source_path = data
                    source_content = data.read_text("utf-8")
                    return rtoml.loads(source_content)
                case TextIOWrapper():
                    content = data.read()
                    source_content = content
                    return rtoml.loads(content)
                case bytes():
                    content = data.decode()
                    source_content = content
                    return rtoml.loads(content)
                case str():
                    source_content = data
                    return rtoml.loads(data)
        except Exception as exc:
            msg, line, column = _extract_rtoml_error_info(exc)
            raise TomlLoadError(
                f"Invalid TOML: {msg}",
                line=line,
                column=column,
                source_path=str(source_path) if isinstance(source_path, UPath) else source_path,
                source_content=source_content,
            ) from exc

    @staticmethod
    def dump_toml(data: Any, *, pretty: bool = False) -> str:
        """Dump data to TOML string using rtoml."""
        import rtoml

        try:
            return rtoml.dumps(data, pretty=pretty)
        except Exception as exc:
            error_msg = f"Cannot serialize to TOML: {exc}"
            raise TomlDumpError(error_msg) from exc

dump_toml staticmethod

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

Dump data to TOML string using rtoml.

Source code in src/configz/toml_tools/rtoml_provider/provider.py
@staticmethod
def dump_toml(data: Any, *, pretty: bool = False) -> str:
    """Dump data to TOML string using rtoml."""
    import rtoml

    try:
        return rtoml.dumps(data, pretty=pretty)
    except Exception as exc:
        error_msg = f"Cannot serialize to TOML: {exc}"
        raise TomlDumpError(error_msg) from exc

load_toml staticmethod

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

Load TOML using rtoml.

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

    try:
        source_content: str | None = None
        source_path: Path | UPath | None = None
        match data:
            case Path():
                source_path = data
                source_content = data.read_text(encoding="utf-8")
                return rtoml.load(data)
            case UPath():
                source_path = data
                source_content = data.read_text("utf-8")
                return rtoml.loads(source_content)
            case TextIOWrapper():
                content = data.read()
                source_content = content
                return rtoml.loads(content)
            case bytes():
                content = data.decode()
                source_content = content
                return rtoml.loads(content)
            case str():
                source_content = data
                return rtoml.loads(data)
    except Exception as exc:
        msg, line, column = _extract_rtoml_error_info(exc)
        raise TomlLoadError(
            f"Invalid TOML: {msg}",
            line=line,
            column=column,
            source_path=str(source_path) if isinstance(source_path, UPath) else source_path,
            source_content=source_content,
        ) from exc