Add converter modules (JSON, YAML, TOML, CSV)
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
This commit is contained in:
39
src/converters/yaml_converter.py
Normal file
39
src/converters/yaml_converter.py
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
"""YAML converter module."""
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from .base import BaseConverter
|
||||||
|
|
||||||
|
|
||||||
|
class YAMLConverter(BaseConverter):
|
||||||
|
"""Converter for YAML format."""
|
||||||
|
|
||||||
|
format_name = 'yaml'
|
||||||
|
extensions = ('.yaml', '.yml')
|
||||||
|
|
||||||
|
def loads(self, content: str) -> Any:
|
||||||
|
"""Parse YAML string to Python object.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
content: YAML string content
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Parsed Python object
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
yaml.YAMLError: If content is not valid YAML
|
||||||
|
"""
|
||||||
|
return yaml.safe_load(content)
|
||||||
|
|
||||||
|
def dumps(self, data: Any, indent: int = 2) -> str:
|
||||||
|
"""Serialize Python object to YAML string.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
data: Python object to serialize
|
||||||
|
indent: Number of spaces for indentation (multiplied for nested levels)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
YAML string representation
|
||||||
|
"""
|
||||||
|
return yaml.dump(data, indent=indent, default_flow_style=False, allow_unicode=True)
|
||||||
Reference in New Issue
Block a user