33 lines
1.0 KiB
Python
33 lines
1.0 KiB
Python
"""YAML format converter using PyYAML."""
|
|
|
|
import yaml
|
|
from pathlib import Path
|
|
from typing import Any, Dict, Optional
|
|
|
|
from config_convert.converters.base import Converter
|
|
|
|
|
|
class YAMLConverter(Converter):
|
|
"""Converter for YAML format."""
|
|
|
|
@property
|
|
def name(self) -> str:
|
|
return "yaml"
|
|
|
|
def loads(self, data: str) -> Dict[str, Any]:
|
|
return yaml.safe_load(data)
|
|
|
|
def dumps(self, data: Dict[str, Any], indent: Optional[int] = None) -> str:
|
|
if indent is None:
|
|
indent = 2
|
|
return yaml.safe_dump(data, indent=indent, allow_unicode=True)
|
|
|
|
def load(self, file_path: str) -> Dict[str, Any]:
|
|
with open(file_path, "r", encoding="utf-8") as f:
|
|
return yaml.safe_load(f)
|
|
|
|
def dump(self, data: Dict[str, Any], file_path: str, indent: Optional[int] = None) -> None:
|
|
Path(file_path).parent.mkdir(parents=True, exist_ok=True)
|
|
with open(file_path, "w", encoding="utf-8") as f:
|
|
yaml.safe_dump(data, f, indent=indent, allow_unicode=True)
|