summarylogtreecommitdiffstats
path: root/installer_settings_manager.py
blob: 3375b2864876877915f83543385cc81e1825eee3 (plain)
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

import os
import json
from dotenv import load_dotenv, set_key

class SettingsManager:
    def __init__(self, root_dir):
        self.root_dir = root_dir
        self.env_path = os.path.join(root_dir, ".env")
        self.defaults = {
            "GROQ_API_KEY": "",
            "FLASK_PORT": "8000",
            "AI_NAME": "Belinda AI",
            "AI_PERSONALITY": "Intelligent assistant",
            "AI_MAX_TOKENS": "1024",
            "APP_THEME": "dark",
            "APP_LANGUAGE": "English",
            "APP_FONT_SIZE": "14",
            "APP_FONT_FAMILY": "Segoe UI",
            "APP_TITLE_SIZE": "24",
            "EXECUTION_MODE": "local"
        }
        
        if not os.path.exists(self.env_path):
            # Ensure the directory exists first
            os.makedirs(self.root_dir, exist_ok=True)
            
            template = os.path.join(root_dir, ".env.example")
            if os.path.exists(template):
                with open(template, 'r', encoding='utf-8', errors='ignore') as f:
                    content = f.read()
                with open(self.env_path, 'w', encoding='utf-8') as f:
                    f.write(content)
            else:
                try:
                    with open(self.env_path, 'w', encoding='utf-8') as f:
                        content = "\n".join([f"{k}={v}" for k, v in self.defaults.items()])
                        f.write(content)
                except: pass
        
        load_dotenv(self.env_path)

    def get(self, key, default=None):
        if default is None:
            default = self.defaults.get(key, "")
        value = os.getenv(key)
        if value is None:
            return default
        return value

    def set(self, key, value):
        set_key(self.env_path, key, str(value))
        os.environ[key] = str(value)

    def get_all(self):
        settings = {}
        if os.path.exists(self.env_path):
            with open(self.env_path, 'r', encoding='utf-8') as f:
                for line in f:
                    line = line.strip()
                    if line and '=' in line and not line.startswith('#'):
                        try:
                            k, v = line.split('=', 1)
                            settings[k.strip()] = v.strip()
                        except: pass
        return settings