Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
bba0aca25a
|
|||
0d93e08397
|
|||
b21f7044eb
|
|||
0cdd887e63 | |||
8052d57a40 | |||
ab3d714727 | |||
a843f77290 | |||
21ce78f475 | |||
753255e649 | |||
b40d1fe911 | |||
ce0503500a |
@@ -1,5 +1,5 @@
|
|||||||
__name__ = "libbot"
|
__name__ = "libbot"
|
||||||
__version__ = "0.4"
|
__version__ = "0.9"
|
||||||
__license__ = "GPL3"
|
__license__ = "GPL3"
|
||||||
__author__ = "Profitroll"
|
__author__ = "Profitroll"
|
||||||
|
|
||||||
|
@@ -1,8 +1,9 @@
|
|||||||
from os import listdir
|
from os import listdir
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any, Dict
|
||||||
|
|
||||||
from libbot import config_get, json_read, sync
|
from libbot import config_get, json_read, sync
|
||||||
|
from libbot.i18n.classes.bot_locale import BotLocale
|
||||||
|
|
||||||
|
|
||||||
async def _(key: str, *args: str, locale: str = sync.config_get("locale")) -> Any:
|
async def _(key: str, *args: str, locale: str = sync.config_get("locale")) -> Any:
|
||||||
@@ -31,7 +32,7 @@ async def _(key: str, *args: str, locale: str = sync.config_get("locale")) -> An
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
except FileNotFoundError:
|
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
|
this_key = this_dict
|
||||||
for dict_key in args:
|
for dict_key in args:
|
||||||
@@ -40,7 +41,7 @@ async def _(key: str, *args: str, locale: str = sync.config_get("locale")) -> An
|
|||||||
try:
|
try:
|
||||||
return this_key[key]
|
return this_key[key]
|
||||||
except KeyError:
|
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:
|
async def in_all_locales(key: str, *args: str) -> list:
|
||||||
@@ -55,13 +56,9 @@ async def in_all_locales(key: str, *args: str) -> list:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
output = []
|
output = []
|
||||||
valid_locales = []
|
|
||||||
|
|
||||||
files_locales = listdir(await config_get("locale", "locations"))
|
files_locales = listdir(await config_get("locale", "locations"))
|
||||||
|
|
||||||
for entry in files_locales:
|
valid_locales = [".".join(entry.split(".")[:-1]) for entry in files_locales]
|
||||||
valid_locales.append(".".join(entry.split(".")[:-1]))
|
|
||||||
|
|
||||||
for lc in valid_locales:
|
for lc in valid_locales:
|
||||||
try:
|
try:
|
||||||
this_dict = await json_read(
|
this_dict = await json_read(
|
||||||
@@ -80,3 +77,38 @@ async def in_all_locales(key: str, *args: str) -> list:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
return output
|
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
|
||||||
|
121
libbot/i18n/classes/bot_locale.py
Normal file
121
libbot/i18n/classes/bot_locale.py
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
from os import listdir
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any, Dict, Union
|
||||||
|
|
||||||
|
from libbot import sync
|
||||||
|
|
||||||
|
|
||||||
|
class BotLocale:
|
||||||
|
"""Small addon that can be used by bot clients' classes in order to minimize I/O"""
|
||||||
|
|
||||||
|
def __init__(self, locales_folder: Union[str, Path, None] = None) -> None:
|
||||||
|
if locales_folder is None:
|
||||||
|
locales_folder = Path(sync.config_get("locale", "locations"))
|
||||||
|
elif isinstance(locales_folder, str):
|
||||||
|
locales_folder = Path(locales_folder)
|
||||||
|
elif not isinstance(locales_folder, Path):
|
||||||
|
raise TypeError("'locales_folder' must be a valid path or path-like object")
|
||||||
|
|
||||||
|
files_locales: list = listdir(locales_folder)
|
||||||
|
|
||||||
|
valid_locales: list = [
|
||||||
|
".".join(entry.split(".")[:-1]) for entry in files_locales
|
||||||
|
]
|
||||||
|
|
||||||
|
self.default: str = sync.config_get("locale")
|
||||||
|
self.locales: dict = {}
|
||||||
|
|
||||||
|
for lc in valid_locales:
|
||||||
|
self.locales[lc] = sync.json_read(Path(f"{locales_folder}/{lc}.json"))
|
||||||
|
|
||||||
|
def _(self, key: str, *args: str, locale: Union[str, None] = None) -> 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 (`Union[str, None]`, *optional*): 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 = self.default
|
||||||
|
|
||||||
|
try:
|
||||||
|
this_dict = self.locales[locale]
|
||||||
|
except KeyError:
|
||||||
|
try:
|
||||||
|
this_dict = self.locales[self.default]
|
||||||
|
except KeyError:
|
||||||
|
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(self, 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 = []
|
||||||
|
|
||||||
|
for name, lc in self.locales.items():
|
||||||
|
try:
|
||||||
|
this_dict = lc
|
||||||
|
except KeyError:
|
||||||
|
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(self, 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 = {}
|
||||||
|
|
||||||
|
for name, lc in self.locales.items():
|
||||||
|
try:
|
||||||
|
this_dict = lc
|
||||||
|
except KeyError:
|
||||||
|
continue
|
||||||
|
|
||||||
|
this_key = this_dict
|
||||||
|
for dict_key in args:
|
||||||
|
this_key = this_key[dict_key]
|
||||||
|
|
||||||
|
try:
|
||||||
|
output[name] = this_key[key]
|
||||||
|
except KeyError:
|
||||||
|
continue
|
||||||
|
|
||||||
|
return output
|
112
libbot/i18n/sync/__init__.py
Normal file
112
libbot/i18n/sync/__init__.py
Normal 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
|
@@ -4,11 +4,11 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "libbot"
|
name = "libbot"
|
||||||
version = "0.4"
|
version = "0.9"
|
||||||
authors = [{ name = "Profitroll" }]
|
authors = [{ name = "Profitroll" }]
|
||||||
description = "Universal bot library with functions needed for basic Discord/Telegram bot development."
|
description = "Universal bot library with functions needed for basic Discord/Telegram bot development."
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.7"
|
requires-python = ">=3.8"
|
||||||
license = { text = "GPL3" }
|
license = { text = "GPL3" }
|
||||||
classifiers = [
|
classifiers = [
|
||||||
"Development Status :: 3 - Alpha",
|
"Development Status :: 3 - Alpha",
|
||||||
@@ -28,7 +28,7 @@ dependencies = ["aiofiles~=23.1.0"]
|
|||||||
[project.optional-dependencies]
|
[project.optional-dependencies]
|
||||||
pycord = ["py-cord>=2.0.0"]
|
pycord = ["py-cord>=2.0.0"]
|
||||||
pyrogram = ["pyrogram>=2.0.0"]
|
pyrogram = ["pyrogram>=2.0.0"]
|
||||||
speed = ["ujson==5.7.0"]
|
speed = ["ujson~=5.8.0"]
|
||||||
|
|
||||||
[project.urls]
|
[project.urls]
|
||||||
Source = "https://git.end-play.xyz/profitroll/LibBotUniversal"
|
Source = "https://git.end-play.xyz/profitroll/LibBotUniversal"
|
||||||
@@ -36,7 +36,7 @@ Documentation = "https://git.end-play.xyz/profitroll/LibBotUniversal/wiki"
|
|||||||
Tracker = "https://git.end-play.xyz/profitroll/LibBotUniversal/issues"
|
Tracker = "https://git.end-play.xyz/profitroll/LibBotUniversal/issues"
|
||||||
|
|
||||||
[tool.setuptools]
|
[tool.setuptools]
|
||||||
packages = ["libbot", "libbot.i18n", "libbot.sync"]
|
packages = ["libbot", "libbot.i18n", "libbot.sync", "libbot.i18n.sync"]
|
||||||
|
|
||||||
[tool.setuptools_scm]
|
[tool.setuptools_scm]
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user