File size: 2,326 Bytes
e0265b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from __future__ import annotations

import json
from copy import deepcopy
from pathlib import Path
from typing import Any


DEFAULT_SETTINGS: dict[str, Any] = {
    "provider": "ollama",
    "ollama_url": "http://localhost:11434",
    "ollama_model": "qwen2.5:1.5b",
    "ollama_chat_max_tokens": 1024,
    "web_search_enabled": True,
    "web_link_reading_enabled": True,
    "command_center_mode": "trainer",
    "desktop_notifications": True,
    "sound_notifications": False,
    "ask_before_long_tasks": True,
    "atlas_warning_temperature": 82,
    "atlas_critical_temperature": 90,
    "atlas_warning_disk_gb": 10,
    "atlas_critical_disk_gb": 1,
    "atlas_stall_minutes": 30,
    "trusted_dataset_ddpm_automation": False,
    "demo_step_delay": 0.24,
    "max_dataset_images_without_confirmation": 100,
    "training_presets": {},
    "tool_folders": {
        "dataset_collector": "",
        "caption_generator": "",
        "lora_trainer": "",
        "ddpm_trainer": "",
        "flow_trainer": "",
        "preview_generator": "",
    },
}


class ConfigManager:
    def __init__(self, root: Path) -> None:
        self.root = root
        self.config_dir = root / "config"
        self.config_dir.mkdir(parents=True, exist_ok=True)
        self.settings_path = self.config_dir / "settings.json"
        self.settings: dict[str, Any] = {}
        self.load()

    def load(self) -> dict[str, Any]:
        self.settings = deepcopy(DEFAULT_SETTINGS)
        if self.settings_path.exists():
            try:
                stored = json.loads(self.settings_path.read_text(encoding="utf-8"))
                if isinstance(stored, dict):
                    self.settings.update(stored)
            except (OSError, json.JSONDecodeError):
                pass
        else:
            self.save()
        return self.settings

    def save(self) -> None:
        temporary = self.settings_path.with_suffix(".tmp")
        temporary.write_text(
            json.dumps(self.settings, indent=2, sort_keys=True),
            encoding="utf-8",
        )
        temporary.replace(self.settings_path)

    def get(self, key: str, default: Any = None) -> Any:
        return self.settings.get(key, default)

    def update(self, values: dict[str, Any]) -> None:
        self.settings.update(values)
        self.save()