|
| 1 | +"""Base configuration class for AI code assistants.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import logging |
| 5 | +from abc import ABC, abstractmethod |
| 6 | +from pathlib import Path |
| 7 | +from typing import Any, Dict, List, Optional, Union |
| 8 | + |
| 9 | +try: |
| 10 | + import tomllib |
| 11 | +except ImportError: |
| 12 | + import tomli as tomllib |
| 13 | + |
| 14 | +import tomli_w |
| 15 | + |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +class BaseToolConfig(ABC): |
| 20 | + """Base class for tool configuration management.""" |
| 21 | + |
| 22 | + def __init__(self, tool_name: str): |
| 23 | + """Initialize with tool name.""" |
| 24 | + self.tool_name = tool_name |
| 25 | + self.home = Path.home() |
| 26 | + self.cwd = Path.cwd() |
| 27 | + |
| 28 | + @abstractmethod |
| 29 | + def get_scope_paths(self) -> Dict[str, List[Path]]: |
| 30 | + """Return mapping of scopes to list of possible config paths. |
| 31 | +
|
| 32 | + Returns: |
| 33 | + Dict[str, List[Path]]: e.g., {'user': [Path(...)], 'project': [Path(...)]} |
| 34 | + """ |
| 35 | + pass |
| 36 | + |
| 37 | + def get_config_path(self, scope: str) -> Optional[Path]: |
| 38 | + """Get the primary config path for a given scope. |
| 39 | +
|
| 40 | + Args: |
| 41 | + scope: 'user' or 'project' |
| 42 | +
|
| 43 | + Returns: |
| 44 | + Path object or None if scope not supported |
| 45 | + """ |
| 46 | + paths = self.get_scope_paths().get(scope, []) |
| 47 | + if not paths: |
| 48 | + return None |
| 49 | + |
| 50 | + # Return the first path that exists, or the first path if none exist (for creation) |
| 51 | + for path in paths: |
| 52 | + if path.exists(): |
| 53 | + return path |
| 54 | + return paths[0] |
| 55 | + |
| 56 | + def load_config(self, scope: Optional[str] = None) -> Dict[str, Any]: |
| 57 | + """Load configuration. |
| 58 | +
|
| 59 | + Args: |
| 60 | + scope: If provided, load only that scope. If None, load all scopes. |
| 61 | +
|
| 62 | + Returns: |
| 63 | + Dict with scope keys if scope is None, or config dict if scope provided. |
| 64 | + """ |
| 65 | + if scope: |
| 66 | + path = self.get_config_path(scope) |
| 67 | + if not path or not path.exists(): |
| 68 | + return {} |
| 69 | + return self._load_file(path) |
| 70 | + |
| 71 | + # Load all scopes |
| 72 | + results = {} |
| 73 | + for s_name, paths in self.get_scope_paths().items(): |
| 74 | + for path in paths: |
| 75 | + if path.exists(): |
| 76 | + try: |
| 77 | + results[s_name] = { |
| 78 | + "data": self._load_file(path), |
| 79 | + "path": str(path), |
| 80 | + } |
| 81 | + break |
| 82 | + except Exception as e: |
| 83 | + logger.warning(f"Failed to load {path}: {e}") |
| 84 | + continue |
| 85 | + return results |
| 86 | + |
| 87 | + def _load_file(self, path: Path) -> Dict[str, Any]: |
| 88 | + """Load a single config file.""" |
| 89 | + if path.suffix == ".toml": |
| 90 | + with open(path, "rb") as f: |
| 91 | + return tomllib.load(f) |
| 92 | + else: |
| 93 | + with open(path, "r", encoding="utf-8") as f: |
| 94 | + return json.load(f) |
| 95 | + |
| 96 | + def save_config(self, data: Dict[str, Any], scope: str) -> Path: |
| 97 | + """Save configuration to the specified scope. |
| 98 | +
|
| 99 | + Args: |
| 100 | + data: Configuration data dict |
| 101 | + scope: 'user' or 'project' |
| 102 | +
|
| 103 | + Returns: |
| 104 | + Path where config was saved |
| 105 | + """ |
| 106 | + path = self.get_config_path(scope) |
| 107 | + if not path: |
| 108 | + raise ValueError(f"Unsupported scope: {scope}") |
| 109 | + |
| 110 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 111 | + |
| 112 | + if path.suffix == ".toml": |
| 113 | + with open(path, "wb") as f: |
| 114 | + tomli_w.dump(data, f) |
| 115 | + else: |
| 116 | + with open(path, "w", encoding="utf-8") as f: |
| 117 | + json.dump(data, f, indent=2) |
| 118 | + |
| 119 | + return path |
| 120 | + |
| 121 | + def set_value(self, key_path: str, value: str, scope: str) -> Path: |
| 122 | + """Set a configuration value using dotted notation. |
| 123 | +
|
| 124 | + Args: |
| 125 | + key_path: Dotted key path (e.g., 'profiles.default.model') |
| 126 | + value: Value to set |
| 127 | + scope: Scope to save to |
| 128 | +
|
| 129 | + Returns: |
| 130 | + Path where config was saved |
| 131 | + """ |
| 132 | + config_data = self.load_config(scope) |
| 133 | + |
| 134 | + # Parse key path |
| 135 | + parts = self._parse_key_path(key_path) |
| 136 | + |
| 137 | + # Set nested value |
| 138 | + self._set_nested_value(config_data, parts, value) |
| 139 | + |
| 140 | + return self.save_config(config_data, scope) |
| 141 | + |
| 142 | + def unset_value(self, key_path: str, scope: str) -> bool: |
| 143 | + """Unset a configuration value. |
| 144 | +
|
| 145 | + Args: |
| 146 | + key_path: Dotted key path |
| 147 | + scope: Scope to remove from |
| 148 | +
|
| 149 | + Returns: |
| 150 | + True if key was found and removed, False otherwise |
| 151 | + """ |
| 152 | + config_data = self.load_config(scope) |
| 153 | + if not config_data: |
| 154 | + return False |
| 155 | + |
| 156 | + parts = self._parse_key_path(key_path) |
| 157 | + |
| 158 | + # Special handling for potentially merged keys in TOML |
| 159 | + # (similar to what was in app.py) |
| 160 | + if len(parts) > 2 and self.get_config_path(scope).suffix == ".toml": |
| 161 | + # Heuristic check for version numbers split by dots |
| 162 | + pass # Logic can be refined if needed |
| 163 | + |
| 164 | + found = self._unset_nested_value(config_data, parts) |
| 165 | + |
| 166 | + if found: |
| 167 | + self.save_config(config_data, scope) |
| 168 | + |
| 169 | + return found |
| 170 | + |
| 171 | + def _parse_key_path(self, key_path: str) -> List[str]: |
| 172 | + """Parse dotted key path, handling quotes.""" |
| 173 | + import re |
| 174 | + # Regex matches quoted strings OR unquoted parts |
| 175 | + parts = re.split(r'(?<!\\)"(?:\\.|[^"\\])*"(?:\s*\.\s*|\s*$)|\s*\.\s*', key_path.strip()) |
| 176 | + |
| 177 | + cleaned = [] |
| 178 | + for part in parts: |
| 179 | + part = part.strip() |
| 180 | + if part and part not in ['.', '']: |
| 181 | + if part.startswith('"') and part.endswith('"'): |
| 182 | + part = part[1:-1].replace('\"', '"') |
| 183 | + cleaned.append(part) |
| 184 | + return cleaned |
| 185 | + |
| 186 | + def _set_nested_value(self, data: Dict, key_parts: List[str], val: Any): |
| 187 | + """Set value in nested dict, creating intermediates.""" |
| 188 | + if len(key_parts) == 1: |
| 189 | + data[key_parts[0]] = val |
| 190 | + return |
| 191 | + |
| 192 | + current_key = key_parts[0] |
| 193 | + if current_key not in data or not isinstance(data[current_key], dict): |
| 194 | + data[current_key] = {} |
| 195 | + |
| 196 | + self._set_nested_value(data[current_key], key_parts[1:], val) |
| 197 | + |
| 198 | + def _unset_nested_value(self, data: Dict, key_parts: List[str]) -> bool: |
| 199 | + """Unset value in nested dict.""" |
| 200 | + if len(key_parts) == 1: |
| 201 | + key = key_parts[0] |
| 202 | + candidates = [key] |
| 203 | + # Try quoted version if it has special chars |
| 204 | + if '/' in key or any(c in key for c in '.-'): |
| 205 | + candidates.append(f'"{key}"') |
| 206 | + |
| 207 | + for candidate in candidates: |
| 208 | + if candidate in data: |
| 209 | + del data[candidate] |
| 210 | + return True |
| 211 | + return False |
| 212 | + |
| 213 | + current_key = key_parts[0] |
| 214 | + if current_key not in data or not isinstance(data[current_key], dict): |
| 215 | + return False |
| 216 | + |
| 217 | + return self._unset_nested_value(data[current_key], key_parts[1:]) |
0 commit comments