diff --git a/src/krux/settings.py b/src/krux/settings.py index 4d8a4765..580d8bce 100644 --- a/src/krux/settings.py +++ b/src/krux/settings.py @@ -95,6 +95,15 @@ def __init__(self, attr, default_value, categories): super().__init__(attr, default_value) self.categories = categories + def __get__(self, obj, objtype=None): + if obj is None: + return self + + stored_val = store.get(obj.namespace, self.attr, self.default_value) + if stored_val not in self.categories: + return self.default_value + return stored_val + class NumberSetting(Setting): """Setting that can be a number within a defined range""" @@ -104,6 +113,19 @@ def __init__(self, numtype, attr, default_value, value_range): self.numtype = numtype self.value_range = value_range + def __get__(self, obj, objtype=None): + if obj is None: + return self + + stored_val = store.get(obj.namespace, self.attr, self.default_value) + if ( + (self.numtype is int and isinstance(stored_val, self.numtype)) + or (self.numtype is float and isinstance(stored_val, (self.numtype, int))) + ) and self.value_range[0] <= stored_val <= self.value_range[1]: + return stored_val + + return self.default_value + class Store: """Acts as a simple JSON file store for settings, falling back to an in-memory dict if no SD""" diff --git a/tests/test_settings.py b/tests/test_settings.py index 48fab248..524ffb71 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -15,6 +15,122 @@ def test_init(mocker, m5stickv): sn.label("test") +def test_stored_i18n_settings(mocker, m5stickv): + # mock store singleton creation before import + stored_settings = """{"settings": {"i18n": {"locale": "pl-PL"}}}""" + mocker.patch("builtins.open", mocker.mock_open(read_data=stored_settings)) + + # import will create store singleton with mocked values + from krux.krux_settings import I18nSettings + + i18n = I18nSettings() + + assert i18n.locale == "pl-PL" + + +def test_wrong_stored_i18n_settings(mocker, m5stickv): + # mock store singleton creation before import + stored_settings = """{"settings": {"i18n": {"locale": "aa-AA"}}}""" + mocker.patch("builtins.open", mocker.mock_open(read_data=stored_settings)) + + # import will create store singleton with mocked values + from krux.krux_settings import I18nSettings + + i18n = I18nSettings() + + assert i18n.locale == "en-US" + + +def test_stored_adafruit_printer_settings(mocker, m5stickv): + print("") + + # mock store singleton creation before import + stored_settings = ( + """{"settings": {"printer": {"thermal": {"adafruit": {"line_delay": 35}}}}}""" + ) + mocker.patch("builtins.open", mocker.mock_open(read_data=stored_settings)) + + # import will create store singleton with mocked values + from krux.krux_settings import AdafruitPrinterSettings + + ada = AdafruitPrinterSettings() + assert ada.line_delay == 35 + + +def test_wrong_stored_adafruit_printer_settings(mocker, m5stickv): + print("") + + # mock store singleton creation before import + stored_settings = ( + """{"settings": {"printer": {"thermal": {"adafruit": {"line_delay": 511}}}}}""" + ) + mocker.patch("builtins.open", mocker.mock_open(read_data=stored_settings)) + + # import will create store singleton with mocked values + from krux.krux_settings import AdafruitPrinterSettings + + ada = AdafruitPrinterSettings() + assert ada.line_delay == 20 + + +def test_float_stored_adafruit_printer_settings(mocker, m5stickv): + print("") + + # mock store singleton creation before import + stored_settings = ( + """{"settings": {"printer": {"thermal": {"adafruit": {"line_delay": 21.2}}}}}""" + ) + mocker.patch("builtins.open", mocker.mock_open(read_data=stored_settings)) + + # import will create store singleton with mocked values + from krux.krux_settings import AdafruitPrinterSettings + + ada = AdafruitPrinterSettings() + assert ada.line_delay == 20 + + +def test_string_stored_adafruit_printer_settings(mocker, m5stickv): + print("") + + # mock store singleton creation before import + stored_settings = """{"settings": {"printer": {"thermal": {"adafruit": {"line_delay": 1,abc}}}}}""" + mocker.patch("builtins.open", mocker.mock_open(read_data=stored_settings)) + + # import will create store singleton with mocked values + from krux.krux_settings import AdafruitPrinterSettings + + ada = AdafruitPrinterSettings() + assert ada.line_delay == 20 + + +def test_stored_cnc_settings(mocker, m5stickv): + print("") + + # mock store singleton creation before import + stored_settings = """{"settings": {"printer": {"cnc": {"border_padding": 127}}}}""" + mocker.patch("builtins.open", mocker.mock_open(read_data=stored_settings)) + + # import will create store singleton with mocked values + from krux.krux_settings import CNCSettings + + cnc = CNCSettings() + assert cnc.border_padding == 127 + + +def test_wrong_stored_cnc_settings(mocker, m5stickv): + print("") + + # mock store singleton creation before import + stored_settings = """{"settings": {"printer": {"thermal": {"adafruit": {"line_delay": 1.abc}}}}}""" + mocker.patch("builtins.open", mocker.mock_open(read_data=stored_settings)) + + # import will create store singleton with mocked values + from krux.krux_settings import CNCSettings + + cnc = CNCSettings() + assert cnc.border_padding == 0.0625 + + def test_store_init(mocker, m5stickv): from krux.settings import Store, SETTINGS_FILENAME, SD_PATH