Skip to content

Commit

Permalink
writing the states and settings jsons is now more robust (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-mauermann authored Oct 8, 2024
1 parent 35079be commit 2d22ea1
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions localization.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import utils

SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
FALLBACK_LANGUAGE = "en"
Expand All @@ -12,20 +13,26 @@

def ensure_settings_file():
if not os.path.exists(GUI_SETTINGS_PATH):
with open(GUI_SETTINGS_PATH, "w") as f:
settings = {"language-id": FALLBACK_LANGUAGE}
f.write(json.dumps(settings, indent=4))
try:
with open(GUI_SETTINGS_PATH, "w") as f:
settings = {"language-id": FALLBACK_LANGUAGE}
f.write(json.dumps(settings, indent=4))
except Exception as e:
utils.writeErrorLog(e)


def get_state_dict() -> dict:

try:
with open(STATE_SETTINGS_PATH, "r", encoding="utf-8") as f:
return json.load(f)
except FileNotFoundError:
with open(STATE_SETTINGS_PATH, "w") as f:
json.dump({}, f)
return {}
try:
with open(STATE_SETTINGS_PATH, "w") as f:
json.dump({}, f)
return {}
except Exception as e:
utils.writeErrorLog(e)
return {}


def get_state(key: str, default=None) -> str:
Expand All @@ -35,8 +42,11 @@ def get_state(key: str, default=None) -> str:
def set_state(key: str, value: str):
state = get_state_dict()
state[key] = value
with open(STATE_SETTINGS_PATH, "w") as f:
json.dump(state, f, indent=4)
try:
with open(STATE_SETTINGS_PATH, "w") as f:
json.dump(state, f, indent=4)
except Exception as e:
utils.writeErrorLog(e)


def load_local_state():
Expand Down

0 comments on commit 2d22ea1

Please sign in to comment.