diff --git a/IoTuring/Configurator/Configuration.py b/IoTuring/Configurator/Configuration.py index 735b7fbe..483f9688 100644 --- a/IoTuring/Configurator/Configuration.py +++ b/IoTuring/Configurator/Configuration.py @@ -3,19 +3,19 @@ # config categories: KEY_ACTIVE_ENTITIES = "active_entities" KEY_ACTIVE_WAREHOUSES = "active_warehouses" -KEY_APP_SETTINGS = "app_settings" +KEY_SETTINGS = "settings" CONFIG_CATEGORY_NAME = { KEY_ACTIVE_ENTITIES: "Entity", KEY_ACTIVE_WAREHOUSES: "Warehouse", - KEY_APP_SETTINGS: "AppSetting" + KEY_SETTINGS: "Setting" } BLANK_CONFIGURATION = { KEY_ACTIVE_ENTITIES: [{"type": "AppInfo"}], KEY_ACTIVE_WAREHOUSES: [], - KEY_APP_SETTINGS: [] + KEY_SETTINGS: [] } KEY_ENTITY_TAG = "tag" @@ -38,14 +38,14 @@ def GetConfigsInCategory(self, config_category: str) -> list["SingleConfiguratio """Return all configurations in a category Args: - config_category (str): KEY_ACTIVE_ENTITIES, KEY_ACTIVE_WAREHOUSES or KEY_APP_SETTINGS + config_category (str): KEY_ACTIVE_ENTITIES, KEY_ACTIVE_WAREHOUSES or KEY_SETTINGS Returns: list: Configurations in the category. Empty list if none found. """ return [config for config in self.configs if config.config_category == config_category] - def GetConfigsOfType(self, config_type: str, config_category: str = "") -> list["SingleConfiguration"]: + def GetAllConfigsOfType(self, config_type: str, config_category: str = "") -> list["SingleConfiguration"]: """Return all configs with the given type, from the given category Args: @@ -62,6 +62,26 @@ def GetConfigsOfType(self, config_type: str, config_category: str = "") -> list[ return [config for config in config_list if config.GetType() == config_type] + def GetSingleConfigOfType(self, config_type: str) -> None | SingleConfiguration: + """Return the only configuration of the given type. Raises exception if multiple found. + + Args: + config_type (str): The type of config to return + + Raises: + Exception: Multiple config found + + Returns: + None | SingleConfiguration: The config, None if not found + """ + configs = self.GetAllConfigsOfType(config_type=config_type) + if not configs: + return None + if len(configs) > 1: + raise Exception("Multiple configs found!") + + return configs[0] + def RemoveActiveConfiguration(self, config: "SingleConfiguration") -> None: """Remove a configuration from the list of active configurations""" if config in self.configs: @@ -69,16 +89,19 @@ def RemoveActiveConfiguration(self, config: "SingleConfiguration") -> None: else: raise ValueError("Configuration not found") - def AddConfiguration(self, config_category: str, single_config_dict: dict, config_type: str = "") -> None: + def AddConfiguration(self, config_category: str, single_config_dict: dict, config_type: str = "") -> "SingleConfiguration": """Add a new configuration to the list of active configurations Args: - config_category (str): KEY_ACTIVE_ENTITIES or KEY_ACTIVE_WAREHOUSES + config_category (str): KEY_ACTIVE_ENTITIES, KEY_ACTIVE_WAREHOUSES or KEY_SETTINGS single_config_dict (dict): all settings as a dict config_type (str, optional): The type of the configuration, if not included in the dict. Raises: ValueError: Config type not defined in the dict nor in the function call + + Returns: + SingleConfiguration: The new single config """ if KEY_ENTITY_TYPE not in single_config_dict: @@ -87,21 +110,12 @@ def AddConfiguration(self, config_category: str, single_config_dict: dict, confi else: raise ValueError("Configuration type not specified") - self.configs.append(SingleConfiguration( - config_category, single_config_dict)) + single_config = SingleConfiguration( + config_category, single_config_dict) - def GetAppSettings(self) -> "SingleConfiguration": - """Find the AppSettings single configuration + self.configs.append(single_config) - Returns: - SingleConfiguration: The AppSettings as a SingleConfiguration - """ - if self.GetConfigsInCategory(KEY_APP_SETTINGS): - return self.GetConfigsInCategory(KEY_APP_SETTINGS)[0] - else: - appconfig = SingleConfiguration(KEY_APP_SETTINGS, {}) - self.configs.append(appconfig) - return appconfig + return single_config def ToDict(self) -> dict: """Full configuration as a dict, for saving to file """ @@ -119,39 +133,30 @@ class SingleConfiguration: config_category: str type: str - tag: str configurations: dict def __init__(self, config_category: str, config_dict: dict) -> None: """Create a new SingleConfiguration Args: - config_category (str): KEY_ACTIVE_ENTITIES, KEY_ACTIVE_WAREHOUSES or KEY_APP_SETTINGS + config_category (str): KEY_ACTIVE_ENTITIES, KEY_ACTIVE_WAREHOUSES or KEY_SETTINGS config_dict (dict): All options as in config file """ self.config_category = config_category - if KEY_ENTITY_TYPE in config_dict: - config_type = config_dict.pop(KEY_ENTITY_TYPE) - setattr(self, KEY_ENTITY_TYPE, config_type) - - if KEY_ENTITY_TAG in config_dict: - config_tag = config_dict.pop(KEY_ENTITY_TAG) - setattr(self, KEY_ENTITY_TAG, config_tag) + # self.type: + setattr(self, KEY_ENTITY_TYPE, config_dict.pop(KEY_ENTITY_TYPE)) self.configurations = config_dict def GetType(self) -> str: """ Get the type name of entity""" - if hasattr(self, KEY_ENTITY_TYPE): - return getattr(self, KEY_ENTITY_TYPE) - else: - return self.config_category + return getattr(self, KEY_ENTITY_TYPE) def GetTag(self) -> str: """ Get the tag of entity""" - if hasattr(self, KEY_ENTITY_TAG): - return getattr(self, KEY_ENTITY_TAG) + if KEY_ENTITY_TAG in self.configurations: + return self.configurations[KEY_ENTITY_TAG] else: return "" @@ -160,8 +165,8 @@ def GetLabel(self) -> str: label = self.GetType() - if hasattr(self, KEY_ENTITY_TAG): - label += f" with tag {getattr(self, KEY_ENTITY_TAG)}" + if self.GetTag(): + label += f" with tag {self.GetTag()}" return label @@ -221,9 +226,5 @@ def HasConfigKey(self, config_key: str) -> bool: def ToDict(self) -> dict: """Full configuration as a dict, as it would be saved to a file """ full_dict = self.configurations - if hasattr(self, KEY_ENTITY_TYPE): - full_dict[KEY_ENTITY_TYPE] = getattr(self, KEY_ENTITY_TYPE) - if hasattr(self, KEY_ENTITY_TAG): - full_dict[KEY_ENTITY_TAG] = getattr(self, KEY_ENTITY_TAG) - + full_dict[KEY_ENTITY_TYPE] = getattr(self, KEY_ENTITY_TYPE) return full_dict diff --git a/IoTuring/Configurator/Configurator.py b/IoTuring/Configurator/Configurator.py index 7babfe02..9c50e655 100644 --- a/IoTuring/Configurator/Configurator.py +++ b/IoTuring/Configurator/Configurator.py @@ -3,9 +3,10 @@ import shutil from IoTuring.Configurator.MenuPreset import QuestionPreset -from IoTuring.Configurator.Configuration import FullConfiguration, SingleConfiguration, KEY_ACTIVE_ENTITIES, KEY_ACTIVE_WAREHOUSES +from IoTuring.Configurator.Configuration import FullConfiguration, SingleConfiguration, KEY_ACTIVE_ENTITIES, KEY_ACTIVE_WAREHOUSES, KEY_SETTINGS, CONFIG_CATEGORY_NAME from IoTuring.Logger.LogObject import LogObject +from IoTuring.Logger.Logger import Logger from IoTuring.Exceptions.Exceptions import UserCancelledException from IoTuring.ClassManager.EntityClassManager import EntityClassManager @@ -92,7 +93,7 @@ def Menu(self) -> None: mainMenuChoices = [ {"name": "Manage entities", "value": self.ManageEntities}, {"name": "Manage warehouses", "value": self.ManageWarehouses}, - {"name": "App Settings", "value": self.ManageSettings}, + {"name": "Settings", "value": self.ManageSettings}, {"name": "Start IoTuring", "value": self.WriteConfigurations}, {"name": "Help", "value": self.DisplayHelp}, {"name": "Quit", "value": self.Quit}, @@ -138,7 +139,7 @@ def ManageEntities(self) -> None: elif choice == CHOICE_GO_BACK: self.Menu() else: - self.ManageSingleEntity(choice) + self.ManageSingleEntity(choice, ecm) def ManageWarehouses(self) -> None: """ UI for Warehouses settings """ @@ -149,9 +150,8 @@ def ManageWarehouses(self) -> None: availableWarehouses = wcm.ListAvailableClasses() for whClass in availableWarehouses: - enabled_sign = " " - if self.IsWarehouseActive(whClass): - enabled_sign = "X" + enabled_sign = "X" \ + if self.config.GetSingleConfigOfType(whClass.NAME) else " " manageWhChoices.append( {"name": f"[{enabled_sign}] - {whClass.NAME}", @@ -168,53 +168,32 @@ def ManageWarehouses(self) -> None: self.ManageSingleWarehouse(choice) def ManageSettings(self) -> None: - """ UI for changing AppSettings """ - preset = AppSettings.ConfigurationPreset() - - settingsChoices = [] - - for entry in preset.presets: - # Load config instead of default: - if self.config.GetAppSettings().HasConfigKey(entry.key): - value = self.config.GetAppSettings().GetConfigValue(entry.key) - else: - value = entry.default - - settingsChoices.append({ - "name": f"{entry.name}: {value}", - "value": entry.key - }) + """ UI for App and Log Settings """ + choices = [ + {"name": "Log Settings", "value": Logger}, + {"name": "App Settings", "value": AppSettings} + ] choice = self.DisplayMenu( - choices=settingsChoices, - message="Select setting to edit") + choices=choices, + message=f"Select settings to edit" + ) if choice == CHOICE_GO_BACK: self.Menu() - else: - q_preset = preset.GetPresetByKey(choice) - if q_preset: - self.ManageSingleSetting(q_preset) - else: - self.DisplayMessage(f"Question preset not found: {choice}") - self.ManageSettings() - - def ManageSingleSetting(self, q_preset: QuestionPreset): - """ UI for changing a single setting """ - appSettingsConfig = self.config.GetAppSettings() - - # Load config as default: - if appSettingsConfig.HasConfigKey(q_preset.key): - q_preset.default = appSettingsConfig.GetConfigValue(q_preset.key) - - value = q_preset.Ask() + else: + settings_config = self.config.GetSingleConfigOfType( + choice.NAME) - if value: - # Add to config: - appSettingsConfig.UpdateConfigValue(q_preset.key, value) + # Add empty config if missing: + if not settings_config: + settings_config = self.config.AddConfiguration( + KEY_SETTINGS, {}, config_type=choice.NAME) - self.ManageSettings() + # Edit: + self.EditActiveClass(choice, settings_config) + self.ManageSettings() def DisplayHelp(self) -> None: """" Display the help message, and load the main menu """ @@ -231,13 +210,13 @@ 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""" - if self.IsWarehouseActive(whClass): + whConfig = self.config.GetSingleConfigOfType(whClass.NAME) + + if whConfig: manageWhChoices = [ {"name": "Edit the warehouse settings", "value": "Edit"}, {"name": "Remove the warehouse", "value": "Remove"} @@ -256,19 +235,15 @@ def ManageSingleWarehouse(self, whClass): elif choice == "Add": self.AddActiveClass(whClass, KEY_ACTIVE_WAREHOUSES) self.ManageWarehouses() - else: - whConfig = self.config.GetConfigsOfType(whClass.NAME)[0] + elif whConfig: if choice == "Edit": - self.EditActiveWarehouse(whConfig) + self.EditActiveClass(whClass, whConfig) + self.ManageSingleWarehouse(whClass) elif choice == "Remove": - confirm = inquirer.confirm(message="Are you sure?").execute() - - if confirm: - self.RemoveActiveConfiguration(whConfig) - + self.RemoveActiveConfiguration(whConfig) self.ManageWarehouses() - def ManageSingleEntity(self, entityConfig: SingleConfiguration): + def ManageSingleEntity(self, entityConfig: SingleConfiguration, ecm: EntityClassManager): """ UI to manage an active entity (edit config/remove) """ manageEntityChoices = [ @@ -284,13 +259,13 @@ def ManageSingleEntity(self, entityConfig: SingleConfiguration): if choice == CHOICE_GO_BACK: self.ManageEntities() elif choice == "Edit": - self.EditActiveEntity(entityConfig) # type: ignore - elif choice == "Remove": - confirm = inquirer.confirm(message="Are you sure?").execute() - - if confirm: - self.RemoveActiveConfiguration(entityConfig) + entityClass = ecm.GetClassFromName(entityConfig.GetType()) + self.EditActiveClass( + entityClass, entityConfig) + self.ManageSingleEntity(entityConfig, ecm) + elif choice == "Remove": + self.RemoveActiveConfiguration(entityConfig) self.ManageEntities() def SelectNewEntity(self, ecm: EntityClassManager): @@ -303,7 +278,7 @@ def SelectNewEntity(self, ecm: EntityClassManager): entityChoices = [] for entityClass in entityClasses: - if self.config.GetConfigsOfType(entityClass.NAME): + if self.config.GetAllConfigsOfType(entityClass.NAME): if not entityClass.AllowMultiInstance(): continue entityChoices.append( @@ -346,13 +321,11 @@ def ShowUnsupportedEntities(self, ecm: EntityClassManager): def RemoveActiveConfiguration(self, singleConfig: SingleConfiguration) -> None: """ Remove configuration (wh or entity) """ - self.config.RemoveActiveConfiguration(singleConfig) - self.DisplayMessage( - f"{singleConfig.GetCategoryName()} removed: {singleConfig.GetLabel()}") - - def IsWarehouseActive(self, whClass) -> bool: - """ Return True if a warehouse is active """ - return bool(self.config.GetConfigsOfType(whClass.NAME)) + confirm = inquirer.confirm(message="Are you sure?").execute() + if confirm: + self.config.RemoveActiveConfiguration(singleConfig) + self.DisplayMessage( + f"{singleConfig.GetCategoryName()} removed: {singleConfig.GetLabel()}") def AddActiveClass(self, ioClass, config_category: str) -> None: """Add a wh or Entity to configuration. @@ -373,13 +346,13 @@ def AddActiveClass(self, ioClass, config_category: str) -> None: self.DisplayMessage(messages.PRESET_RULES) self.DisplayMessage( - f"Configure {ioClass.NAME} {ioClass.CATEGORY_NAME}") + f"Configure {ioClass.NAME} {CONFIG_CATEGORY_NAME[config_category]}") preset.AskQuestions() self.ClearScreen(force_clear=True) else: self.DisplayMessage( - f"No configuration needed for this {ioClass.CATEGORY_NAME} :)") + f"No configuration needed for this {CONFIG_CATEGORY_NAME[config_category]} :)") self.config.AddConfiguration( config_category, preset.GetDict(), ioClass.NAME) @@ -389,27 +362,75 @@ def AddActiveClass(self, ioClass, config_category: str) -> None: except Exception as e: print( - f"Error during {ioClass.CATEGORY_NAME} preset loading: {str(e)}") + f"Error during {CONFIG_CATEGORY_NAME[config_category]} preset loading: {str(e)}") - def EditActiveWarehouse(self, whConfig: SingleConfiguration) -> None: - """ UI for single Warehouse settings edit """ - self.DisplayMessage( - "You can't do that at the moment, change the configuration file manually. Sorry for the inconvenience") + def EditActiveClass(self, ioClass, single_config: "SingleConfiguration") -> None: + """ UI for changing settings """ + preset = ioClass.ConfigurationPreset() - self.ManageWarehouses() + if preset.HasQuestions(): - # TODO Implement - # WarehouseMenuPresetToConfiguration appends a warehosue to the conf so here I should remove it to read it later - # TO implement only when I know how to add removable value while editing configurations + choices = [] - def EditActiveEntity(self, entityConfig: SingleConfiguration) -> None: - """ UI for single Entity settings edit """ - self.DisplayMessage( - "You can't do that at the moment, change the configuration file manually. Sorry for the inconvenience") + # Add tag: + if single_config.GetTag(): + preset.AddTagQuestion() - self.ManageEntities() + for entry in preset.presets: + # Load config instead of default: + if single_config.HasConfigKey(entry.key): + value = single_config.GetConfigValue(entry.key) + if entry.question_type == "secret": + value = "*" * len(value) + else: + value = entry.default + + # Nice display for None: + if value is None: + value = "" + + choices.append({ + "name": f"{entry.name}: {value}", + "value": entry.key + }) + + choice = self.DisplayMenu( + choices=choices, + message="Select config to edit") + + if choice == CHOICE_GO_BACK: + return + else: + q_preset = preset.GetPresetByKey(choice) + if q_preset: + self.EditSinglePreset(q_preset, single_config) + self.EditActiveClass(ioClass, single_config) + else: + self.DisplayMessage(f"Question preset not found: {choice}") - # TODO Implement + else: + self.DisplayMessage( + f"No configuration for this {single_config.GetCategoryName()} :)") + + def EditSinglePreset(self, q_preset: QuestionPreset, single_config: SingleConfiguration): + """ UI for changing a single setting """ + + try: + # Load config as default: + if single_config.HasConfigKey(q_preset.key): + if q_preset.default: + q_preset.instruction = f"Default: {q_preset.default}" + q_preset.default = single_config.GetConfigValue(q_preset.key) + + value = q_preset.Ask() + + # If no default and not changed, do not save: + if value or q_preset.default is not None: + # Add to config: + single_config.UpdateConfigValue(q_preset.key, value) + + except UserCancelledException: + self.DisplayMessage("Configuration cancelled", force_clear=True) def ClearScreen(self, force_clear=False): """ Clear the screen on any platform. If self.pinned_lines greater than zero, it won't be cleared. diff --git a/IoTuring/Configurator/MenuPreset.py b/IoTuring/Configurator/MenuPreset.py index 6bbc7c35..1fe94975 100644 --- a/IoTuring/Configurator/MenuPreset.py +++ b/IoTuring/Configurator/MenuPreset.py @@ -28,7 +28,13 @@ def __init__(self, self.value = None self.question = self.name - if mandatory: + + # yesno question cannot be mandatory: + if self.question_type == "yesno": + self.mandatory = False + + # Add mandatory mark: + if self.mandatory: self.question += " {!}" def ShouldDisplay(self, menupreset: "MenuPreset") -> bool: @@ -76,22 +82,22 @@ def validate(x): return bool(x) }) question_options["message"] = self.question + ":" - + if self.default is not None: # yesno questions need boolean default: if self.question_type == "yesno": question_options["default"] = \ bool(str(self.default).lower() - in BooleanAnswers.TRUE_ANSWERS) + in BooleanAnswers.TRUE_ANSWERS) elif self.question_type == "integer": question_options["default"] = int(self.default) else: question_options["default"] = self.default else: if self.question_type == "integer": - # The default default is 0, overwrite to None: + # The default integer is 0, overwrite to None: question_options["default"] = None - + # text: prompt_function = inquirer.text @@ -117,6 +123,7 @@ def validate(x): return bool(x) elif self.question_type == "filepath": prompt_function = inquirer.filepath + # Ask the question: prompt = prompt_function( instruction=self.instruction, **question_options diff --git a/IoTuring/Entity/Entity.py b/IoTuring/Entity/Entity.py index 4f9ec963..625928b9 100644 --- a/IoTuring/Entity/Entity.py +++ b/IoTuring/Entity/Entity.py @@ -3,7 +3,7 @@ import subprocess from IoTuring.Configurator.ConfiguratorObject import ConfiguratorObject -from IoTuring.Configurator.Configuration import SingleConfiguration, CONFIG_CATEGORY_NAME, KEY_ACTIVE_ENTITIES +from IoTuring.Configurator.Configuration import SingleConfiguration from IoTuring.MyApp.AppSettings import AppSettings, CONFIG_KEY_UPDATE_INTERVAL from IoTuring.Exceptions.Exceptions import UnknownEntityKeyException from IoTuring.Logger.LogObject import LogObject @@ -16,8 +16,6 @@ class Entity(LogObject, ConfiguratorObject): NAME = "Unnamed" ALLOW_MULTI_INSTANCE = False - CATEGORY_NAME = CONFIG_CATEGORY_NAME[KEY_ACTIVE_ENTITIES] - def __init__(self, single_configuration: SingleConfiguration) -> None: diff --git a/IoTuring/Logger/Logger.py b/IoTuring/Logger/Logger.py index 14ae6840..8ae443cd 100644 --- a/IoTuring/Logger/Logger.py +++ b/IoTuring/Logger/Logger.py @@ -9,6 +9,21 @@ from IoTuring.Logger import consts from IoTuring.Logger.LogLevel import LogLevelObject, LogLevel from IoTuring.Exceptions.Exceptions import UnknownLoglevelException +from IoTuring.Configurator.ConfiguratorObject import ConfiguratorObject +from IoTuring.Configurator.MenuPreset import MenuPreset + + +CONFIG_KEY_CONSOLE_LOG_LEVEL = "console_log_level" +CONFIG_KEY_FILE_LOG_LEVEL = "file_log_level" +CONFIG_KEY_FILE_LOG_ENABLED = "file_log_enabled" +CONFIG_KEY_FILE_LOG_PATH = "file_log_path" + + +LogLevelChoices = [{"name": l["string"], "value": l["const"]} + for l in consts.LOG_LEVELS] + + + class Singleton(type): @@ -24,7 +39,9 @@ def __call__(cls): # type: ignore return cls._instances[cls] -class Logger(LogLevelObject, metaclass=Singleton): +class Logger(LogLevelObject, ConfiguratorObject, metaclass=Singleton): + + NAME = "Logger" lock = threading.Lock() @@ -176,3 +193,26 @@ def checkTerminalSupportsColors(): # isatty is not always implemented, #6223. is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() return supported_platform and is_a_tty + + + @classmethod + def ConfigurationPreset(cls): + preset = MenuPreset() + + preset.AddEntry(name="Console log level", key=CONFIG_KEY_CONSOLE_LOG_LEVEL, + question_type="select", mandatory=True, default=consts.DEFAULT_LOG_LEVEL, + 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", default="Y") + + preset.AddEntry(name="File log level", key=CONFIG_KEY_FILE_LOG_LEVEL, + question_type="select", mandatory=True, default=consts.DEFAULT_LOG_LEVEL, + choices=LogLevelChoices) + + preset.AddEntry(name="File log path", key=CONFIG_KEY_FILE_LOG_PATH, + question_type="filepath", mandatory=True, default=consts.DEFAULT_LOG_LEVEL, + instruction="Directory where log files will be saved") + + return preset \ No newline at end of file diff --git a/IoTuring/MyApp/AppSettings.py b/IoTuring/MyApp/AppSettings.py index 621c3ed2..332e9b33 100644 --- a/IoTuring/MyApp/AppSettings.py +++ b/IoTuring/MyApp/AppSettings.py @@ -6,54 +6,34 @@ from IoTuring.Configurator.MenuPreset import MenuPreset from IoTuring.Logger.Logger import Singleton -from IoTuring.Logger.consts import LOG_LEVELS, DEFAULT_LOG_LEVEL - -CONFIG_KEY_CONSOLE_LOG_LEVEL = "console_log_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" -LogLevelChoices = [{"name": l["string"], "value": l["const"]} - for l in LOG_LEVELS] - class AppSettings(ConfiguratorObject, metaclass=Singleton): """Singleton for storing AppSettings, not related to Entites or Warehouses """ + NAME = "AppSettings" def __init__(self) -> None: pass def LoadConfiguration(self, configurator: "Configurator"): """ Load/update configurations to the singleton """ - self.configurations = configurator.config.GetAppSettings() + self.configurations = configurator.config.GetSingleConfigOfType(self.NAME) self.AddMissingDefaultConfigs() @classmethod def ConfigurationPreset(cls): preset = MenuPreset() - preset.AddEntry(name="Console log level", key=CONFIG_KEY_CONSOLE_LOG_LEVEL, - question_type="select", mandatory=True, default=DEFAULT_LOG_LEVEL, - 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="integer", default=10) - preset.AddEntry(name="Secondary update interval in minutes", - key=CONFIG_KEY_SLOW_INTERVAL, mandatory=True, - question_type="integer", default=10) + # preset.AddEntry(name="Secondary update interval in minutes", + # key=CONFIG_KEY_SLOW_INTERVAL, mandatory=True, + # question_type="integer", default=10) return preset diff --git a/IoTuring/Warehouse/Warehouse.py b/IoTuring/Warehouse/Warehouse.py index 9ef6fb78..8f02afe5 100644 --- a/IoTuring/Warehouse/Warehouse.py +++ b/IoTuring/Warehouse/Warehouse.py @@ -4,7 +4,7 @@ from IoTuring.Configurator.ConfiguratorObject import ConfiguratorObject from IoTuring.Entity.EntityManager import EntityManager from IoTuring.MyApp.AppSettings import AppSettings, CONFIG_KEY_UPDATE_INTERVAL -from IoTuring.Configurator.Configuration import SingleConfiguration, CONFIG_CATEGORY_NAME, KEY_ACTIVE_WAREHOUSES +from IoTuring.Configurator.Configuration import SingleConfiguration from threading import Thread import time @@ -12,7 +12,6 @@ class Warehouse(LogObject, ConfiguratorObject): NAME = "Unnamed" - CATEGORY_NAME = CONFIG_CATEGORY_NAME[KEY_ACTIVE_WAREHOUSES] def __init__(self, single_configuration: SingleConfiguration) -> None: self.loopTimeout = float(AppSettings().GetFromConfigurations(CONFIG_KEY_UPDATE_INTERVAL)) diff --git a/IoTuring/__init__.py b/IoTuring/__init__.py index de5504c8..9e9854df 100644 --- a/IoTuring/__init__.py +++ b/IoTuring/__init__.py @@ -52,10 +52,6 @@ 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: @@ -76,12 +72,13 @@ def loop(): # This have to start after configurator.Menu(), otherwise won't work starting from the menu signal.signal(signal.SIGINT, Exit_SIGINT_handler) - - logger.Log(Logger.LOG_INFO, "App", App()) # Print App info logger.Log(Logger.LOG_INFO, "Configurator", "Run the script with -c to enter configuration mode") + # Load AppSettings: + AppSettings().LoadConfiguration(configurator) + eM = EntityManager() # These will be done from the configuration reader