Skip to content

provider

Class info

Classes

Name Children Inherits
PytomlppProvider
anyenv.toml_tools.pytomlpp_provider.provider
PyTOMLPP implementation of the TOML provider interface.

    🛈 DocStrings

    PyTOMLPP provider implementation.

    PytomlppProvider

    Bases: TomlProviderBase

    PyTOMLPP implementation of the TOML provider interface.

    Source code in src/anyenv/toml_tools/pytomlpp_provider/provider.py
    class PytomlppProvider(TomlProviderBase):
        """PyTOMLPP implementation of the TOML provider interface."""
    
        @staticmethod
        def load_toml(data: str | bytes | TextIOWrapper | Path | UPath) -> Any:
            """Load TOML using pytomlpp."""
            import pytomlpp
    
            try:
                source_content: str | None = None
                source_path: Path | UPath | None = None
                match data:
                    case Path() | UPath():
                        source_path = data
                        content = data.read_text("utf-8")
                        source_content = content
                        return pytomlpp.loads(content)
                    case TextIOWrapper():
                        content = data.read()
                        source_content = content
                        return pytomlpp.loads(content)
                    case bytes():
                        content = data.decode()
                        source_content = content
                        return pytomlpp.loads(content)
                    case str():
                        source_content = data
                        return pytomlpp.loads(data)
            except Exception as exc:
                msg, line, column = _extract_pytomlpp_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 pytomlpp."""
            import pytomlpp
    
            try:
                # pytomlpp doesn't have a pretty option, it always formats nicely
                result: str = pytomlpp.dumps(data)
            except Exception as exc:
                error_msg = f"Cannot serialize to TOML: {exc}"
                raise TomlDumpError(error_msg) from exc
            else:
                return result
    

    dump_toml staticmethod

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

    Dump data to TOML string using pytomlpp.

    Source code in src/anyenv/toml_tools/pytomlpp_provider/provider.py
    @staticmethod
    def dump_toml(data: Any, *, pretty: bool = False) -> str:
        """Dump data to TOML string using pytomlpp."""
        import pytomlpp
    
        try:
            # pytomlpp doesn't have a pretty option, it always formats nicely
            result: str = pytomlpp.dumps(data)
        except Exception as exc:
            error_msg = f"Cannot serialize to TOML: {exc}"
            raise TomlDumpError(error_msg) from exc
        else:
            return result
    

    load_toml staticmethod

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

    Load TOML using pytomlpp.

    Source code in src/anyenv/toml_tools/pytomlpp_provider/provider.py
    @staticmethod
    def load_toml(data: str | bytes | TextIOWrapper | Path | UPath) -> Any:
        """Load TOML using pytomlpp."""
        import pytomlpp
    
        try:
            source_content: str | None = None
            source_path: Path | UPath | None = None
            match data:
                case Path() | UPath():
                    source_path = data
                    content = data.read_text("utf-8")
                    source_content = content
                    return pytomlpp.loads(content)
                case TextIOWrapper():
                    content = data.read()
                    source_content = content
                    return pytomlpp.loads(content)
                case bytes():
                    content = data.decode()
                    source_content = content
                    return pytomlpp.loads(content)
                case str():
                    source_content = data
                    return pytomlpp.loads(data)
        except Exception as exc:
            msg, line, column = _extract_pytomlpp_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