RtomlProvider
Base classes
| Name |
Children |
Inherits |
TomlProviderBase anyenv.toml_tools.base Base class for all TOML providers. |
|
|
â‹” Inheritance diagram
graph TD
96264333["provider.RtomlProvider"]
1311279371["base.TomlProviderBase"]
925645236["abc.ABC"]
2751680898["builtins.object"]
1311279371 --> 96264333
925645236 --> 1311279371
2751680898 --> 925645236
🛈 DocStrings
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(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(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
|
:fa-brands-github: Show source on GitHub