Files
config-convert-cli/config_convert/converters/yaml_converter.py
7000pctAUTO 2fe1bd8843
Some checks failed
CI / test (push) Has been cancelled
CI / lint (push) Has been cancelled
Initial upload: ConfigConvert CLI with full test suite and CI/CD
2026-02-04 07:05:21 +00:00

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)