Now using black for formatting
This commit is contained in:
@@ -13,24 +13,30 @@ from typing import Any
|
||||
|
||||
from modules.logger import logWrite
|
||||
|
||||
|
||||
def jsonLoad(filename: str) -> Any:
|
||||
"""Loads arg1 as json and returns its contents"""
|
||||
with open(filename, "r", encoding='utf8') as file:
|
||||
with open(filename, "r", encoding="utf8") as file:
|
||||
try:
|
||||
output = loads(file.read())
|
||||
except JSONDecodeError:
|
||||
logWrite(f"Could not load json file {filename}: file seems to be incorrect!\n{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{print_exc()}")
|
||||
logWrite(
|
||||
f"Could not load json file {filename}: file does not seem to exist!\n{print_exc()}"
|
||||
)
|
||||
raise
|
||||
file.close()
|
||||
return output
|
||||
|
||||
|
||||
def jsonSave(contents: Any, filename: str) -> None:
|
||||
"""Dumps dict/list arg1 to file arg2"""
|
||||
try:
|
||||
with open(filename, "w", encoding='utf8') as file:
|
||||
with open(filename, "w", encoding="utf8") as file:
|
||||
file.write(dumps(contents, ensure_ascii=False, indent=4))
|
||||
file.close()
|
||||
except Exception as exp:
|
||||
@@ -44,7 +50,7 @@ def configSet(key: str, value, *args: str):
|
||||
* 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:
|
||||
@@ -57,6 +63,7 @@ def configSet(key: str, value, *args: str):
|
||||
jsonSave(this_dict, "config.json")
|
||||
return
|
||||
|
||||
|
||||
def configGet(key: str, *args: str):
|
||||
"""Get value of the config key
|
||||
Args:
|
||||
@@ -64,13 +71,14 @@ def configGet(key: str, *args: str):
|
||||
* *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:
|
||||
@@ -79,36 +87,42 @@ def locale(key: str, *args: str, locale=configGet("locale")):
|
||||
* locale (str): Locale to looked up in. Defaults to config's locale value.
|
||||
Returns:
|
||||
* any: Value of provided locale key
|
||||
"""
|
||||
if (locale == None):
|
||||
"""
|
||||
if locale == None:
|
||||
locale = configGet("locale")
|
||||
|
||||
|
||||
try:
|
||||
this_dict = jsonLoad(f'{configGet("locale", "locations")}{sep}{locale}.json')
|
||||
except FileNotFoundError:
|
||||
try:
|
||||
this_dict = jsonLoad(f'{configGet("locale", "locations")}{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")}{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}"'
|
||||
|
||||
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:
|
||||
from psutil import Process
|
||||
except ModuleNotFoundError:
|
||||
print(locale("deps_missing", "console", locale=configGet("locale")), flush=True)
|
||||
exit()
|
||||
|
||||
|
||||
def killProc(pid: int) -> None:
|
||||
"""Kill process by its PID. Meant to be used to kill the main process of bot itself.
|
||||
|
||||
@@ -117,6 +131,7 @@ def killProc(pid: int) -> None:
|
||||
"""
|
||||
if osname == "posix":
|
||||
from signal import SIGKILL
|
||||
|
||||
kill(pid, SIGKILL)
|
||||
else:
|
||||
Process(pid).kill()
|
||||
Process(pid).kill()
|
||||
|
Reference in New Issue
Block a user