Add method to load json and re-throw on error
import json
+from json import JSONDecodeError
+
from dotenv import load_dotenv
from os import getenv
from plex_trakt_sync.path import config_file, env_file, default_config_file
"""
TRAKT_POST_DELAY = 1.1
+
class Config(dict):
env_keys = [
"PLEX_BASEURL",
@@ -31,16 +34,14 @@ def __getitem__(self, item):
return dict.__getitem__(self, item)
def initialize(self):
- with open(default_config_file, "r") as fp:
- defaults = json.load(fp)
+ defaults = self.load_json(default_config_file)
self.update(defaults)
if not exists(config_file):
with open(config_file, "w") as fp:
fp.write(json.dumps(defaults, indent=4))
- with open(config_file, "r") as fp:
- config = json.load(fp)
+ config = self.load_json(config_file)
self.update(config)
self.initialized = True
@@ -64,5 +65,13 @@ def save(self):
else:
txt.write("{}=\n".format(key))
+ def load_json(self, path):
+ with open(path, "r") as fp:
+ try:
+ config = json.load(fp)
+ except JSONDecodeError as e:
+ raise RuntimeError(f"Unable to parse {path}: {e}")
+ return config
+
CONFIG = Config()
