From b9332f685d8b08a9409b90f094c47f9261f678a4 Mon Sep 17 00:00:00 2001 From: infeeeee Date: Sun, 31 Dec 2023 20:10:47 +0100 Subject: [PATCH] AppSettings Singleton --- IoTuring/Configurator/Configurator.py | 2 ++ IoTuring/Configurator/ConfiguratorLoader.py | 11 ------- IoTuring/Entity/Entity.py | 2 +- IoTuring/Logger/Logger.py | 2 +- IoTuring/Logger/consts.py | 1 + IoTuring/MyApp/AppSettings.py | 35 +++++++++++++-------- IoTuring/Warehouse/Warehouse.py | 2 +- IoTuring/__init__.py | 8 +++-- 8 files changed, 34 insertions(+), 29 deletions(-) diff --git a/IoTuring/Configurator/Configurator.py b/IoTuring/Configurator/Configurator.py index 681468458..60eed0b53 100644 --- a/IoTuring/Configurator/Configurator.py +++ b/IoTuring/Configurator/Configurator.py @@ -228,6 +228,8 @@ def Quit(self) -> None: def WriteConfigurations(self) -> None: """ Save to configurations file """ self.configuratorIO.writeConfigurations(self.config.ToDict()) + # Reload AppSettings + AppSettings().LoadConfiguration(self) def ManageSingleWarehouse(self, whClass): """UI for single Warehouse settings""" diff --git a/IoTuring/Configurator/ConfiguratorLoader.py b/IoTuring/Configurator/ConfiguratorLoader.py index 91b12c737..eee7e070b 100644 --- a/IoTuring/Configurator/ConfiguratorLoader.py +++ b/IoTuring/Configurator/ConfiguratorLoader.py @@ -5,7 +5,6 @@ from IoTuring.ClassManager.WarehouseClassManager import WarehouseClassManager from IoTuring.ClassManager.EntityClassManager import EntityClassManager from IoTuring.Warehouse.Warehouse import Warehouse -from IoTuring.MyApp.AppSettings import AppSettings class ConfiguratorLoader(LogObject): @@ -63,14 +62,4 @@ def LoadEntities(self) -> list[Entity]: # - pass the configuration to the warehouse function that uses the configuration to init the Warehouse # - append the Warehouse to the list - - def LoadAppSettings(self) -> None: - """ Load app settings from config and defafults to AppSettings.Settings class attribute """ - - - appSettings = AppSettings(self.configurations.GetAppSettings()) - appSettings.AddMissingDefaultConfigs() - - # Add configs to class: - AppSettings.Settings = appSettings.GetConfigurations() \ No newline at end of file diff --git a/IoTuring/Entity/Entity.py b/IoTuring/Entity/Entity.py index 2b99352d0..4f9ec9637 100644 --- a/IoTuring/Entity/Entity.py +++ b/IoTuring/Entity/Entity.py @@ -31,7 +31,7 @@ def __init__(self, single_configuration: SingleConfiguration) -> None: # When I update the values this number changes (randomly) so each warehouse knows I have updated self.valuesID = 0 - self.updateTimeout = float(AppSettings.Settings[CONFIG_KEY_UPDATE_INTERVAL]) + self.updateTimeout = float(AppSettings().GetFromConfigurations(CONFIG_KEY_UPDATE_INTERVAL)) def Initialize(self): diff --git a/IoTuring/Logger/Logger.py b/IoTuring/Logger/Logger.py index c6a07bc45..b8fe20b64 100644 --- a/IoTuring/Logger/Logger.py +++ b/IoTuring/Logger/Logger.py @@ -18,7 +18,7 @@ class Singleton(type): _instances = {} - def __call__(cls): + def __call__(cls): # type: ignore if cls not in cls._instances: cls._instances[cls] = super(Singleton, cls).__call__() return cls._instances[cls] diff --git a/IoTuring/Logger/consts.py b/IoTuring/Logger/consts.py index 8678c2e5a..eeb2f5444 100644 --- a/IoTuring/Logger/consts.py +++ b/IoTuring/Logger/consts.py @@ -56,6 +56,7 @@ # before those spaces I add this string LONG_MESSAGE_PRESTRING_CHAR = ' ' +DEFAULT_LOG_LEVEL = "LOG_INFO" CONSOLE_LOG_LEVEL = "LOG_INFO" FILE_LOG_LEVEL = "LOG_INFO" diff --git a/IoTuring/MyApp/AppSettings.py b/IoTuring/MyApp/AppSettings.py index cc3f3b74c..ae3794f8a 100644 --- a/IoTuring/MyApp/AppSettings.py +++ b/IoTuring/MyApp/AppSettings.py @@ -1,28 +1,34 @@ +from typing import TYPE_CHECKING +if TYPE_CHECKING: + from IoTuring.Configurator.Configurator import Configurator + from IoTuring.Configurator.ConfiguratorObject import ConfiguratorObject from IoTuring.Configurator.MenuPreset import MenuPreset +from IoTuring.Logger.Logger import Singleton -from IoTuring.Logger import consts +from IoTuring.Logger.consts import LOG_LEVELS, DEFAULT_LOG_LEVEL CONFIG_KEY_CONSOLE_LOG_LEVEL = "console_log_level" -CONFIG_KEY_FILE_LOG_LEVEL = "console_file_level" +CONFIG_KEY_FILE_LOG_LEVEL = "file_log_level" +CONFIG_KEY_FILE_LOG_ENABLED = "file_log_enabled" CONFIG_KEY_UPDATE_INTERVAL = "update_interval" CONFIG_KEY_SLOW_INTERVAL = "slow_interval" -DEFAULT_LOG_LEVEL = "LOG_INFO" LogLevelChoices = [{"name": l["string"], "value": l["const"]} - for l in consts.LOG_LEVELS] -# LogLevelChoices = [l["const"] for l in consts.LOG_LEVELS] + for l in LOG_LEVELS] + +class AppSettings(ConfiguratorObject, metaclass=Singleton): -class AppSettings(ConfiguratorObject): - # Default log levels, so Logging can start before configuration is loaded - Settings = { - CONFIG_KEY_CONSOLE_LOG_LEVEL: DEFAULT_LOG_LEVEL, - CONFIG_KEY_FILE_LOG_LEVEL: DEFAULT_LOG_LEVEL - } + def __init__(self) -> None: + pass + + def LoadConfiguration(self, configurator: "Configurator"): + self.configurations = configurator.config.GetAppSettings() + self.AddMissingDefaultConfigs() @classmethod def ConfigurationPreset(cls): @@ -33,16 +39,19 @@ def ConfigurationPreset(cls): instruction="IOTURING_LOG_LEVEL envvar overwrites this setting!", choices=LogLevelChoices) + preset.AddEntry(name="Enable file logging", key=CONFIG_KEY_FILE_LOG_ENABLED, + question_type="yesno", mandatory=True, default="Y") + preset.AddEntry(name="File log level", key=CONFIG_KEY_FILE_LOG_LEVEL, question_type="select", mandatory=True, default=DEFAULT_LOG_LEVEL, choices=LogLevelChoices) preset.AddEntry(name="Main update interval in seconds", key=CONFIG_KEY_UPDATE_INTERVAL, mandatory=True, - question_type="text", default="10") + question_type="integer", default=10) preset.AddEntry(name="Secondary update interval in minutes", key=CONFIG_KEY_SLOW_INTERVAL, mandatory=True, - question_type="text", default="10") + question_type="integer", default=10) return preset diff --git a/IoTuring/Warehouse/Warehouse.py b/IoTuring/Warehouse/Warehouse.py index d56ac7001..9ef6fb78c 100644 --- a/IoTuring/Warehouse/Warehouse.py +++ b/IoTuring/Warehouse/Warehouse.py @@ -15,7 +15,7 @@ class Warehouse(LogObject, ConfiguratorObject): CATEGORY_NAME = CONFIG_CATEGORY_NAME[KEY_ACTIVE_WAREHOUSES] def __init__(self, single_configuration: SingleConfiguration) -> None: - self.loopTimeout = float(AppSettings.Settings[CONFIG_KEY_UPDATE_INTERVAL]) + self.loopTimeout = float(AppSettings().GetFromConfigurations(CONFIG_KEY_UPDATE_INTERVAL)) self.configurations = single_configuration diff --git a/IoTuring/__init__.py b/IoTuring/__init__.py index 9d2ae010c..de5504c85 100644 --- a/IoTuring/__init__.py +++ b/IoTuring/__init__.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 from IoTuring.MyApp.App import App +from IoTuring.MyApp.AppSettings import AppSettings from IoTuring.Configurator.Configurator import Configurator from IoTuring.Configurator.ConfiguratorLoader import ConfiguratorLoader from IoTuring.Entity.EntityManager import EntityManager @@ -51,6 +52,10 @@ def loop(): logger = Logger() configurator = Configurator() + # Load AppSettings: + AppSettings().LoadConfiguration(configurator) + + logger.Log(Logger.LOG_DEBUG, "App", f"Selected options: {vars(args)}") if args.configurator: @@ -71,8 +76,7 @@ def loop(): # This have to start after configurator.Menu(), otherwise won't work starting from the menu signal.signal(signal.SIGINT, Exit_SIGINT_handler) - # Load AppSettings: - ConfiguratorLoader(configurator).LoadAppSettings() + logger.Log(Logger.LOG_INFO, "App", App()) # Print App info logger.Log(Logger.LOG_INFO, "Configurator",