|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Copyright 2026 Ague Samuel Amen |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +""" |
| 17 | +EngineConfigManager |
| 18 | +
|
| 19 | +Persists engine UI configuration per workspace at: |
| 20 | + <workspace>/.ark/<engine_id>/config.json |
| 21 | +""" |
| 22 | + |
| 23 | +from __future__ import annotations |
| 24 | + |
| 25 | +import json |
| 26 | +import os |
| 27 | +import re |
| 28 | +from datetime import datetime, timezone |
| 29 | +from typing import Any, Optional |
| 30 | + |
| 31 | +ENGINE_CONFIG_DIRNAME = ".ark" |
| 32 | +ENGINE_CONFIG_BASENAME = "config.json" |
| 33 | +ENGINE_CONFIG_VERSION = 1 |
| 34 | + |
| 35 | + |
| 36 | +def _safe_engine_id(engine_id: str) -> str: |
| 37 | + try: |
| 38 | + raw = str(engine_id or "").strip() |
| 39 | + safe = re.sub(r"[^A-Za-z0-9._-]+", "_", raw) |
| 40 | + return safe or "engine" |
| 41 | + except Exception: |
| 42 | + return "engine" |
| 43 | + |
| 44 | + |
| 45 | +def _engine_config_dir(workspace_dir: str, engine_id: str) -> str: |
| 46 | + return os.path.join(workspace_dir, ENGINE_CONFIG_DIRNAME, _safe_engine_id(engine_id)) |
| 47 | + |
| 48 | + |
| 49 | +def _engine_config_path(workspace_dir: str, engine_id: str) -> str: |
| 50 | + return os.path.join(_engine_config_dir(workspace_dir, engine_id), ENGINE_CONFIG_BASENAME) |
| 51 | + |
| 52 | + |
| 53 | +def _atomic_write_json(path: str, data: dict) -> None: |
| 54 | + tmp = path + ".tmp" |
| 55 | + with open(tmp, "w", encoding="utf-8") as f: |
| 56 | + json.dump(data, f, indent=4) |
| 57 | + os.replace(tmp, path) |
| 58 | + |
| 59 | + |
| 60 | +def load_engine_config(workspace_dir: str, engine_id: str) -> dict[str, Any]: |
| 61 | + if not workspace_dir or not engine_id: |
| 62 | + return {} |
| 63 | + path = _engine_config_path(workspace_dir, engine_id) |
| 64 | + if not os.path.isfile(path): |
| 65 | + return {} |
| 66 | + try: |
| 67 | + with open(path, encoding="utf-8") as f: |
| 68 | + data = json.load(f) |
| 69 | + return data if isinstance(data, dict) else {} |
| 70 | + except Exception: |
| 71 | + return {} |
| 72 | + |
| 73 | + |
| 74 | +def save_engine_config( |
| 75 | + workspace_dir: str, |
| 76 | + engine_id: str, |
| 77 | + options: Optional[dict], |
| 78 | + engine_version: Optional[str] = None, |
| 79 | +) -> bool: |
| 80 | + if not workspace_dir or not engine_id: |
| 81 | + return False |
| 82 | + try: |
| 83 | + cfg_dir = _engine_config_dir(workspace_dir, engine_id) |
| 84 | + os.makedirs(cfg_dir, exist_ok=True) |
| 85 | + payload = { |
| 86 | + "meta": { |
| 87 | + "engine_id": str(engine_id), |
| 88 | + "version": ENGINE_CONFIG_VERSION, |
| 89 | + "updated_at": datetime.now(timezone.utc).isoformat(), |
| 90 | + }, |
| 91 | + "options": options if isinstance(options, dict) else {}, |
| 92 | + } |
| 93 | + if engine_version: |
| 94 | + payload["meta"]["engine_version"] = str(engine_version) |
| 95 | + _atomic_write_json(_engine_config_path(workspace_dir, engine_id), payload) |
| 96 | + return True |
| 97 | + except Exception: |
| 98 | + return False |
| 99 | + |
| 100 | + |
| 101 | +def apply_engine_config(gui, engine, data: dict) -> None: |
| 102 | + if not isinstance(data, dict): |
| 103 | + return |
| 104 | + opts = data.get("options") if isinstance(data, dict) else None |
| 105 | + if not isinstance(opts, dict): |
| 106 | + opts = data if isinstance(data, dict) else {} |
| 107 | + try: |
| 108 | + fn = getattr(engine, "set_config", None) |
| 109 | + if callable(fn): |
| 110 | + fn(gui, opts) |
| 111 | + except Exception: |
| 112 | + pass |
| 113 | + |
| 114 | + |
| 115 | +def apply_engine_configs_for_workspace(gui, workspace_dir: str) -> None: |
| 116 | + if not workspace_dir: |
| 117 | + return |
| 118 | + try: |
| 119 | + import EngineLoader as engines_loader |
| 120 | + |
| 121 | + for eid in engines_loader.registry.available_engines(): |
| 122 | + try: |
| 123 | + engine = engines_loader.registry.get_instance(eid) |
| 124 | + if not engine: |
| 125 | + continue |
| 126 | + data = load_engine_config(workspace_dir, eid) |
| 127 | + if data: |
| 128 | + apply_engine_config(gui, engine, data) |
| 129 | + except Exception: |
| 130 | + continue |
| 131 | + except Exception: |
| 132 | + pass |
| 133 | + |
| 134 | + try: |
| 135 | + if hasattr(gui, "update_command_preview"): |
| 136 | + gui.update_command_preview() |
| 137 | + except Exception: |
| 138 | + pass |
| 139 | + |
| 140 | + |
| 141 | +def save_engine_config_for_gui(gui, engine_id: str) -> bool: |
| 142 | + try: |
| 143 | + workspace_dir = getattr(gui, "workspace_dir", None) |
| 144 | + if not workspace_dir or not engine_id: |
| 145 | + return False |
| 146 | + import EngineLoader as engines_loader |
| 147 | + |
| 148 | + engine = engines_loader.registry.get_instance(engine_id) |
| 149 | + if not engine: |
| 150 | + return False |
| 151 | + options = {} |
| 152 | + try: |
| 153 | + fn = getattr(engine, "get_config", None) |
| 154 | + if callable(fn): |
| 155 | + options = fn(gui) or {} |
| 156 | + except Exception: |
| 157 | + options = {} |
| 158 | + return save_engine_config( |
| 159 | + workspace_dir, |
| 160 | + engine_id, |
| 161 | + options, |
| 162 | + getattr(engine, "version", None), |
| 163 | + ) |
| 164 | + except Exception: |
| 165 | + return False |
0 commit comments