Add formatters, utils, schemas and version
Some checks failed
CI / test (3.10) (push) Has been cancelled
CI / test (3.11) (push) Has been cancelled
CI / test (3.12) (push) Has been cancelled
CI / test (3.8) (push) Has been cancelled
CI / lint (push) Failing after 12s
CI / test (3.9) (push) Has been cancelled
CI / typecheck (push) Failing after 13s
CI / build-package (push) Failing after 14s
Some checks failed
CI / test (3.10) (push) Has been cancelled
CI / test (3.11) (push) Has been cancelled
CI / test (3.12) (push) Has been cancelled
CI / test (3.8) (push) Has been cancelled
CI / lint (push) Failing after 12s
CI / test (3.9) (push) Has been cancelled
CI / typecheck (push) Failing after 13s
CI / build-package (push) Failing after 14s
This commit is contained in:
54
configforge/formatters/yaml_handler.py
Normal file
54
configforge/formatters/yaml_handler.py
Normal file
@@ -0,0 +1,54 @@
|
||||
"""YAML format handler for ConfigForge."""
|
||||
|
||||
import yaml
|
||||
from typing import Any, Dict
|
||||
|
||||
from configforge.exceptions import ConversionError
|
||||
|
||||
|
||||
class YAMLHandler:
|
||||
"""Handler for YAML configuration files."""
|
||||
|
||||
@staticmethod
|
||||
def loads(content: str) -> Dict[str, Any]:
|
||||
"""Parse YAML content."""
|
||||
try:
|
||||
result = yaml.safe_load(content)
|
||||
return result if result is not None else {}
|
||||
except yaml.YAMLError as e:
|
||||
raise ConversionError(f"Invalid YAML: {str(e)}") from e
|
||||
|
||||
@staticmethod
|
||||
def dumps(data: Any, default_flow_style: bool = False) -> str:
|
||||
"""Serialize data to YAML string."""
|
||||
try:
|
||||
return yaml.safe_dump(data, default_flow_style=default_flow_style, allow_unicode=True)
|
||||
except (TypeError, ValueError) as e:
|
||||
raise ConversionError(f"Failed to serialize YAML: {str(e)}") from e
|
||||
|
||||
@staticmethod
|
||||
def read(filepath: str) -> Dict[str, Any]:
|
||||
"""Read and parse YAML file."""
|
||||
try:
|
||||
with open(filepath, "r", encoding="utf-8") as f:
|
||||
result = yaml.safe_load(f)
|
||||
return result if result is not None else {}
|
||||
except FileNotFoundError:
|
||||
raise ConversionError(f"File not found: {filepath}")
|
||||
except PermissionError:
|
||||
raise ConversionError(f"Permission denied: {filepath}")
|
||||
except yaml.YAMLError as e:
|
||||
raise ConversionError(f"Invalid YAML in {filepath}: {str(e)}") from e
|
||||
|
||||
@staticmethod
|
||||
def write(filepath: str, data: Any, default_flow_style: bool = False) -> None:
|
||||
"""Write data to YAML file."""
|
||||
try:
|
||||
with open(filepath, "w", encoding="utf-8") as f:
|
||||
yaml.safe_dump(data, f, default_flow_style=default_flow_style, allow_unicode=True)
|
||||
except FileNotFoundError:
|
||||
raise ConversionError(f"Directory not found for: {filepath}")
|
||||
except PermissionError:
|
||||
raise ConversionError(f"Permission denied to write: {filepath}")
|
||||
except (TypeError, ValueError) as e:
|
||||
raise ConversionError(f"Failed to write YAML: {str(e)}") from e
|
||||
Reference in New Issue
Block a user