Skip to content

Commit

Permalink
AppSettings Singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
infeeeee committed Dec 31, 2023
1 parent 5318c78 commit b9332f6
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 29 deletions.
2 changes: 2 additions & 0 deletions IoTuring/Configurator/Configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
11 changes: 0 additions & 11 deletions IoTuring/Configurator/ConfiguratorLoader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()

2 changes: 1 addition & 1 deletion IoTuring/Entity/Entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion IoTuring/Logger/Logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions IoTuring/Logger/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
35 changes: 22 additions & 13 deletions IoTuring/MyApp/AppSettings.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
2 changes: 1 addition & 1 deletion IoTuring/Warehouse/Warehouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
8 changes: 6 additions & 2 deletions IoTuring/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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",
Expand Down

0 comments on commit b9332f6

Please sign in to comment.