djkesu's picture
added attribute conn
f90475c
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)