1
0
Fork 0
Browse Source

Optimized imports

master
Profitroll 2 months ago
parent
commit
c4177d1575
  1. 31
      modules/utils.py

31
modules/utils.py

@ -5,11 +5,11 @@ except ModuleNotFoundError:
from json import JSONDecodeError as JSONDecodeError
from json import loads, dumps
import os
import sys
import traceback
from sys import exit
from os import sep, kill
from os import name as osname
from traceback import print_exc
from signal import SIGKILL # type: ignore
from modules.logging import logWrite
def jsonLoad(filename):
@ -18,10 +18,10 @@ def jsonLoad(filename):
try:
output = loads(file.read())
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
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
file.close()
return output
@ -33,7 +33,7 @@ def jsonSave(contents, filename):
file.write(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()}")
logWrite(f"Could not save json file {filename}: {exp}\n{print_exc()}")
return
@ -83,13 +83,13 @@ def locale(key: str, *args: str, locale=configGet("locale")):
locale = configGet("locale")
try:
this_dict = jsonLoad(f'{configGet("locale", "locations")}{os.sep}{locale}.json')
this_dict = jsonLoad(f'{configGet("locale", "locations")}{sep}{locale}.json')
except FileNotFoundError:
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:
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:
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}"'
try:
import psutil
from psutil import Process
except ModuleNotFoundError:
print(locale("deps_missing", "console", locale=configGet("locale")), flush=True)
sys.exit()
exit()
def killProc(pid):
if os.name == "posix":
os.kill(pid, SIGKILL)
if osname == "posix":
from signal import SIGKILL # type: ignore
kill(pid, SIGKILL)
else:
p = psutil.Process(pid)
p = Process(pid)
p.kill()
Loading…
Cancel
Save