|
| 1 | +""" |
| 2 | +Scale preset manager for loading and saving user-modified scale presets. |
| 3 | +
|
| 4 | +This module handles loading and saving clinical and session scale presets |
| 5 | +to a user config file in the application log directory. |
| 6 | +""" |
| 7 | + |
| 8 | +import json |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +from ..config import CLINICAL_SCALES_PRESETS, SESSION_SCALES_PRESETS |
| 13 | + |
| 14 | + |
| 15 | +class ScalePresetManager: |
| 16 | + """Manager for scale presets with user customization support.""" |
| 17 | + |
| 18 | + def __init__(self, config_dir: str | None = None): |
| 19 | + """Initialize the scale preset manager. |
| 20 | +
|
| 21 | + Args: |
| 22 | + config_dir: Directory for config files. If None, uses logs in app installation directory. |
| 23 | + """ |
| 24 | + if config_dir is None: |
| 25 | + # Default to logs folder in the application installation directory |
| 26 | + # For deployed app: C:\Program Files\BML\Clinical DBS Annotator\logs |
| 27 | + # For development: uses the source directory |
| 28 | + if getattr(sys, "frozen", False): |
| 29 | + # Running as deployed executable (PyInstaller/Nuitka) |
| 30 | + app_root = Path(sys.executable).parent |
| 31 | + else: |
| 32 | + # Running in development mode |
| 33 | + app_root = Path(__file__).parent.parent.parent |
| 34 | + self.config_dir = app_root / "logs" |
| 35 | + else: |
| 36 | + self.config_dir = Path(config_dir) |
| 37 | + |
| 38 | + self.config_dir.mkdir(parents=True, exist_ok=True) |
| 39 | + self.config_file = self.config_dir / "scale_presets.json" |
| 40 | + |
| 41 | + def get_clinical_presets(self) -> dict[str, list[str]]: |
| 42 | + """Get clinical scale presets, loading user modifications if available. |
| 43 | +
|
| 44 | + Returns: |
| 45 | + Dictionary of clinical scale presets (preset name -> list of scale names) |
| 46 | + """ |
| 47 | + user_presets = self._load_user_presets() |
| 48 | + if user_presets and "clinical" in user_presets: |
| 49 | + return user_presets["clinical"] |
| 50 | + return CLINICAL_SCALES_PRESETS |
| 51 | + |
| 52 | + def get_session_presets(self) -> dict[str, list[tuple[str, str, str]]]: |
| 53 | + """Get session scale presets, loading user modifications if available. |
| 54 | +
|
| 55 | + Returns: |
| 56 | + Dictionary of session scale presets (preset name -> list of (name, min, max) tuples) |
| 57 | + """ |
| 58 | + user_presets = self._load_user_presets() |
| 59 | + if user_presets and "session" in user_presets: |
| 60 | + return user_presets["session"] |
| 61 | + return SESSION_SCALES_PRESETS |
| 62 | + |
| 63 | + def save_clinical_presets(self, presets: dict[str, list[str]]) -> None: |
| 64 | + """Save clinical scale presets to user config file. |
| 65 | +
|
| 66 | + Args: |
| 67 | + presets: Dictionary of clinical scale presets (preset name -> list of scale names) |
| 68 | + """ |
| 69 | + user_presets = self._load_user_presets() or {} |
| 70 | + user_presets["clinical"] = presets |
| 71 | + self._save_user_presets(user_presets) |
| 72 | + |
| 73 | + def save_session_presets( |
| 74 | + self, presets: dict[str, list[tuple[str, str, str]]] |
| 75 | + ) -> None: |
| 76 | + """Save session scale presets to user config file. |
| 77 | +
|
| 78 | + Args: |
| 79 | + presets: Dictionary of session scale presets (preset name -> list of (name, min, max) tuples) |
| 80 | + """ |
| 81 | + user_presets = self._load_user_presets() or {} |
| 82 | + user_presets["session"] = presets |
| 83 | + self._save_user_presets(user_presets) |
| 84 | + |
| 85 | + def _load_user_presets(self) -> dict | None: |
| 86 | + """Load user presets from config file. |
| 87 | +
|
| 88 | + Returns: |
| 89 | + Dictionary with 'clinical' and 'session' keys, or None if file doesn't exist. |
| 90 | + """ |
| 91 | + if not self.config_file.exists(): |
| 92 | + return None |
| 93 | + |
| 94 | + try: |
| 95 | + with open(self.config_file, encoding="utf-8") as f: |
| 96 | + return json.load(f) |
| 97 | + except OSError, json.JSONDecodeError: |
| 98 | + return None |
| 99 | + |
| 100 | + def _save_user_presets(self, presets: dict) -> None: |
| 101 | + """Save user presets to config file. |
| 102 | +
|
| 103 | + Args: |
| 104 | + presets: Dictionary with 'clinical' and 'session' keys |
| 105 | + """ |
| 106 | + with open(self.config_file, "w", encoding="utf-8") as f: |
| 107 | + json.dump(presets, f, indent=4) |
| 108 | + |
| 109 | + |
| 110 | +# Global instance |
| 111 | +_preset_manager: ScalePresetManager | None = None |
| 112 | + |
| 113 | + |
| 114 | +def get_scale_preset_manager() -> ScalePresetManager: |
| 115 | + """Get the global scale preset manager instance. |
| 116 | +
|
| 117 | + Returns: |
| 118 | + The global ScalePresetManager instance |
| 119 | + """ |
| 120 | + global _preset_manager |
| 121 | + if _preset_manager is None: |
| 122 | + _preset_manager = ScalePresetManager() |
| 123 | + return _preset_manager |
0 commit comments