Compare commits

...

7 Commits
v0.4 ... v0.7

Author SHA1 Message Date
8052d57a40 Updated to 0.7 2023-05-26 15:52:29 +02:00
ab3d714727 Added in_every_locale() to i18n 2023-05-26 15:52:10 +02:00
a843f77290 Fixed missing import 2023-05-26 13:13:23 +02:00
21ce78f475 Fixed formatting to be black-compliant 2023-05-26 13:04:08 +02:00
753255e649 Updated to version 0.5 2023-05-26 13:03:04 +02:00
b40d1fe911 Added sync i18n 2023-05-26 13:02:18 +02:00
ce0503500a Small refactor is made 2023-05-26 13:02:09 +02:00
4 changed files with 154 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
__name__ = "libbot"
__version__ = "0.4"
__version__ = "0.7"
__license__ = "GPL3"
__author__ = "Profitroll"

View File

@@ -1,6 +1,6 @@
from os import listdir
from pathlib import Path
from typing import Any
from typing import Any, Dict
from libbot import config_get, json_read, sync
@@ -31,7 +31,7 @@ async def _(key: str, *args: str, locale: str = sync.config_get("locale")) -> An
)
)
except FileNotFoundError:
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 {args} from locale "{locale}"'
this_key = this_dict
for dict_key in args:
@@ -40,7 +40,7 @@ async def _(key: str, *args: str, locale: str = sync.config_get("locale")) -> An
try:
return this_key[key]
except KeyError:
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 {args} from locale "{locale}"'
async def in_all_locales(key: str, *args: str) -> list:
@@ -55,13 +55,9 @@ async def in_all_locales(key: str, *args: str) -> list:
"""
output = []
valid_locales = []
files_locales = listdir(await config_get("locale", "locations"))
for entry in files_locales:
valid_locales.append(".".join(entry.split(".")[:-1]))
valid_locales = [".".join(entry.split(".")[:-1]) for entry in files_locales]
for lc in valid_locales:
try:
this_dict = await json_read(
@@ -80,3 +76,38 @@ async def in_all_locales(key: str, *args: str) -> list:
continue
return output
async def in_every_locale(key: str, *args: str) -> Dict[str, Any]:
"""Get value of the provided key and path in every available locale with locale tag
### Args:
* key (`str`): The last key of the locale's keys path.
* *args (`list`): Path to key like: `dict[args][key]`.
### Returns:
* `Dict[str, Any]`: Locale is a key and it's value from locale file is a value
"""
output = {}
files_locales = listdir(await config_get("locale", "locations"))
valid_locales = [".".join(entry.split(".")[:-1]) for entry in files_locales]
for lc in valid_locales:
try:
this_dict = await json_read(
Path(f'{await config_get("locale", "locations")}/{lc}.json')
)
except FileNotFoundError:
continue
this_key = this_dict
for dict_key in args:
this_key = this_key[dict_key]
try:
output[lc] = this_key[key]
except KeyError:
continue
return output

View File

@@ -0,0 +1,112 @@
from os import listdir
from pathlib import Path
from typing import Any, Dict
from libbot import sync
from libbot.sync import config_get, json_read
def _(key: str, *args: str, locale: str = sync.config_get("locale")) -> Any:
"""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. Is usually `str`, `dict` or `list`
"""
if locale is None:
locale = sync.config_get("locale")
try:
this_dict = json_read(
Path(f'{config_get("locale", "locations")}/{locale}.json')
)
except FileNotFoundError:
try:
this_dict = json_read(
Path(f'{config_get("locale", "locations")}/{config_get("locale")}.json')
)
except FileNotFoundError:
return f'⚠️ Locale in config is invalid: could not get "{key}" in {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 {args} from locale "{locale}"'
def in_all_locales(key: str, *args: str) -> list:
"""Get value of the provided key and path in all available locales
### Args:
* key (`str`): The last key of the locale's keys path.
* *args (`list`): Path to key like: `dict[args][key]`.
### Returns:
* `list`: List of values in all locales
"""
output = []
files_locales = listdir(config_get("locale", "locations"))
valid_locales = [".".join(entry.split(".")[:-1]) for entry in files_locales]
for lc in valid_locales:
try:
this_dict = json_read(
Path(f'{config_get("locale", "locations")}/{lc}.json')
)
except FileNotFoundError:
continue
this_key = this_dict
for dict_key in args:
this_key = this_key[dict_key]
try:
output.append(this_key[key])
except KeyError:
continue
return output
def in_every_locale(key: str, *args: str) -> Dict[str, Any]:
"""Get value of the provided key and path in every available locale with locale tag
### Args:
* key (`str`): The last key of the locale's keys path.
* *args (`list`): Path to key like: `dict[args][key]`.
### Returns:
* `Dict[str, Any]`: Locale is a key and it's value from locale file is a value
"""
output = {}
files_locales = listdir(config_get("locale", "locations"))
valid_locales = [".".join(entry.split(".")[:-1]) for entry in files_locales]
for lc in valid_locales:
try:
this_dict = json_read(
Path(f'{config_get("locale", "locations")}/{lc}.json')
)
except FileNotFoundError:
continue
this_key = this_dict
for dict_key in args:
this_key = this_key[dict_key]
try:
output[lc] = this_key[key]
except KeyError:
continue
return output

View File

@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "libbot"
version = "0.4"
version = "0.7"
authors = [{ name = "Profitroll" }]
description = "Universal bot library with functions needed for basic Discord/Telegram bot development."
readme = "README.md"
@@ -36,7 +36,7 @@ Documentation = "https://git.end-play.xyz/profitroll/LibBotUniversal/wiki"
Tracker = "https://git.end-play.xyz/profitroll/LibBotUniversal/issues"
[tool.setuptools]
packages = ["libbot", "libbot.i18n", "libbot.sync"]
packages = ["libbot", "libbot.i18n", "libbot.sync", "libbot.i18n.sync"]
[tool.setuptools_scm]