Optimized imports

This commit is contained in:
Profitroll 2022-09-18 18:27:13 +02:00
parent f0c333968b
commit c4177d1575

View File

@ -5,11 +5,11 @@ except ModuleNotFoundError:
from json import JSONDecodeError as JSONDecodeError from json import JSONDecodeError as JSONDecodeError
from json import loads, dumps from json import loads, dumps
import os from sys import exit
import sys from os import sep, kill
import traceback from os import name as osname
from traceback import print_exc
from signal import SIGKILL # type: ignore
from modules.logging import logWrite from modules.logging import logWrite
def jsonLoad(filename): def jsonLoad(filename):
@ -18,10 +18,10 @@ def jsonLoad(filename):
try: try:
output = loads(file.read()) output = loads(file.read())
except JSONDecodeError: except JSONDecodeError:
logWrite(f"Could not load json file {filename}: file seems to be incorrect!\n{traceback.print_exc()}") logWrite(f"Could not load json file {filename}: file seems to be incorrect!\n{print_exc()}")
raise raise
except FileNotFoundError: except FileNotFoundError:
logWrite(f"Could not load json file {filename}: file does not seem to exist!\n{traceback.print_exc()}") logWrite(f"Could not load json file {filename}: file does not seem to exist!\n{print_exc()}")
raise raise
file.close() file.close()
return output return output
@ -33,7 +33,7 @@ def jsonSave(contents, filename):
file.write(dumps(contents, ensure_ascii=False, indent=4)) file.write(dumps(contents, ensure_ascii=False, indent=4))
file.close() file.close()
except Exception as exp: except Exception as exp:
logWrite(f"Could not save json file {filename}: {exp}\n{traceback.print_exc()}") logWrite(f"Could not save json file {filename}: {exp}\n{print_exc()}")
return return
@ -83,13 +83,13 @@ def locale(key: str, *args: str, locale=configGet("locale")):
locale = configGet("locale") locale = configGet("locale")
try: try:
this_dict = jsonLoad(f'{configGet("locale", "locations")}{os.sep}{locale}.json') this_dict = jsonLoad(f'{configGet("locale", "locations")}{sep}{locale}.json')
except FileNotFoundError: except FileNotFoundError:
try: try:
this_dict = jsonLoad(f'{configGet("locale", "locations")}{os.sep}{configGet("locale")}.json') this_dict = jsonLoad(f'{configGet("locale", "locations")}{sep}{configGet("locale")}.json')
except FileNotFoundError: except FileNotFoundError:
try: try:
this_dict = jsonLoad(f'{configGet("locale_fallback", "locations")}{os.sep}{configGet("locale")}.json') this_dict = jsonLoad(f'{configGet("locale_fallback", "locations")}{sep}{configGet("locale")}.json')
except: except:
return f'⚠️ Locale in config is invalid: could not get "{key}" in {str(args)} from locale "{locale}"' return f'⚠️ Locale in config is invalid: could not get "{key}" in {str(args)} from locale "{locale}"'
@ -103,14 +103,15 @@ def locale(key: str, *args: str, locale=configGet("locale")):
return f'⚠️ Locale in config is invalid: could not get "{key}" in {str(args)} from locale "{locale}"' return f'⚠️ Locale in config is invalid: could not get "{key}" in {str(args)} from locale "{locale}"'
try: try:
import psutil from psutil import Process
except ModuleNotFoundError: except ModuleNotFoundError:
print(locale("deps_missing", "console", locale=configGet("locale")), flush=True) print(locale("deps_missing", "console", locale=configGet("locale")), flush=True)
sys.exit() exit()
def killProc(pid): def killProc(pid):
if os.name == "posix": if osname == "posix":
os.kill(pid, SIGKILL) from signal import SIGKILL # type: ignore
kill(pid, SIGKILL)
else: else:
p = psutil.Process(pid) p = Process(pid)
p.kill() p.kill()