Skip to content

provider

Class info

Classes

Name Children Inherits
RtomlProvider
anyenv.toml_tools.rtoml_provider.provider
RTOML implementation of the TOML provider interface.

    🛈 DocStrings

    RTOML provider implementation.

    RtomlProvider

    Bases: TomlProviderBase

    RTOML implementation of the TOML provider interface.

    Source code in src/anyenv/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(  # noqa: TRY003
                    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/anyenv/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/anyenv/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(  # noqa: TRY003
                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