From 9d2e20b4260678d78f1ea35b04b525d414011346 Mon Sep 17 00:00:00 2001 From: zackees Date: Tue, 9 May 2023 18:53:58 -0700 Subject: [PATCH] adds settings that i forgot to add --- src/video_subtitles/settings.py | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/video_subtitles/settings.py diff --git a/src/video_subtitles/settings.py b/src/video_subtitles/settings.py new file mode 100644 index 0000000..84cfc1f --- /dev/null +++ b/src/video_subtitles/settings.py @@ -0,0 +1,41 @@ +""" +Reads and writes settings to a json file. +""" + +import json +import os + +HERE = os.path.dirname(os.path.abspath(__file__)) + +SETTINGS_JSON = os.path.join(HERE, "settings.json") + + +class Settings: + """Settings class.""" + + def __init__(self) -> None: + self.data: dict[str, str | int | dict] = {} + self.load() + + def deepl_key(self) -> str | None: + """Return the DeepL API key.""" + return self.data.get("deepl_key", None) # type: ignore + + def set_deepl_key(self, key: str) -> None: + """Set the DeepL API key.""" + self.data["deepl_key"] = key + + def save(self) -> None: + """Save the settings.""" + # dump json to file + with open(SETTINGS_JSON, encoding="utf-8", mode="w") as f: + json.dump(self.data, f, indent=4) + + def load(self) -> None: + """Load the settings.""" + # load json from file + if not os.path.exists(SETTINGS_JSON): + self.data = {} + return + with open(SETTINGS_JSON, encoding="utf-8", mode="r") as f: + self.data = json.load(f)