| import json |
| import os |
| from typing import Dict, Any, Optional |
| import datetime |
|
|
|
|
| CONFIG_DIR = ".letxipu" |
| CONFIG_FILE = "search-config.json" |
| PRESETS_FILE = "presets.json" |
|
|
|
|
| class PersistenceManager: |
| def __init__(self, project_path: str = "."): |
| self.config_dir = os.path.join(project_path, CONFIG_DIR) |
| self.config_file = os.path.join(self.config_dir, CONFIG_FILE) |
| self.presets_file = os.path.join(self.config_dir, PRESETS_FILE) |
| os.makedirs(self.config_dir, exist_ok=True) |
|
|
| def save_config(self, config: Dict[str, Any]): |
| with open(self.config_file, "w", encoding="utf-8") as f: |
| json.dump(config, f, indent=2, ensure_ascii=False) |
|
|
| def load_config(self) -> Dict[str, Any]: |
| if os.path.exists(self.config_file): |
| with open(self.config_file, "r", encoding="utf-8") as f: |
| return json.load(f) |
| return {} |
|
|
| def save_preset(self, name: str, config: Dict[str, Any]): |
| presets = self.load_presets() |
| presets[name] = { |
| "config": config, |
| "timestamp": datetime.datetime.now().isoformat(), |
| } |
| with open(self.presets_file, "w", encoding="utf-8") as f: |
| json.dump(presets, f, indent=2, ensure_ascii=False) |
|
|
| def load_presets(self) -> Dict[str, Any]: |
| if os.path.exists(self.presets_file): |
| with open(self.presets_file, "r", encoding="utf-8") as f: |
| return json.load(f) |
| return {} |
|
|
| def load_preset(self, name: str) -> Optional[Dict[str, Any]]: |
| presets = self.load_presets() |
| return presets.get(name, {}).get("config") |
|
|
| def delete_preset(self, name: str): |
| presets = self.load_presets() |
| if name in presets: |
| del presets[name] |
| with open(self.presets_file, "w", encoding="utf-8") as f: |
| json.dump(presets, f, indent=2, ensure_ascii=False) |
|
|
| def list_presets(self) -> list: |
| return list(self.load_presets().keys()) |
|
|