TomlLibProvider
Base classes
| Name |
Children |
Inherits |
TomlProviderBase anyenv.toml_tools.base Base class for all TOML providers. |
|
|
⋔ Inheritance diagram
graph TD
3568317111["provider.TomlLibProvider"]
1311279371["base.TomlProviderBase"]
925645236["abc.ABC"]
2751680898["builtins.object"]
1311279371 --> 3568317111
925645236 --> 1311279371
2751680898 --> 925645236
🛈 DocStrings
Bases: TomlProviderBase
TOMLLIB implementation of the TOML provider interface.
Source code in src/anyenv/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( # noqa: TRY003
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(data: Any, *, pretty: bool = False) -> str
Dump data to TOML string using tomllib.
Source code in src/anyenv/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(data: str | bytes | TextIOWrapper | Path | UPath) -> Any
Load TOML using tomllib.
Source code in src/anyenv/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( # noqa: TRY003
f"Invalid TOML: {msg}",
line=line,
column=column,
source_path=source_path,
source_content=source_content,
) from exc
|
:fa-brands-github: Show source on GitHub