Initial upload: ConfDoc v0.1.0 - Config validation and documentation generator

This commit is contained in:
2026-01-31 07:10:12 +00:00
parent fecf8bd78e
commit 20be5526d5

View File

@@ -0,0 +1,60 @@
from typing import Dict, Optional, Type
from confdoc.parsers.json_parser import JsonParser
from confdoc.parsers.yaml_parser import YamlParser
from confdoc.parsers.toml_parser import TomlParser
class ConfigParserFactory:
"""Factory for creating configuration parsers based on format."""
def __init__(self):
self._parsers: Dict[str, object] = {
"json": JsonParser(),
"yaml": YamlParser(),
"toml": TomlParser(),
}
self._format_detection = {
".json": "json",
".yaml": "yaml",
".yml": "yaml",
".toml": "toml",
}
def get_parser(self, format: Optional[str] = None):
"""Get a parser for the specified format."""
if format is None:
format = "json"
parser = self._parsers.get(format.lower())
if parser is None:
raise ValueError(f"Unsupported format: {format}")
return parser
def get_parser_for_file(self, file_path: str):
"""Detect format from file path and return appropriate parser."""
import os
ext = os.path.splitext(file_path)[1].lower()
format = self._format_detection.get(ext, "json")
return self.get_parser(format)
def detect_format(self, content: str) -> str:
"""Detect format from content."""
content = content.strip()
if content.startswith("{"):
return "json"
elif content.startswith("["):
return "toml"
elif content.startswith("#"):
return "yaml"
elif ":" in content and "=" not in content.split(":")[0]:
return "yaml"
elif "=" in content and not content.startswith("{"):
return "toml"
return "json"
def register_parser(self, format: str, parser: object):
"""Register a new parser."""
self._parsers[format] = parser