File size: 2,174 Bytes
e302830
3bbf2c7
e302830
3bbf2c7
f90475c
3bbf2c7
f90475c
3bbf2c7
 
 
e302830
 
 
 
 
 
 
 
 
 
 
 
f90475c
 
 
e302830
 
 
 
 
 
 
 
 
 
3bbf2c7
 
 
f90475c
3bbf2c7
e302830
 
 
f90475c
e302830
 
 
 
 
 
 
 
3bbf2c7
e302830
 
 
 
 
3bbf2c7
 
 
 
f90475c
 
 
 
 
 
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
import sqlite3
from pathlib import Path
from typing import Any, Dict

class PersistentSettings:
    """
    This class manages the persistent settings and the database connection.
    """

    def __init__(self, **data: Any):
        # Connect to the SQLite database
        self.conn = sqlite3.connect("config.db")

        # Create a table for settings if it doesn't exist
        self.conn.execute("""
            CREATE TABLE IF NOT EXISTS settings (
                key TEXT PRIMARY KEY,
                value TEXT
            )
        """)

        # Fetch settings from the database and initialize
        self.settings = self.fetch_settings()
        for key, value in data.items():
            self.settings[key] = value

    def fetch_settings(self) -> Dict[str, Any]:
        """
        Retrieve settings from the database
        """
        cursor = self.conn.cursor()
        cursor.execute("SELECT key, value FROM settings")
        settings = dict(cursor.fetchall())
        cursor.close()
        return settings

    def update(self, **data: Any) -> None:
        """
        Persist the dictionary that represents the settings into the database
        """
        cursor = self.conn.cursor()

        # Update or insert each key-value pair into the database
        for key, value in data.items():
            cursor.execute(
                "INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)",
                (key, value)
            )

        # Commit the changes to the database
        self.conn.commit()
        cursor.close()

    def close(self) -> None:
        """
        Close the database connection
        """
        self.conn.close()

class TortoiseConfig(PersistentSettings):
    def __init__(self, **data: Any):
        super().__init__(**data)
        if not Path(self.settings.get("AR_CHECKPOINT", "")).is_file():
            self.settings["AR_CHECKPOINT"] = "."
        if not Path(self.settings.get("DIFF_CHECKPOINT", "")).is_file():
            self.settings["DIFF_CHECKPOINT"] = "."
        self.EXTRA_VOICES_DIR = self.settings.get("EXTRA_VOICES_DIR", "")
        self.LOW_VRAM = self.settings.get("LOW_VRAM", True)