From 5f0a0b21fd4e15fc6339137e153c67a81b7e7517 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Wed, 4 Feb 2026 07:05:19 +0000 Subject: [PATCH] Initial upload: ConfigConvert CLI with full test suite and CI/CD --- config_convert/converters/base.py | 63 +++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 config_convert/converters/base.py diff --git a/config_convert/converters/base.py b/config_convert/converters/base.py new file mode 100644 index 0000000..85ca1ad --- /dev/null +++ b/config_convert/converters/base.py @@ -0,0 +1,63 @@ +"""Base converter class and factory for format-specific converters.""" + +from abc import ABC, abstractmethod +from typing import Any, Dict, List, Optional + + +class Converter(ABC): + """Abstract base class for format converters.""" + + @property + @abstractmethod + def name(self) -> str: + """Return the format name.""" + pass + + @abstractmethod + def loads(self, data: str) -> Dict[str, Any]: + """Parse a string into a dictionary.""" + pass + + @abstractmethod + def dumps(self, data: Dict[str, Any], indent: Optional[int] = None) -> str: + """Serialize a dictionary to a string.""" + pass + + @abstractmethod + def load(self, file_path: str) -> Dict[str, Any]: + """Read and parse a file.""" + pass + + @abstractmethod + def dump(self, data: Dict[str, Any], file_path: str, indent: Optional[int] = None) -> None: + """Serialize and write to a file.""" + pass + + +class ConverterFactory: + """Factory for creating format-specific converters.""" + + _converters: Dict[str, Converter] = {} + + @classmethod + def register(cls, format_name: str, converter: Converter) -> None: + """Register a converter for a format.""" + cls._converters[format_name.lower()] = converter + + @classmethod + def get(cls, format_name: str) -> Converter: + """Get a converter for the specified format.""" + format_lower = format_name.lower() + if format_lower not in cls._converters: + raise ValueError(f"Unsupported format: {format_name}") + return cls._converters[format_lower] + + @classmethod + def supported_formats(cls) -> List[str]: + """Return list of supported format names.""" + return list(cls._converters.keys()) + + @classmethod + def is_supported(cls, format_name: str) -> bool: + """Check if a format is supported.""" + return format_name.lower() in cls._converters