Skip to content

Commit

Permalink
load only required locales (#18)
Browse files Browse the repository at this point in the history
* instead of downloading all translation tables, required locales are determined from the user list settings and the user locale.
* variable userlistsettings renamed to user_list_settings (snake_case)
  • Loading branch information
tr4nt0r authored Feb 22, 2024
1 parent 08af022 commit be4e030
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 0.4.1

* instead of downloading all translation tables, required locales are determined from the user list settings and the user locale. ([tr4nt0r](https://github.com/tr4nt0r))
* variable `userlistsettings` renamed to snake_cae `user_list_settings`. ([tr4nt0r](https://github.com/tr4nt0r))

## 0.4.0

* **Localization support:** catalog items are now automatically translated based on the shopping lists language if configured, otherwise the users default language is used ([tr4nt0r](https://github.com/tr4nt0r))
Expand Down
61 changes: 45 additions & 16 deletions bring_api/bring.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __init__(
self.password = password

self.public_uuid = ""
self.userlistsettings: dict[str, dict[str, str]] = {}
self.user_list_settings: dict[str, dict[str, str]] = {}
self.user_locale = BRING_DEFAULT_LOCALE

self.__translations: dict[str, dict[str, str]] = {}
Expand Down Expand Up @@ -152,11 +152,9 @@ async def login(self) -> BringAuthResponse:
self.headers["X-BRING-COUNTRY"] = locale["country"]
self.user_locale = f'{locale["language"]}-{locale["country"]}'

if len(self.__translations) == 0:
await self.__load_article_translations()
self.user_list_settings = await self.__load_user_list_settings()

if len(self.userlistsettings) == 0:
await self.__load_user_list_settings()
self.__translations = await self.__load_article_translations()

return data

Expand Down Expand Up @@ -740,25 +738,48 @@ async def does_user_exist(self, mail: Optional[str] = None) -> bool:

return True

async def __load_article_translations(self) -> None:
"""Load all translation dictionaries into memory.
async def __load_article_translations(self) -> dict[str, dict[str, str]]:
"""Load all required translation dictionaries into memory.
Parameters
----------
locales_required : list[str]
A list of locales to download.
Raises
------
BringRequestException
If the request fails.
Returns
-------
dict
dict of downloaded dictionaries
"""
dictionaries: dict[str, dict[str, str]] = {}

locales_required = list(
dict.fromkeys(
[
list_setting["listArticleLanguage"]
for list_setting in self.user_list_settings.values()
]
+ [self.user_locale]
)
)

for locale in BRING_SUPPORTED_LOCALES:
for locale in locales_required or BRING_SUPPORTED_LOCALES:
if locale == BRING_DEFAULT_LOCALE:
continue
try:
url = f"{LOCALES_BASE_URL}articles.{locale}.json"
async with self._session.get(url) as r:
_LOGGER.debug("Response from %s: %s", url, r.status)
r.raise_for_status()

try:
self.__translations[locale] = await r.json()
dictionaries[locale] = await r.json()
except JSONDecodeError as e:
_LOGGER.error(
"Exception: Cannot load articles.%s.json:\n%s",
Expand Down Expand Up @@ -791,6 +812,8 @@ async def __load_article_translations(self) -> None:
"failed due to request exception."
) from e

return dictionaries

def __translate(
self,
item_id: str,
Expand Down Expand Up @@ -850,17 +873,22 @@ def __translate(
"Translation failed due to error loading translation dictionary."
) from e

async def __load_user_list_settings(self) -> None:
async def __load_user_list_settings(self) -> dict[str, dict[str, str]]:
"""Load user list settings into memory.
Raises
------
BringTranslationException
If the userlistsettings coould not be loaded.
If the user list settings could not be loaded.
Returns
-------
dict[str, dict[str, str]]
A dict of settings of the users lists
"""
try:
self.userlistsettings = {
return {
user_list_setting["listUuid"]: {
user_setting["key"]: user_setting["value"]
for user_setting in user_list_setting["usersettings"]
Expand All @@ -869,13 +897,14 @@ async def __load_user_list_settings(self) -> None:
"userlistsettings"
]
}

except Exception as e:
_LOGGER.error(
"Exception: Cannot load userlistsettings:\n%s",
"Exception: Cannot load user list settings:\n%s",
traceback.format_exc(),
)
raise BringTranslationException(
"Translation failed due to error loading userlistsettings."
"Translation failed due to error loading user list settings."
) from e

async def get_all_user_settings(self) -> BringUserSettingsResponse:
Expand Down Expand Up @@ -992,8 +1021,8 @@ def __locale(self, list_uuid: str) -> str:
If list locale could not be determined from the userlistsettings or user.
"""
if list_uuid in self.userlistsettings:
return self.userlistsettings[list_uuid]["listArticleLanguage"]
if list_uuid in self.user_list_settings:
return self.user_list_settings[list_uuid]["listArticleLanguage"]
return self.user_locale

async def get_user_account(self) -> BringSyncCurrentUserResponse:
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = bring-api
version = 0.4.0
version = 0.4.1
author = Cyrill Raccaud
author_email = cyrill.raccaud+pypi@gmail.com
description = Unofficial package to access Bring! shopping lists API.
Expand Down
8 changes: 4 additions & 4 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async def test_translation(bring: Bring, lst: BringList):
locale_to = "de-DE"
locale_from = "de-CH"

locale_org = bring.userlistsettings[lst["listUuid"]]["listArticleLanguage"]
locale_org = bring.user_list_settings[lst["listUuid"]]["listArticleLanguage"]

test_items = {
"Pouletbrüstli": "Hähnchenbrust",
Expand All @@ -96,11 +96,11 @@ async def test_translation(bring: Bring, lst: BringList):
}
for k, v in test_items.items():
# Save an item an item to
bring.userlistsettings[lst["listUuid"]]["listArticleLanguage"] = locale_to
bring.user_list_settings[lst["listUuid"]]["listArticleLanguage"] = locale_to
await bring.save_item(lst["listUuid"], v)

# Get all the pending items of a list
bring.userlistsettings[lst["listUuid"]]["listArticleLanguage"] = locale_from
bring.user_list_settings[lst["listUuid"]]["listArticleLanguage"] = locale_from
items = await bring.get_list(lst["listUuid"])
item = next(ii["itemId"] for ii in items["purchase"] if ii["itemId"] == k)
assert item == k
Expand All @@ -109,7 +109,7 @@ async def test_translation(bring: Bring, lst: BringList):
await bring.remove_item(lst["listUuid"], k)

# reset locale to original value for other tests
bring.userlistsettings[lst["listUuid"]]["listArticleLanguage"] = locale_org
bring.user_list_settings[lst["listUuid"]]["listArticleLanguage"] = locale_org


async def main():
Expand Down

0 comments on commit be4e030

Please sign in to comment.