Files
gitignore-generator/gitignore_generator/config.py
7000pctAUTO 951f5e26d5
Some checks failed
CI / test (push) Has been cancelled
Initial upload: core files and configuration
2026-02-02 15:53:13 +00:00

67 lines
2.1 KiB
Python

"""Configuration management for gitignore-generator."""
import os
from pathlib import Path
from typing import Optional
import yaml
class Config:
"""Configuration management class."""
def __init__(self, config_path: Optional[str] = None):
"""Initialize configuration."""
if config_path is None:
config_path = os.environ.get(
"GITIGNORE_CONFIG",
str(Path(__file__).parent.parent / ".gitignore-generator" / "config.yaml")
)
self.config_path = Path(config_path)
self._config = self._load_config()
def _load_config(self) -> dict:
"""Load configuration from file."""
if self.config_path.exists():
with open(self.config_path, "r") as f:
return yaml.safe_load(f) or {}
return {}
@property
def template_dir(self) -> Path:
"""Get template directory path."""
env_dir = os.environ.get("TEMPLATE_DIR")
if env_dir:
return Path(env_dir)
config_dir = self._config.get("template_dir")
if config_dir:
return Path(__file__).parent / config_dir
return Path(__file__).parent / "templates"
@property
def default_output(self) -> str:
"""Get default output file name."""
env_output = os.environ.get("GITIGNORE_DEFAULT_FILE")
if env_output:
return env_output
config_output = self._config.get("default_output")
if config_output:
return config_output
return ".gitignore"
@property
def custom_templates_dir(self) -> Path:
"""Get custom templates directory path."""
custom_dir = self._config.get("custom_templates_dir", "~/.config/gitignore-generator/templates")
return Path(custom_dir).expanduser()
def get_template_path(self, template_name: str, category: str = "languages") -> Optional[Path]:
"""Get the path to a template file."""
template_path = self.template_dir / category / f"{template_name}.gitignore"
if template_path.exists():
return template_path
return None
config = Config()