import os import yaml from typing import Optional, Dict, Any from pathlib import Path class GitLabMonitorConfig: """Configuration management for GitLab pipeline monitoring daemon.""" def __init__(self, config_path: Optional[str] = None): self.config_path = config_path or os.path.expanduser( "~/.config/gtd-tools/gitlab_monitor.yaml" ) self.config = self._load_config() def _load_config(self) -> Dict[str, Any]: """Load configuration from file or create default.""" config_file = Path(self.config_path) if config_file.exists(): try: with open(config_file, "r") as f: return yaml.safe_load(f) or {} except Exception as e: print(f"Error loading config: {e}") return self._default_config() else: # Create default config file config = self._default_config() self._save_config(config) return config def _default_config(self) -> Dict[str, Any]: """Return default configuration.""" return { "email_monitoring": { "subject_patterns": ["Failed pipeline", "Pipeline failed"], "sender_patterns": ["*@gitlab.com", "*gitlab*"], "check_interval": 30, # seconds }, "gitlab": { "api_token": os.getenv("GITLAB_API_TOKEN", ""), "base_url": "https://gitlab.com", "default_project_id": None, }, "openai": { "api_key": os.getenv("OPENAI_API_KEY", ""), "model": "gpt-4", # GPT-5 not available yet, using GPT-4 "max_tokens": 1000, "temperature": 0.1, }, "notifications": { "enabled": True, "sound": True, "show_summary_window": True, }, "logging": { "level": "INFO", "log_file": os.path.expanduser( "~/.config/gtd-tools/gitlab_monitor.log" ), }, } def _save_config(self, config: Dict[str, Any]): """Save configuration to file.""" config_file = Path(self.config_path) config_file.parent.mkdir(parents=True, exist_ok=True) with open(config_file, "w") as f: yaml.dump(config, f, default_flow_style=False) def get_gitlab_token(self) -> Optional[str]: """Get GitLab API token.""" return self.config.get("gitlab", {}).get("api_token") or os.getenv( "GITLAB_API_TOKEN" ) def get_openai_key(self) -> Optional[str]: """Get OpenAI API key.""" return self.config.get("openai", {}).get("api_key") or os.getenv( "OPENAI_API_KEY" ) def get_subject_patterns(self) -> list: """Get email subject patterns to monitor.""" return self.config.get("email_monitoring", {}).get("subject_patterns", []) def get_sender_patterns(self) -> list: """Get sender patterns to monitor.""" return self.config.get("email_monitoring", {}).get("sender_patterns", []) def get_check_interval(self) -> int: """Get email check interval in seconds.""" return self.config.get("email_monitoring", {}).get("check_interval", 30) def get_gitlab_base_url(self) -> str: """Get GitLab base URL.""" return self.config.get("gitlab", {}).get("base_url", "https://gitlab.com") def get_openai_model(self) -> str: """Get OpenAI model to use.""" return self.config.get("openai", {}).get("model", "gpt-4") def is_notifications_enabled(self) -> bool: """Check if notifications are enabled.""" return self.config.get("notifications", {}).get("enabled", True) def save(self): """Save current configuration to file.""" self._save_config(self.config)