diff --git a/.code_doc_cli/utils/config.py b/.code_doc_cli/utils/config.py new file mode 100644 index 0000000..cc1cc30 --- /dev/null +++ b/.code_doc_cli/utils/config.py @@ -0,0 +1,107 @@ +"""Configuration handling.""" + +import os +from dataclasses import dataclass, field +from typing import Optional +import toml + + +@dataclass +class Config: + """Configuration for code-doc-cli.""" + + input_patterns: list[str] = field(default_factory=lambda: ["**/*.py", "**/*.ts", "**/*.go"]) + output_file: Optional[str] = None + language: Optional[str] = None + template_style: str = "default" + syntax_highlighting: bool = True + theme: str = "default" + fail_on_warning: bool = False + exclude_patterns: list[str] = field(default_factory=list) + include_private: bool = False + output_format: str = "markdown" + + def to_dict(self) -> dict: + """Convert config to dictionary.""" + return { + "input_patterns": self.input_patterns, + "output_file": self.output_file, + "language": self.language, + "template_style": self.template_style, + "syntax_highlighting": self.syntax_highlighting, + "theme": self.theme, + "fail_on_warning": self.fail_on_warning, + "exclude_patterns": self.exclude_patterns, + "include_private": self.include_private, + "output_format": self.output_format, + } + + +def load_config(config_path: Optional[str] = None) -> Config: + """Load configuration from file. + + Args: + config_path: Path to config file (code-doc.toml) + + Returns: + Config object + """ + config = Config() + + if not config_path: + config_path = os.environ.get("CODE_DOC_CONFIG") + if not config_path: + config_path = os.path.join(os.getcwd(), "code-doc.toml") + if not os.path.exists(config_path): + return config + + if not os.path.exists(config_path): + return config + + try: + with open(config_path, "r", encoding="utf-8") as f: + data = toml.load(f) + + if "tool" in data and "code-doc" in data["tool"]: + config_data = data["tool"]["code-doc"] + + if "input_patterns" in config_data: + config.input_patterns = config_data["input_patterns"] + if "output_file" in config_data: + config.output_file = config_data["output_file"] + if "language" in config_data: + config.language = config_data["language"] + if "template_style" in config_data: + config.template_style = config_data["template_style"] + if "syntax_highlighting" in config_data: + config.syntax_highlighting = config_data["syntax_highlighting"] + if "theme" in config_data: + config.theme = config_data["theme"] + if "fail_on_warning" in config_data: + config.fail_on_warning = config_data["fail_on_warning"] + if "exclude_patterns" in config_data: + config.exclude_patterns = config_data["exclude_patterns"] + if "include_private" in config_data: + config.include_private = config_data["include_private"] + if "output_format" in config_data: + config.output_format = config_data["output_format"] + + except toml.TomlDecodeError as e: + raise ValueError(f"Invalid TOML in config file: {e}") + except Exception as e: + raise ValueError(f"Error reading config file: {e}") + + return config + + +def save_config(config: Config, config_path: str) -> None: + """Save configuration to file. + + Args: + config: Config object + config_path: Path to save config + """ + data = {"tool": {"code-doc": config.to_dict()}} + + with open(config_path, "w", encoding="utf-8") as f: + toml.dump(data, f)