|
| 1 | +# EngineConfigHelper - Auto-Save Configuration System |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The `EngineConfigHelper` now features **automatic configuration persistence**: |
| 6 | + |
| 7 | +- ✅ Auto-loads configuration on first access |
| 8 | +- ✅ Auto-saves configuration on every change |
| 9 | +- ✅ No manual save() calls needed |
| 10 | +- ✅ Simple, clean API |
| 11 | +- ✅ Persistent storage |
| 12 | + |
| 13 | +## How It Works |
| 14 | + |
| 15 | +### Auto-Load |
| 16 | + |
| 17 | +Configuration is automatically loaded from disk on first access: |
| 18 | + |
| 19 | +```python |
| 20 | +from engine_sdk import EngineConfigHelper |
| 21 | + |
| 22 | +class MyEngine(CompilerEngine): |
| 23 | + def __init__(self): |
| 24 | + self.config = EngineConfigHelper(self.id) |
| 25 | + |
| 26 | + def preflight(self, gui, file: str) -> bool: |
| 27 | + # Configuration is auto-loaded on first access |
| 28 | + timeout = self.config.get(gui, "timeout", default=30) |
| 29 | + # ✅ Configuration loaded from disk automatically |
| 30 | + return True |
| 31 | +``` |
| 32 | + |
| 33 | +### Auto-Save |
| 34 | + |
| 35 | +Configuration is automatically saved to disk on every change: |
| 36 | + |
| 37 | +```python |
| 38 | +def preflight(self, gui, file: str) -> bool: |
| 39 | + # Set value - automatically saved to disk |
| 40 | + self.config.set(gui, "timeout", 60) |
| 41 | + # ✅ Configuration saved to disk automatically |
| 42 | + |
| 43 | + # Update multiple values - automatically saved |
| 44 | + self.config.update(gui, {"key1": "value1", "key2": "value2"}) |
| 45 | + # ✅ Configuration saved to disk automatically |
| 46 | + |
| 47 | + return True |
| 48 | +``` |
| 49 | + |
| 50 | +## API Reference |
| 51 | + |
| 52 | +### get(gui, key, default=None) |
| 53 | + |
| 54 | +Get a configuration value. Auto-loads if needed. |
| 55 | + |
| 56 | +```python |
| 57 | +timeout = self.config.get(gui, "timeout", default=30) |
| 58 | +``` |
| 59 | + |
| 60 | +### set(gui, key, value) |
| 61 | + |
| 62 | +Set a configuration value and auto-save. |
| 63 | + |
| 64 | +```python |
| 65 | +self.config.set(gui, "timeout", 60) |
| 66 | +# ✅ Automatically saved to disk |
| 67 | +``` |
| 68 | + |
| 69 | +### update(gui, updates) |
| 70 | + |
| 71 | +Update multiple values and auto-save. |
| 72 | + |
| 73 | +```python |
| 74 | +self.config.update(gui, { |
| 75 | + "timeout": 60, |
| 76 | + "verbose": True, |
| 77 | + "output_dir": "dist" |
| 78 | +}) |
| 79 | +# ✅ Automatically saved to disk |
| 80 | +``` |
| 81 | + |
| 82 | +### delete(gui, key) |
| 83 | + |
| 84 | +Delete a configuration value and auto-save. |
| 85 | + |
| 86 | +```python |
| 87 | +self.config.delete(gui, "old_key") |
| 88 | +# ✅ Automatically saved to disk |
| 89 | +``` |
| 90 | + |
| 91 | +### clear(gui) |
| 92 | + |
| 93 | +Clear all configuration values and auto-save. |
| 94 | + |
| 95 | +```python |
| 96 | +self.config.clear(gui) |
| 97 | +# ✅ Automatically saved to disk |
| 98 | +``` |
| 99 | + |
| 100 | +### load(gui) |
| 101 | + |
| 102 | +Explicitly load configuration from disk. |
| 103 | + |
| 104 | +```python |
| 105 | +config = self.config.load(gui) |
| 106 | +# Returns: {"timeout": 60, "verbose": True, ...} |
| 107 | +``` |
| 108 | + |
| 109 | +### save(gui) |
| 110 | + |
| 111 | +Explicitly save configuration to disk. |
| 112 | + |
| 113 | +```python |
| 114 | +success = self.config.save(gui) |
| 115 | +# Returns: True if successful, False otherwise |
| 116 | +``` |
| 117 | + |
| 118 | +### has_key(gui, key) |
| 119 | + |
| 120 | +Check if a configuration key exists. |
| 121 | + |
| 122 | +```python |
| 123 | +if self.config.has_key(gui, "timeout"): |
| 124 | + print("Timeout is configured") |
| 125 | +``` |
| 126 | + |
| 127 | +### keys(gui) |
| 128 | + |
| 129 | +Get all configuration keys. |
| 130 | + |
| 131 | +```python |
| 132 | +keys = self.config.keys(gui) |
| 133 | +# Returns: ["timeout", "verbose", "output_dir"] |
| 134 | +``` |
| 135 | + |
| 136 | +### values(gui) |
| 137 | + |
| 138 | +Get all configuration values. |
| 139 | + |
| 140 | +```python |
| 141 | +values = self.config.values(gui) |
| 142 | +# Returns: [60, True, "dist"] |
| 143 | +``` |
| 144 | + |
| 145 | +### items(gui) |
| 146 | + |
| 147 | +Get all configuration items. |
| 148 | + |
| 149 | +```python |
| 150 | +items = self.config.items(gui) |
| 151 | +# Returns: [("timeout", 60), ("verbose", True), ("output_dir", "dist")] |
| 152 | +``` |
| 153 | + |
| 154 | +### reset(gui) |
| 155 | + |
| 156 | +Reset configuration to last saved state. |
| 157 | + |
| 158 | +```python |
| 159 | +self.config.reset(gui) |
| 160 | +# Reloads configuration from disk |
| 161 | +``` |
| 162 | + |
| 163 | +### to_dict(gui) |
| 164 | + |
| 165 | +Get configuration as dictionary. |
| 166 | + |
| 167 | +```python |
| 168 | +config_dict = self.config.to_dict(gui) |
| 169 | +# Returns: {"timeout": 60, "verbose": True, ...} |
| 170 | +``` |
| 171 | + |
| 172 | +## Storage Location |
| 173 | + |
| 174 | +Configurations are stored in: |
| 175 | + |
| 176 | +``` |
| 177 | +workspace/ark_engines_config/<engine_id>/config.json |
| 178 | +``` |
| 179 | + |
| 180 | +Example: |
| 181 | + |
| 182 | +``` |
| 183 | +workspace/ |
| 184 | +└── ark_engines_config/ |
| 185 | + ├── pyinstaller/ |
| 186 | + │ └── config.json |
| 187 | + ├── nuitka/ |
| 188 | + │ └── config.json |
| 189 | + └── cx_freeze/ |
| 190 | + └── config.json |
| 191 | +``` |
| 192 | + |
| 193 | +## Configuration Format |
| 194 | + |
| 195 | +Each configuration is stored as JSON: |
| 196 | + |
| 197 | +```json |
| 198 | +{ |
| 199 | + "output_dir": "dist", |
| 200 | + "one_file": false, |
| 201 | + "console": true, |
| 202 | + "timeout": 60, |
| 203 | + "verbose": true, |
| 204 | + "env_vars": { |
| 205 | + "VAR1": "value1" |
| 206 | + } |
| 207 | +} |
| 208 | +``` |
| 209 | + |
| 210 | +## Complete Example |
| 211 | + |
| 212 | +```python |
| 213 | +from engine_sdk import ( |
| 214 | + CompilerEngine, |
| 215 | + EngineConfigHelper, |
| 216 | + safe_log, |
| 217 | +) |
| 218 | + |
| 219 | +class MyEngine(CompilerEngine): |
| 220 | + id = "my_engine" |
| 221 | + name = "My Engine" |
| 222 | + version = "1.0.0" |
| 223 | + required_core_version = "1.0.0" |
| 224 | + required_sdk_version = "1.0.0" |
| 225 | + |
| 226 | + def __init__(self): |
| 227 | + super().__init__() |
| 228 | + self.config = EngineConfigHelper(self.id) |
| 229 | + |
| 230 | + def preflight(self, gui, file: str) -> bool: |
| 231 | + try: |
| 232 | + # Get configuration (auto-loads if needed) |
| 233 | + timeout = self.config.get(gui, "timeout", default=30) |
| 234 | + verbose = self.config.get(gui, "verbose", default=False) |
| 235 | + |
| 236 | + safe_log(gui, f"Timeout: {timeout}, Verbose: {verbose}") |
| 237 | + |
| 238 | + # Set configuration (auto-saves) |
| 239 | + self.config.set(gui, "last_used", file) |
| 240 | + |
| 241 | + return True |
| 242 | + except Exception as e: |
| 243 | + safe_log(gui, f"Error: {e}") |
| 244 | + return False |
| 245 | + |
| 246 | + def build_command(self, gui, file: str) -> list[str]: |
| 247 | + try: |
| 248 | + # Get configuration (auto-loads if needed) |
| 249 | + output_dir = self.config.get(gui, "output_dir", default="dist") |
| 250 | + optimization = self.config.get(gui, "optimization", default=0) |
| 251 | + |
| 252 | + cmd = ["my-compiler", file, "--output", output_dir] |
| 253 | + |
| 254 | + if optimization > 0: |
| 255 | + cmd.extend(["--optimize", str(optimization)]) |
| 256 | + |
| 257 | + return cmd |
| 258 | + except Exception as e: |
| 259 | + safe_log(gui, f"Error: {e}") |
| 260 | + return [] |
| 261 | + |
| 262 | + def create_tab(self, gui): |
| 263 | + try: |
| 264 | + from PySide6.QtWidgets import ( |
| 265 | + QWidget, QVBoxLayout, QHBoxLayout, QLabel, |
| 266 | + QLineEdit, QSpinBox, QPushButton |
| 267 | + ) |
| 268 | + |
| 269 | + tab = QWidget() |
| 270 | + layout = QVBoxLayout(tab) |
| 271 | + |
| 272 | + # Get configuration (auto-loads if needed) |
| 273 | + output_dir = self.config.get(gui, "output_dir", default="dist") |
| 274 | + optimization = self.config.get(gui, "optimization", default=0) |
| 275 | + |
| 276 | + # Output directory |
| 277 | + row1 = QHBoxLayout() |
| 278 | + row1.addWidget(QLabel("Output Directory:")) |
| 279 | + output_edit = QLineEdit() |
| 280 | + output_edit.setText(output_dir) |
| 281 | + row1.addWidget(output_edit) |
| 282 | + layout.addLayout(row1) |
| 283 | + |
| 284 | + # Optimization level |
| 285 | + row2 = QHBoxLayout() |
| 286 | + row2.addWidget(QLabel("Optimization:")) |
| 287 | + opt_spin = QSpinBox() |
| 288 | + opt_spin.setValue(optimization) |
| 289 | + row2.addWidget(opt_spin) |
| 290 | + layout.addLayout(row2) |
| 291 | + |
| 292 | + # Save button |
| 293 | + save_btn = QPushButton("Save Configuration") |
| 294 | + |
| 295 | + def _save_config(): |
| 296 | + # Update configuration (auto-saves) |
| 297 | + self.config.update(gui, { |
| 298 | + "output_dir": output_edit.text(), |
| 299 | + "optimization": opt_spin.value() |
| 300 | + }) |
| 301 | + safe_log(gui, "✅ Configuration saved") |
| 302 | + |
| 303 | + save_btn.clicked.connect(_save_config) |
| 304 | + layout.addWidget(save_btn) |
| 305 | + layout.addStretch() |
| 306 | + |
| 307 | + return tab, "My Engine" |
| 308 | + except Exception as e: |
| 309 | + safe_log(gui, f"Error: {e}") |
| 310 | + return None |
| 311 | +``` |
| 312 | + |
| 313 | +## Benefits |
| 314 | + |
| 315 | +### For Users |
| 316 | +- ✅ Configuration persists automatically |
| 317 | +- ✅ No need to manually save |
| 318 | +- ✅ Settings are always up-to-date |
| 319 | +- ✅ Better user experience |
| 320 | + |
| 321 | +### For Developers |
| 322 | +- ✅ Simpler code |
| 323 | +- ✅ No manual save() calls |
| 324 | +- ✅ Automatic persistence |
| 325 | +- ✅ Less error-prone |
| 326 | +- ✅ Cleaner API |
| 327 | + |
| 328 | +### For the Project |
| 329 | +- ✅ Better architecture |
| 330 | +- ✅ Consistent behavior |
| 331 | +- ✅ Easier to maintain |
| 332 | +- ✅ Production-ready |
| 333 | +- ✅ Scalable design |
| 334 | + |
| 335 | +## Migration from Manual Save |
| 336 | + |
| 337 | +### Before (Manual Save) |
| 338 | + |
| 339 | +```python |
| 340 | +def preflight(self, gui, file: str) -> bool: |
| 341 | + self.config.load(gui) |
| 342 | + timeout = self.config.get(gui, "timeout", default=30) |
| 343 | + self.config.set(gui, "timeout", 60) |
| 344 | + self.config.save(gui) # ❌ Manual save |
| 345 | + return True |
| 346 | +``` |
| 347 | + |
| 348 | +### After (Auto-Save) |
| 349 | + |
| 350 | +```python |
| 351 | +def preflight(self, gui, file: str) -> bool: |
| 352 | + timeout = self.config.get(gui, "timeout", default=30) |
| 353 | + self.config.set(gui, "timeout", 60) # ✅ Auto-saves |
| 354 | + return True |
| 355 | +``` |
| 356 | + |
| 357 | +## Troubleshooting |
| 358 | + |
| 359 | +### Configuration not persisting |
| 360 | + |
| 361 | +1. Check that `gui.workspace_dir` is set |
| 362 | +2. Check that `ark_engines_config/` directory is writable |
| 363 | +3. Check that JSON serialization works for your values |
| 364 | +4. Run `verify_engines_loader.py` for diagnostics |
| 365 | + |
| 366 | +### Configuration not loading |
| 367 | + |
| 368 | +1. Check that configuration file exists |
| 369 | +2. Check that JSON is valid |
| 370 | +3. Check that `gui.workspace_dir` is correct |
| 371 | +4. Try calling `self.config.reset(gui)` to reload |
| 372 | + |
| 373 | +### Performance issues |
| 374 | + |
| 375 | +- Auto-save happens on every change |
| 376 | +- For bulk updates, use `update()` instead of multiple `set()` calls |
| 377 | +- Example: `self.config.update(gui, {key1: val1, key2: val2})` |
| 378 | + |
| 379 | +## Summary |
| 380 | + |
| 381 | +The new auto-save system provides: |
| 382 | +- ✅ Automatic configuration persistence |
| 383 | +- ✅ Simple, clean API |
| 384 | +- ✅ No manual save() calls needed |
| 385 | +- ✅ Better user experience |
| 386 | +- ✅ Production-ready |
| 387 | + |
| 388 | +Use it in all new engines! |
0 commit comments