From c4177d1575ff0d22ff29e6eaf57e1f6cfcd7022c Mon Sep 17 00:00:00 2001 From: Profitroll <47523801+profitrollgame@users.noreply.github.com> Date: Sun, 18 Sep 2022 18:27:13 +0200 Subject: [PATCH] Optimized imports --- modules/utils.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/modules/utils.py b/modules/utils.py index 1380140..d8731b1 100644 --- a/modules/utils.py +++ b/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() \ No newline at end of file