chore(conductor): Mark track 'Saved Tool Presets' as complete
This commit is contained in:
91
src/tool_presets.py
Normal file
91
src/tool_presets.py
Normal file
@@ -0,0 +1,91 @@
|
||||
import tomllib
|
||||
import tomli_w
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, Optional, Union
|
||||
from src import paths
|
||||
from src.models import ToolPreset
|
||||
|
||||
class ToolPresetManager:
|
||||
def __init__(self, project_root: Optional[Union[str, Path]] = None):
|
||||
self.project_root = Path(project_root) if project_root else None
|
||||
|
||||
def _load_from_path(self, path: Path) -> Dict[str, ToolPreset]:
|
||||
if not path.exists():
|
||||
return {}
|
||||
try:
|
||||
with open(path, "rb") as f:
|
||||
data = tomllib.load(f)
|
||||
presets = {}
|
||||
for name, config in data.items():
|
||||
if isinstance(config, dict):
|
||||
presets[name] = ToolPreset.from_dict(name, config)
|
||||
return presets
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
def load_all(self) -> Dict[str, ToolPreset]:
|
||||
"""
|
||||
Merges global and project presets.
|
||||
Project presets override global ones if they have the same name.
|
||||
"""
|
||||
presets = self._load_from_path(paths.get_global_tool_presets_path())
|
||||
if self.project_root:
|
||||
project_presets = self._load_from_path(paths.get_project_tool_presets_path(self.project_root))
|
||||
presets.update(project_presets)
|
||||
return presets
|
||||
|
||||
def save_preset(self, preset: ToolPreset, scope: str = "project") -> None:
|
||||
"""
|
||||
Saves a preset to either 'global' or 'project' scope.
|
||||
Scope must be 'global' or 'project'.
|
||||
"""
|
||||
if scope == "global":
|
||||
path = paths.get_global_tool_presets_path()
|
||||
elif scope == "project":
|
||||
if not self.project_root:
|
||||
raise ValueError("Project root not set for project scope saving.")
|
||||
path = paths.get_project_tool_presets_path(self.project_root)
|
||||
else:
|
||||
raise ValueError(f"Invalid scope: {scope}")
|
||||
|
||||
data = {}
|
||||
if path.exists():
|
||||
try:
|
||||
with open(path, "rb") as f:
|
||||
data = tomllib.load(f)
|
||||
except Exception:
|
||||
data = {}
|
||||
|
||||
data[preset.name] = preset.to_dict()
|
||||
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(path, "wb") as f:
|
||||
tomli_w.dump(data, f)
|
||||
|
||||
def delete_preset(self, name: str, scope: str = "project") -> None:
|
||||
"""
|
||||
Deletes a preset from the specified scope.
|
||||
Scope must be 'global' or 'project'.
|
||||
"""
|
||||
if scope == "global":
|
||||
path = paths.get_global_tool_presets_path()
|
||||
elif scope == "project":
|
||||
if not self.project_root:
|
||||
raise ValueError("Project root not set for project scope deletion.")
|
||||
path = paths.get_project_tool_presets_path(self.project_root)
|
||||
else:
|
||||
raise ValueError(f"Invalid scope: {scope}")
|
||||
|
||||
if not path.exists():
|
||||
return
|
||||
|
||||
try:
|
||||
with open(path, "rb") as f:
|
||||
data = tomllib.load(f)
|
||||
except Exception:
|
||||
return
|
||||
|
||||
if name in data:
|
||||
del data[name]
|
||||
with open(path, "wb") as f:
|
||||
tomli_w.dump(data, f)
|
||||
Reference in New Issue
Block a user