from json import JSONDecodeError, dumps, loads from typing import Any import darkdetect def jsonLoad(filename): """Loads arg1 as json and returns its contents""" with open(filename, "r", encoding='utf8') as file: try: output = loads(file.read()) except JSONDecodeError: raise except FileNotFoundError: raise return output def jsonSave(contents, filename): """Dumps dict/list arg1 to file arg2""" try: with open(filename, "w", encoding='utf8') as file: file.write(dumps(contents, ensure_ascii=False, indent=4)) except Exception as exp: raise return def nested_set(dic, keys, value, create_missing=True): d = dic for key in keys[:-1]: if key in d: d = d[key] elif create_missing: d = d.setdefault(key, {}) else: return dic if keys[-1] in d or create_missing: d[keys[-1]] = value return dic def configSet(keys: list, value: Any, create_missing=True) -> None: """Set config's value to provided one ### Args: * keys (`list`): List of keys from the highest one to target. * value (`Any`): Needed value. * create_missing (`bool`, optional): Create missing items on the way. Defaults to True. """ this_dict = jsonLoad("config.json") this_dict = nested_set(this_dict, keys, value, create_missing=create_missing) jsonSave(this_dict, "config.json") return def configGet(key: str, *args: str) -> Any: """Get value of the config key ### Args: * key (`str`): The last key of the keys path. * *args (`str`): Path to key like: dict[args][key]. ### Returns: * any: Value of provided key """ try: this_dict = jsonLoad("config.json") except FileNotFoundError: print("Config file not found!", flush=True) exit() this_key = this_dict for dict_key in args: this_key = this_key[dict_key] return this_key[key] def use_dark_mode() -> bool: """Return whether dark mode should be used ### Returns: * `bool`: True if yes and False if no """ if configGet("dark_mode_auto") is True: return bool(darkdetect.isDark()) else: return configGet("dark_mode")