File size: 1,506 Bytes
6bcacf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import yaml
from os import path, makedirs
from models.settings import Settings
from paths import FastStableDiffusionPaths


class AppSettings:
    def __init__(self):
        self.config_path = FastStableDiffusionPaths().get_app_settings_path()

    @property
    def settings(self):
        return self._config

    def load(self):
        if not path.exists(self.config_path):
            base_dir = path.dirname(self.config_path)
            if not path.exists(base_dir):
                makedirs(base_dir)
            try:
                print("Settings not found creating default settings")
                with open(self.config_path, "w") as file:
                    yaml.dump(
                        self._load_default(),
                        file,
                    )
            except Exception as ex:
                print(f"Error in creating settings : {ex}")
                exit()
        try:
            with open(self.config_path) as file:
                settings_dict = yaml.safe_load(file)
                self._config = Settings.parse_obj(settings_dict)
        except Exception as ex:
            print(f"Error in loading settings : {ex}")

    def save(self):
        try:
            with open(self.config_path, "w") as file:
                yaml.dump(self._config.dict(), file)
        except Exception as ex:
            print(f"Error in saving settings : {ex}")

    def _load_default(self) -> dict:
        defult_config = Settings()
        return defult_config.dict()