Add config file for specifying backends

This commit is contained in:
2022-09-13 22:16:51 -06:00
parent afb7a456a1
commit 3227730a37
3 changed files with 49 additions and 11 deletions
+34 -9
View File
@@ -1,5 +1,6 @@
from typing import Type
import os
import sys
from urllib import parse
@@ -20,6 +21,7 @@ from PyQt5.QtGui import QImage
from .qtmpv import MpvObject
import requests
import toml
class Episode(QObject):
@@ -186,6 +188,31 @@ def DatabaseType(data_source) -> Type:
return Database
def load_config() -> list[str]:
try:
config_dir: str = os.path.join(os.environ["XDG_CONFIG_HOME"], "ikinuki")
except:
config_dir: str = os.path.join(os.environ["HOME"], ".config", "ikinuki")
os.makedirs(config_dir, exist_ok=True)
config_file: str = os.path.join(config_dir, "client.toml")
try:
config: dict = toml.load(config_file)
except FileNotFoundError:
print(f'Config file not found at "{config_file}"')
print("Writing example config file. Please update and relaunch.")
default_config = """# [[backends]]
# address = "127.0.0.1"
# port = 32520"""
with open(config_file, "w") as f:
f.write(default_config)
sys.exit(-1)
return [f'http://{b["address"]}:{b["port"]}/' for b in config["backends"]]
def main():
app = QApplication(sys.argv)
@@ -194,15 +221,13 @@ def main():
locale.setlocale(locale.LC_NUMERIC, "C")
data_source = DataSource(
[
# "http://127.0.0.1:8080/a/",
# "http://127.0.0.1:8080/b/",
# "http://127.0.0.1:8080/c/",
"http://127.0.0.1:32520/",
]
)
# data_source = DataSource([])
try:
backends: list[str] = load_config()
except Exception as e:
print(f"ERROR: Could not load config file: {repr(e)}")
sys.exit(-1)
data_source = DataSource(backends)
qmlRegisterType(DatabaseType(data_source), "Ikinuki.Client", 1, 0, "Database")
# qmlRegisterType(Provider, "Ikinuki.Client", 1, 0, "Provider")