42 lines
1.0 KiB
Python
42 lines
1.0 KiB
Python
"""TOML converter module."""
|
|
|
|
import tomlkit
|
|
from typing import Any
|
|
|
|
from .base import BaseConverter
|
|
|
|
|
|
class TOMLConverter(BaseConverter):
|
|
"""Converter for TOML format."""
|
|
|
|
format_name = 'toml'
|
|
extensions = ('.toml',)
|
|
|
|
def loads(self, content: str) -> Any:
|
|
"""Parse TOML string to Python object.
|
|
|
|
Args:
|
|
content: TOML string content
|
|
|
|
Returns:
|
|
Parsed Python object
|
|
|
|
Raises:
|
|
tomlkit.exceptions.ParseError: If content is not valid TOML
|
|
"""
|
|
return tomlkit.parse(content)
|
|
|
|
def dumps(self, data: Any, indent: int = 2) -> str:
|
|
"""Serialize Python object to TOML string.
|
|
|
|
Args:
|
|
data: Python object to serialize
|
|
indent: Ignored for TOML (uses its own formatting)
|
|
|
|
Returns:
|
|
TOML string representation
|
|
"""
|
|
if isinstance(data, tomlkit.TOMLDocument):
|
|
return tomlkit.dumps(data)
|
|
return tomlkit.dumps(data)
|