Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
21ce78f475 | |||
753255e649 | |||
b40d1fe911 | |||
ce0503500a | |||
|
dc760a8b67 | ||
|
0f39f6158e | ||
|
07faca0fe0 |
@@ -1,5 +1,5 @@
|
||||
__name__ = "libbot"
|
||||
__version__ = "0.2"
|
||||
__version__ = "0.5"
|
||||
__license__ = "GPL3"
|
||||
__author__ = "Profitroll"
|
||||
|
||||
|
@@ -2,7 +2,11 @@ from pathlib import Path
|
||||
from typing import Any, Union
|
||||
|
||||
import aiofiles
|
||||
|
||||
try:
|
||||
from ujson import dumps, loads
|
||||
except ImportError:
|
||||
from json import dumps, loads
|
||||
|
||||
from libbot.sync import nested_set
|
||||
|
||||
|
@@ -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(
|
||||
|
77
libbot/i18n/sync/__init__.py
Normal file
77
libbot/i18n/sync/__init__.py
Normal file
@@ -0,0 +1,77 @@
|
||||
from os import listdir
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
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
|
@@ -1,7 +1,10 @@
|
||||
from pathlib import Path
|
||||
from typing import Any, Union
|
||||
|
||||
try:
|
||||
from ujson import dumps, loads
|
||||
except ImportError:
|
||||
from json import dumps, loads
|
||||
|
||||
|
||||
def json_read(path: Union[str, Path]) -> Any:
|
||||
|
47
pyproject.toml
Normal file
47
pyproject.toml
Normal file
@@ -0,0 +1,47 @@
|
||||
[build-system]
|
||||
requires = ["setuptools>=62.6,<66"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "libbot"
|
||||
version = "0.5"
|
||||
authors = [{ name = "Profitroll" }]
|
||||
description = "Universal bot library with functions needed for basic Discord/Telegram bot development."
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.7"
|
||||
license = { text = "GPL3" }
|
||||
classifiers = [
|
||||
"Development Status :: 3 - Alpha",
|
||||
"Intended Audience :: Developers",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Operating System :: OS Independent",
|
||||
"Programming Language :: Python :: 3.7",
|
||||
"Programming Language :: Python :: 3.8",
|
||||
"Programming Language :: Python :: 3.9",
|
||||
"Programming Language :: Python :: 3.10",
|
||||
"Programming Language :: Python :: 3.11",
|
||||
"Topic :: Software Development :: Libraries :: Python Modules",
|
||||
"Topic :: Utilities",
|
||||
]
|
||||
dependencies = ["aiofiles~=23.1.0"]
|
||||
|
||||
[project.optional-dependencies]
|
||||
pycord = ["py-cord>=2.0.0"]
|
||||
pyrogram = ["pyrogram>=2.0.0"]
|
||||
speed = ["ujson==5.7.0"]
|
||||
|
||||
[project.urls]
|
||||
Source = "https://git.end-play.xyz/profitroll/LibBotUniversal"
|
||||
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"]
|
||||
|
||||
[tool.setuptools_scm]
|
||||
|
||||
[tool.black]
|
||||
target-version = ['py37', 'py38', 'py39', 'py310', 'py311']
|
||||
|
||||
[tool.isort]
|
||||
profile = "black"
|
32
setup.py
32
setup.py
@@ -1,32 +1,4 @@
|
||||
from setuptools import setup
|
||||
|
||||
setup(
|
||||
name="libbot",
|
||||
version="0.2",
|
||||
author="Profitroll",
|
||||
description="Universal bot library with functions needed for basic Discord/Telegram bot development.",
|
||||
long_description="Universal bot library with functions needed for basic Discord/Telegram bot development.",
|
||||
long_description_content_type="text/markdown",
|
||||
author_email="profitroll@end-play.xyz",
|
||||
url="https://git.end-play.xyz/profitroll/LibBotUniversal",
|
||||
project_urls={
|
||||
"Bug Tracker": "https://git.end-play.xyz/profitroll/LibBotUniversal/issues",
|
||||
"Documentation": "https://git.end-play.xyz/profitroll/LibBotUniversal/wiki",
|
||||
"Source Code": "https://git.end-play.xyz/profitroll/LibBotUniversal.git",
|
||||
},
|
||||
packages=[
|
||||
"libbot",
|
||||
"libbot.i18n",
|
||||
"libbot.sync",
|
||||
],
|
||||
install_requires=["aiofiles~=23.1.0", "ujson==5.7.0"],
|
||||
classifiers=[
|
||||
"Development Status :: 3 - Alpha",
|
||||
"Intended Audience :: Developers",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Operating System :: OS Independent",
|
||||
"Programming Language :: Python :: 3",
|
||||
"Topic :: Software Development :: Libraries :: Python Modules",
|
||||
"Topic :: Utilities",
|
||||
],
|
||||
)
|
||||
if __name__ == "__main__":
|
||||
setup()
|
||||
|
Reference in New Issue
Block a user