Locales added

This commit is contained in:
Profitroll
2022-10-20 12:24:32 +02:00
parent 29d725e6db
commit b173544506
6 changed files with 149 additions and 38 deletions

View File

@@ -1,8 +1,9 @@
from typing import Union
from ujson import JSONDecodeError as JSONDecodeError
from ujson import loads, dumps
from sys import exit
from os import kill
from os import kill, sep
from os import name as osname
from traceback import print_exc
@@ -33,14 +34,19 @@ def jsonSave(contents, filename):
return
def configSet(key: str, value, *args: str):
def configSet(key: str, value, *args: str, file: str = "config"):
"""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].
* file (str): User ID to save. Saved to config if not provided. Defaults to "config".
"""
this_dict = jsonLoad("config.json")
if file == "config":
filepath = ""
else:
filepath = f"data{sep}users{sep}"
this_dict = jsonLoad(f"{filepath}{file}.json")
string = "this_dict"
for arg in args:
string += f'["{arg}"]'
@@ -49,54 +55,55 @@ def configSet(key: str, value, *args: str):
else:
string += f'["{key}"] = {value}'
exec(string)
jsonSave(this_dict, "config.json")
jsonSave(this_dict, f"{filepath}{file}.json")
return
def configGet(key: str, *args: str):
def configGet(key: str, *args: str, file: str = "config"):
"""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].
* file (str): User ID to load. Loads config if not provided. Defaults to "config".
Returns:
* any: Value of provided key
"""
this_dict = jsonLoad("config.json")
if file == "config":
this_dict = jsonLoad("config.json")
else:
this_dict = jsonLoad(f"data{sep}users{sep}{file}.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")
def locale(key: str, *args: str, locale=configGet("locale")) -> Union[str, list, dict]:
"""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")}{sep}{locale}.json')
# except FileNotFoundError:
# try:
# 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')
# except:
# return f'⚠️ Locale in config is invalid: could not get "{key}" in {str(args)} from locale "{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')
except FileNotFoundError:
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]
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:
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