djkesu commited on
Commit
f90475c
1 Parent(s): a4f441f

added attribute conn

Browse files
Files changed (1) hide show
  1. app_utils/conf.py +13 -24
app_utils/conf.py CHANGED
@@ -2,20 +2,11 @@ import sqlite3
2
  from pathlib import Path
3
  from typing import Any, Dict
4
 
5
- from pydantic import BaseModel
6
-
7
- class PersistentSettings(BaseModel):
8
  """
9
- This pydantic model will try to initialize itself from
10
- the database upon every instantiation
11
-
12
- It further supplies an update function, that allows to write
13
- back any changes into the database, under its key.
14
  """
15
 
16
- class Config:
17
- arbitrary_types_allowed = True # Exclude conn from Pydantic validation
18
-
19
  def __init__(self, **data: Any):
20
  # Connect to the SQLite database
21
  self.conn = sqlite3.connect("config.db")
@@ -29,7 +20,9 @@ class PersistentSettings(BaseModel):
29
  """)
30
 
31
  # Fetch settings from the database and initialize
32
- super().__init__(**self.fetch_settings(), **data)
 
 
33
 
34
  def fetch_settings(self) -> Dict[str, Any]:
35
  """
@@ -43,12 +36,12 @@ class PersistentSettings(BaseModel):
43
 
44
  def update(self, **data: Any) -> None:
45
  """
46
- Persist the pydantic-dict that represents the model
47
  """
48
  cursor = self.conn.cursor()
49
 
50
  # Update or insert each key-value pair into the database
51
- for key, value in {**self.dict(), **data}.items():
52
  cursor.execute(
53
  "INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)",
54
  (key, value)
@@ -65,15 +58,11 @@ class PersistentSettings(BaseModel):
65
  self.conn.close()
66
 
67
  class TortoiseConfig(PersistentSettings):
68
- EXTRA_VOICES_DIR: str = ""
69
- AR_CHECKPOINT: str = "."
70
- DIFF_CHECKPOINT: str = "."
71
- LOW_VRAM: bool = True
72
- conn: sqlite3.Connection = None
73
-
74
  def __init__(self, **data: Any):
75
  super().__init__(**data)
76
- if not Path(self.AR_CHECKPOINT).is_file():
77
- self.AR_CHECKPOINT = "."
78
- if not Path(self.DIFF_CHECKPOINT).is_file():
79
- self.DIFF_CHECKPOINT = "."
 
 
 
2
  from pathlib import Path
3
  from typing import Any, Dict
4
 
5
+ class PersistentSettings:
 
 
6
  """
7
+ This class manages the persistent settings and the database connection.
 
 
 
 
8
  """
9
 
 
 
 
10
  def __init__(self, **data: Any):
11
  # Connect to the SQLite database
12
  self.conn = sqlite3.connect("config.db")
 
20
  """)
21
 
22
  # Fetch settings from the database and initialize
23
+ self.settings = self.fetch_settings()
24
+ for key, value in data.items():
25
+ self.settings[key] = value
26
 
27
  def fetch_settings(self) -> Dict[str, Any]:
28
  """
 
36
 
37
  def update(self, **data: Any) -> None:
38
  """
39
+ Persist the dictionary that represents the settings into the database
40
  """
41
  cursor = self.conn.cursor()
42
 
43
  # Update or insert each key-value pair into the database
44
+ for key, value in data.items():
45
  cursor.execute(
46
  "INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)",
47
  (key, value)
 
58
  self.conn.close()
59
 
60
  class TortoiseConfig(PersistentSettings):
 
 
 
 
 
 
61
  def __init__(self, **data: Any):
62
  super().__init__(**data)
63
+ if not Path(self.settings.get("AR_CHECKPOINT", "")).is_file():
64
+ self.settings["AR_CHECKPOINT"] = "."
65
+ if not Path(self.settings.get("DIFF_CHECKPOINT", "")).is_file():
66
+ self.settings["DIFF_CHECKPOINT"] = "."
67
+ self.EXTRA_VOICES_DIR = self.settings.get("EXTRA_VOICES_DIR", "")
68
+ self.LOW_VRAM = self.settings.get("LOW_VRAM", True)