|
1 | 1 | # dlclivegui/utils/settings_store.py |
| 2 | +from __future__ import annotations |
| 3 | + |
2 | 4 | import logging |
3 | 5 | from pathlib import Path |
4 | 6 |
|
@@ -70,124 +72,194 @@ class ModelPathStore: |
70 | 72 | def __init__(self, settings: QSettings | None = None): |
71 | 73 | self._settings = settings or QSettings("DeepLabCut", "DLCLiveGUI") |
72 | 74 |
|
73 | | - def _norm(self, p: str | None) -> str | None: |
| 75 | + # ------------------------- |
| 76 | + # Normalization helpers |
| 77 | + # ------------------------- |
| 78 | + def _as_path(self, p: str | None) -> Path | None: |
| 79 | + """Best-effort conversion to Path (expand ~, interpret '.' as cwd).""" |
74 | 80 | if not p: |
75 | 81 | return None |
| 82 | + s = str(p).strip() |
| 83 | + if not s: |
| 84 | + return None |
76 | 85 | try: |
77 | | - return str(Path(p).expanduser().resolve()) |
| 86 | + pp = Path(s).expanduser() |
| 87 | + if s in (".", "./"): |
| 88 | + pp = Path.cwd() |
| 89 | + return pp |
78 | 90 | except Exception: |
79 | | - logger.debug("Failed to normalize path: %s", p) |
| 91 | + logger.debug("Failed to parse path: %s", p) |
| 92 | + return None |
| 93 | + |
| 94 | + def _norm_existing_dir(self, p: str | None) -> str | None: |
| 95 | + """Return an absolute, resolved existing directory path, else None.""" |
| 96 | + pp = self._as_path(p) |
| 97 | + if pp is None: |
| 98 | + return None |
| 99 | + try: |
| 100 | + # If a file was given, use its parent directory |
| 101 | + if pp.exists() and pp.is_file(): |
| 102 | + pp = pp.parent |
| 103 | + |
| 104 | + if pp.exists() and pp.is_dir(): |
| 105 | + return str(pp.resolve()) |
| 106 | + except Exception: |
| 107 | + logger.debug("Failed to normalize directory: %s", p) |
| 108 | + return None |
| 109 | + |
| 110 | + def _norm_existing_path(self, p: str | None) -> str | None: |
| 111 | + """Return an absolute, resolved existing path (file or dir), else None.""" |
| 112 | + pp = self._as_path(p) |
| 113 | + if pp is None: |
80 | 114 | return None |
| 115 | + try: |
| 116 | + if pp.exists(): |
| 117 | + return str(pp.resolve()) |
| 118 | + except Exception: |
| 119 | + logger.debug("Failed to normalize path: %s", p) |
| 120 | + return None |
81 | 121 |
|
| 122 | + # ------------------------- |
| 123 | + # Load |
| 124 | + # ------------------------- |
82 | 125 | def load_last(self) -> str | None: |
| 126 | + """Return last model path if it still exists and looks usable.""" |
83 | 127 | val = self._settings.value("dlc/last_model_path") |
84 | | - path = self._norm(str(val)) if val else None |
| 128 | + path = self._norm_existing_path(str(val)) if val else None |
85 | 129 | if not path: |
86 | 130 | return None |
| 131 | + |
87 | 132 | try: |
88 | | - return path if is_model_file(path) else None |
| 133 | + pp = Path(path) |
| 134 | + # Accept a valid model *file* |
| 135 | + if pp.is_file() and is_model_file(str(pp)): |
| 136 | + return str(pp) |
89 | 137 | except Exception: |
90 | | - logger.debug("Last model path is not a valid model file: %s", path) |
91 | | - return None |
| 138 | + logger.debug("Last model path not valid/usable: %s", path) |
| 139 | + |
| 140 | + return None |
92 | 141 |
|
93 | 142 | def load_last_dir(self) -> str | None: |
| 143 | + """Return last directory if it still exists and is a directory.""" |
94 | 144 | val = self._settings.value("dlc/last_model_dir") |
95 | | - d = self._norm(str(val)) if val else None |
96 | | - if not d: |
97 | | - return None |
98 | | - try: |
99 | | - p = Path(d) |
100 | | - return str(p) if p.exists() and p.is_dir() else None |
101 | | - except Exception: |
102 | | - logger.debug("Last model dir is not a valid directory: %s", d) |
103 | | - return None |
| 145 | + d = self._norm_existing_dir(str(val)) if val else None |
| 146 | + return d |
104 | 147 |
|
| 148 | + # ------------------------- |
| 149 | + # Save |
| 150 | + # ------------------------- |
105 | 151 | def save_if_valid(self, path: str) -> None: |
106 | | - """Save last model *file* if it looks valid, and always save its directory.""" |
107 | | - path = self._norm(path) or "" |
108 | | - if not path: |
| 152 | + """ |
| 153 | + Save last model path if it looks valid/usable, and always save its directory. |
| 154 | + - For files: always save parent directory. |
| 155 | + - For directories: save directory itself if it looks like a TF model dir. |
| 156 | + """ |
| 157 | + norm = self._norm_existing_path(path) |
| 158 | + if not norm: |
109 | 159 | return |
| 160 | + |
110 | 161 | try: |
111 | | - parent = str(Path(path).parent) |
112 | | - self._settings.setValue("dlc/last_model_dir", parent) |
| 162 | + p = Path(norm) |
| 163 | + |
| 164 | + # Always persist a *directory* that is safe for QFileDialog.setDirectory(...) |
| 165 | + if p.is_dir(): |
| 166 | + model_dir = p |
| 167 | + else: |
| 168 | + model_dir = p.parent |
| 169 | + |
| 170 | + model_dir_norm = self._norm_existing_dir(str(model_dir)) |
| 171 | + if model_dir_norm: |
| 172 | + self._settings.setValue("dlc/last_model_dir", model_dir_norm) |
| 173 | + |
| 174 | + # Persist model path if it is a valid model file, or a TF model directory |
| 175 | + if p.is_file() and is_model_file(str(p)): |
| 176 | + self._settings.setValue("dlc/last_model_path", str(p)) |
| 177 | + elif p.is_dir() and self._looks_like_tf_model_dir(p): |
| 178 | + self._settings.setValue("dlc/last_model_path", str(p)) |
113 | 179 |
|
114 | | - if is_model_file(path): |
115 | | - self._settings.setValue("dlc/last_model_path", str(Path(path))) |
116 | 180 | except Exception: |
117 | | - logger.debug("Failed to save last model path: %s", path) |
118 | | - pass |
| 181 | + logger.debug("Failed to save model path: %s", path, exc_info=True) |
119 | 182 |
|
120 | 183 | def save_last_dir(self, directory: str) -> None: |
121 | | - directory = self._norm(directory) or "" |
122 | | - if not directory: |
| 184 | + d = self._norm_existing_dir(directory) |
| 185 | + if not d: |
123 | 186 | return |
124 | 187 | try: |
125 | | - p = Path(directory) |
126 | | - if p.exists() and p.is_dir(): |
127 | | - self._settings.setValue("dlc/last_model_dir", str(p)) |
| 188 | + self._settings.setValue("dlc/last_model_dir", d) |
128 | 189 | except Exception: |
129 | | - pass |
| 190 | + logger.debug("Failed to save last model dir: %s", d, exc_info=True) |
130 | 191 |
|
| 192 | + # ------------------------- |
| 193 | + # Resolve |
| 194 | + # ------------------------- |
131 | 195 | def resolve(self, config_path: str | None) -> str: |
132 | | - """Resolve the best model path to display in the UI.""" |
133 | | - config_path = self._norm(config_path) |
134 | | - if config_path: |
| 196 | + """ |
| 197 | + Resolve the best model path to display in the UI. |
| 198 | + Preference: |
| 199 | + 1) config_path if valid/usable |
| 200 | + 2) persisted last model path if valid/usable |
| 201 | + 3) empty |
| 202 | + """ |
| 203 | + cfg = self._norm_existing_path(config_path) |
| 204 | + if cfg: |
135 | 205 | try: |
136 | | - if is_model_file(config_path): |
137 | | - return config_path |
| 206 | + p = Path(cfg) |
| 207 | + if p.is_file() and is_model_file(cfg): |
| 208 | + return cfg |
| 209 | + if p.is_dir() and self._looks_like_tf_model_dir(p): |
| 210 | + return cfg |
138 | 211 | except Exception: |
139 | | - logger.debug("Config path is not a valid model file: %s", config_path) |
140 | | - pass |
| 212 | + logger.debug("Config path not usable: %s", cfg) |
141 | 213 |
|
142 | 214 | persisted = self.load_last() |
143 | 215 | if persisted: |
144 | | - try: |
145 | | - if is_model_file(persisted): |
146 | | - return persisted |
147 | | - except Exception: |
148 | | - pass |
| 216 | + return persisted |
149 | 217 |
|
150 | 218 | return "" |
151 | 219 |
|
152 | 220 | def suggest_start_dir(self, fallback_dir: str | None = None) -> str: |
153 | | - """Pick the best directory to start the file dialog in.""" |
| 221 | + """ |
| 222 | + Pick the best directory to start file dialogs in. |
| 223 | + Guarantees: returns an existing absolute directory (never '.'). |
| 224 | + """ |
154 | 225 | # 1) last dir |
155 | 226 | last_dir = self.load_last_dir() |
156 | 227 | if last_dir: |
157 | 228 | return last_dir |
158 | 229 |
|
159 | | - # 2) directory of last valid model file |
160 | | - last_file = self.load_last() |
161 | | - if last_file: |
| 230 | + # 2) directory of last valid model path |
| 231 | + last = self.load_last() |
| 232 | + if last: |
162 | 233 | try: |
163 | | - parent = Path(last_file).parent |
164 | | - if parent.exists(): |
165 | | - return str(parent) |
| 234 | + p = Path(last) |
| 235 | + if p.is_file(): |
| 236 | + parent = self._norm_existing_dir(str(p.parent)) |
| 237 | + if parent: |
| 238 | + return parent |
| 239 | + elif p.is_dir(): |
| 240 | + d = self._norm_existing_dir(str(p)) |
| 241 | + if d: |
| 242 | + return d |
166 | 243 | except Exception: |
167 | | - logger.debug("Failed to get parent of last model file: %s", last_file) |
168 | | - pass |
| 244 | + logger.debug("Failed to derive start dir from last model: %s", last) |
169 | 245 |
|
170 | | - # 3) fallback dir (config.model_directory) if valid |
171 | | - if fallback_dir: |
172 | | - try: |
173 | | - p = Path(fallback_dir).expanduser() |
174 | | - if p.exists() and p.is_dir(): |
175 | | - return str(p) |
176 | | - except Exception: |
177 | | - logger.debug("Fallback dir is not a valid directory: %s", fallback_dir) |
178 | | - pass |
| 246 | + # 3) fallback dir (e.g. config.dlc.model_directory) |
| 247 | + fb = self._norm_existing_dir(fallback_dir) |
| 248 | + if fb: |
| 249 | + return fb |
179 | 250 |
|
180 | | - # 4) last resort: home |
181 | | - return str(Path.home()) |
| 251 | + # 4) last resort: cwd if exists else home |
| 252 | + cwd = self._norm_existing_dir(str(Path.cwd())) |
| 253 | + return cwd or str(Path.home()) |
182 | 254 |
|
183 | 255 | def suggest_selected_file(self) -> str | None: |
184 | | - """Optional: return a file to preselect if it exists.""" |
185 | | - last_file = self.load_last() |
186 | | - if not last_file: |
| 256 | + """Return a file to preselect if it exists (only files, not directories).""" |
| 257 | + last = self.load_last() |
| 258 | + if not last: |
187 | 259 | return None |
188 | 260 | try: |
189 | | - p = Path(last_file) |
| 261 | + p = Path(last) |
190 | 262 | return str(p) if p.exists() and p.is_file() else None |
191 | 263 | except Exception: |
192 | | - logger.debug("Failed to check existence of last model file: %s", last_file) |
| 264 | + logger.debug("Failed to check existence of last model: %s", last) |
193 | 265 | return None |
0 commit comments