Skip to content

Commit

Permalink
small changes to krux_settings, black + lint
Browse files Browse the repository at this point in the history
  • Loading branch information
tadeubas committed Sep 3, 2024
1 parent 12212d2 commit 129fc19
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
9 changes: 4 additions & 5 deletions src/boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,20 @@ def login(ctx_login):

login_start_from = None
while True:
login = krux.pages.login.Login(ctx_login)
if not login.run(login_start_from):
login_page = krux.pages.login.Login(ctx_login)
if not login_page.run(login_start_from):
break

if ctx_login.wallet is not None:
# Have a loaded wallet
del login
del login_page
break

# Login closed due to change of locale at Settings
login_start_from = (
krux.pages.login.Login.SETTINGS_MENU_INDEX
) # will start Login again from Settings index
del login

del login_page

# Unimport login
del krux.pages.login
Expand Down
28 changes: 20 additions & 8 deletions src/krux/krux_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
MAIN_TXT,
TEST_TXT,
)
from .translations import translation_index
from .key import SCRIPT_LONG_NAMES
from krux.translations import translation_index

BAUDRATES = [1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200]

Expand All @@ -54,16 +54,21 @@
THERMAL_ADAFRUIT_TXT = "thermal/adafruit"

TRANSLATION_LOOKUP_TABLE = "translation_dict"
DEFAULT_LOCALE = "en-US"


def translations(locale):
def _get_locale_module(locale):
"""Get the name of the module based on the locale"""
return "krux.translations_" + locale.replace("-", "_")


def _translations(locale):
"""Returns the translations map for the given locale"""
if locale == "en-US":
if locale == DEFAULT_LOCALE:
return None

locale = locale.replace("-", "_")
translations_module = __import__(
"krux.translations_" + locale, None, None, TRANSLATION_LOOKUP_TABLE
_get_locale_module(locale), None, None, TRANSLATION_LOOKUP_TABLE
)

return getattr(translations_module, TRANSLATION_LOOKUP_TABLE)
Expand All @@ -72,7 +77,7 @@ def translations(locale):
def t(slug):
"""Translates a slug according to the current locale"""
slug_id = binascii.crc32(slug.encode("utf-8"))
lookup = translations(Settings().i18n.locale)
lookup = _translations(Settings().i18n.locale)
if not lookup or slug_id not in lookup:
return slug
return lookup[slug_id]
Expand Down Expand Up @@ -101,9 +106,8 @@ class I18nSettings(SettingsNamespace):
"""I18n-specific settings"""

namespace = "settings.i18n"
DEFAULT_LOCALE = "en-US"
locale = CategorySetting(
"locale", DEFAULT_LOCALE, translation_index + [DEFAULT_LOCALE]
"locale", DEFAULT_LOCALE, list(translation_index) + [DEFAULT_LOCALE]
)

def label(self, attr):
Expand Down Expand Up @@ -406,6 +410,14 @@ class Settings(SettingsNamespace):

namespace = "settings"

# Make sure only one instance is created (Singleton)
_instance = None

def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance

def __init__(self):
self.wallet = DefaultWallet()
self.security = SecuritySettings()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def tdata(mocker):

def test_translations(mocker, m5stickv, tdata):
import binascii
from krux.krux_settings import translations
from krux.krux_settings import _translations

cases = [
(tdata[0], None),
Expand All @@ -22,7 +22,7 @@ def test_translations(mocker, m5stickv, tdata):
for case in cases:
mocker.patch("krux.krux_settings.translation_index", list(case[0].keys())[0])
mocker.patch("krux.translations_es_MX.translation_dict", case[1])
lookup = translations(list(case[0].keys())[0])
lookup = _translations(list(case[0].keys())[0])

assert lookup == case[1]

Expand Down

0 comments on commit 129fc19

Please sign in to comment.