try: import ujson as json from ujson import JSONDecodeError as JSONDecodeError except ModuleNotFoundError: import json from json import JSONDecodeError as JSONDecodeError import os import sys import traceback from signal import SIGKILL # type: ignore from modules.logging import logWrite def jsonLoad(filename): """Loads arg1 as json and returns its contents""" with open(filename, "r", encoding='utf8') as file: try: output = json.loads(file.read()) except JSONDecodeError: logWrite(f"Could not load json file {filename}: file seems to be incorrect!\n{traceback.print_exc()}") raise except FileNotFoundError: logWrite(f"Could not load json file {filename}: file does not seem to exist!\n{traceback.print_exc()}") raise file.close() return output def jsonSave(contents, filename): """Dumps dict/list arg1 to file arg2""" try: with open(filename, "w", encoding='utf8') as file: file.write(json.dumps(contents, ensure_ascii=False, indent=4)) file.close() except Exception as exp: logWrite(f"Could not save json file {filename}: {exp}\n{traceback.print_exc()}") return def configSet(key: str, value, *args: str): """Set key to a value Args: * key (str): The last key of the keys path. * value (str/int/float/list/dict/None): Some needed value. * *args (str): Path to key like: dict[args][key]. """ this_dict = jsonLoad("config.json") string = "this_dict" for arg in args: string += f'["{arg}"]' if type(value) in [str]: string += f'["{key}"] = "{value}"' else: string += f'["{key}"] = {value}' exec(string) jsonSave(this_dict, "config.json") return def configGet(key: str, *args: str): """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 """ this_dict = jsonLoad("config.json") this_key = this_dict for dict_key in args: this_key = this_key[dict_key] return this_key[key] def locale(key: str, *args: str, locale=configGet("locale")): """Get value of locale string Args: * key (str): The last key of the locale's keys path. * *args (list): Path to key like: dict[args][key]. * locale (str): Locale to looked up in. Defaults to config's locale value. Returns: * any: Value of provided locale key """ if (locale == None): locale = configGet("locale") try: this_dict = jsonLoad(f'{configGet("locale", "locations")}{os.sep}{locale}.json') except FileNotFoundError: try: this_dict = jsonLoad(f'{configGet("locale", "locations")}{os.sep}{configGet("locale")}.json') except FileNotFoundError: try: this_dict = jsonLoad(f'{configGet("locale_fallback", "locations")}{os.sep}{configGet("locale")}.json') except: return f'⚠️ Locale in config is invalid: could not get "{key}" in {str(args)} from locale "{locale}"' this_key = this_dict for dict_key in args: this_key = this_key[dict_key] try: return this_key[key] except KeyError: return f'⚠️ Locale in config is invalid: could not get "{key}" in {str(args)} from locale "{locale}"' try: import psutil except ModuleNotFoundError: print(locale("deps_missing", "console", locale=configGet("locale")), flush=True) sys.exit() def killProc(pid): if os.name == "posix": os.kill(pid, SIGKILL) else: p = psutil.Process(pid) p.kill()