Skip to content

Commit

Permalink
Finished Logger settings
Browse files Browse the repository at this point in the history
  • Loading branch information
infeeeee committed Jan 14, 2024
1 parent 2a29178 commit d5c09fd
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 145 deletions.
12 changes: 8 additions & 4 deletions IoTuring/Configurator/Configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,25 @@ def GetAllConfigsOfType(self, config_type: str, config_category: str = "") -> li

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.
def LoadSingleConfig(self, config_type: str, config_category: str) -> SingleConfiguration:
""" Return the only configuration of the given type. Raises exception if multiple found.
Add the config if not found.
Args:
config_type (str): The type of config to return
config_category (str): KEY_ACTIVE_ENTITIES, KEY_ACTIVE_WAREHOUSES or KEY_SETTINGS
Raises:
Exception: Multiple config found
Returns:
None | SingleConfiguration: The config, None if not found
SingleConfiguration: The config
"""
configs = self.GetAllConfigsOfType(config_type=config_type)
if not configs:
return None
return self.AddConfiguration(
config_category, {}, config_type)

if len(configs) > 1:
raise Exception("Multiple configs found!")

Expand Down
19 changes: 7 additions & 12 deletions IoTuring/Configurator/Configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def ManageWarehouses(self) -> None:
for whClass in availableWarehouses:

enabled_sign = "X" \
if self.config.GetSingleConfigOfType(whClass.NAME) else " "
if self.config.GetAllConfigsOfType(whClass.NAME) else " "

manageWhChoices.append(
{"name": f"[{enabled_sign}] - {whClass.NAME}",
Expand Down Expand Up @@ -184,13 +184,7 @@ def ManageSettings(self) -> None:
self.Menu()

else:
settings_config = self.config.GetSingleConfigOfType(
choice.NAME)

# Add empty config if missing:
if not settings_config:
settings_config = self.config.AddConfiguration(
KEY_SETTINGS, {}, config_type=choice.NAME)
settings_config = self.config.LoadSingleConfig(choice.NAME, KEY_SETTINGS)

# Edit:
self.EditActiveClass(choice, settings_config)
Expand All @@ -215,9 +209,9 @@ def WriteConfigurations(self) -> None:
def ManageSingleWarehouse(self, whClass):
"""UI for single Warehouse settings"""

whConfig = self.config.GetSingleConfigOfType(whClass.NAME)
whConfigList = self.config.GetAllConfigsOfType(whClass.NAME)

if whConfig:
if whConfigList:
manageWhChoices = [
{"name": "Edit the warehouse settings", "value": "Edit"},
{"name": "Remove the warehouse", "value": "Remove"}
Expand All @@ -236,7 +230,8 @@ def ManageSingleWarehouse(self, whClass):
elif choice == "Add":
self.AddActiveClass(whClass, KEY_ACTIVE_WAREHOUSES)
self.ManageWarehouses()
elif whConfig:
elif whConfigList:
whConfig = whConfigList[0]
if choice == "Edit":
self.EditActiveClass(whClass, whConfig)
self.ManageSingleWarehouse(whClass)
Expand Down Expand Up @@ -419,7 +414,7 @@ def EditSinglePreset(self, q_preset: QuestionPreset, single_config: SingleConfig
try:
# Load config as default:
if single_config.HasConfigKey(q_preset.key):
if q_preset.default:
if q_preset.default and q_preset.question_type != "yesno":
q_preset.instruction = f"Default: {q_preset.default}"
q_preset.default = single_config.GetConfigValue(q_preset.key)

Expand Down
26 changes: 21 additions & 5 deletions IoTuring/Configurator/ConfiguratorLoader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

from IoTuring.Entity.Entity import Entity
from IoTuring.Logger.LogObject import LogObject
from IoTuring.Configurator.Configurator import Configurator, KEY_ACTIVE_ENTITIES, KEY_ACTIVE_WAREHOUSES
from IoTuring.Configurator.Configurator import Configurator, KEY_ACTIVE_ENTITIES, KEY_ACTIVE_WAREHOUSES, KEY_SETTINGS
from IoTuring.ClassManager.WarehouseClassManager import WarehouseClassManager
from IoTuring.ClassManager.EntityClassManager import EntityClassManager
from IoTuring.Warehouse.Warehouse import Warehouse

from IoTuring.MyApp.AppSettings import AppSettings
from IoTuring.Logger.Logger import Logger


class ConfiguratorLoader(LogObject):
configurator = None
Expand All @@ -28,11 +31,13 @@ def LoadWarehouses(self) -> list[Warehouse]:
whClass = wcm.GetClassFromName(whConfig.GetLongName())

if whClass is None:
self.Log(self.LOG_ERROR, f"Can't find {whConfig.GetType()} warehouse, check your configurations.")
self.Log(
self.LOG_ERROR, f"Can't find {whConfig.GetType()} warehouse, check your configurations.")
else:
wh = whClass(whConfig)
wh.AddMissingDefaultConfigs()
self.Log(self.LOG_DEBUG, f"Full configuration with defaults: {wh.configurations}")
self.Log(
self.LOG_DEBUG, f"Full configuration with defaults: {wh.configurations.ToDict()}")
warehouses.append(wh)
return warehouses

Expand All @@ -48,11 +53,13 @@ def LoadEntities(self) -> list[Entity]:
for entityConfig in self.configurations.GetConfigsInCategory(KEY_ACTIVE_ENTITIES):
entityClass = ecm.GetClassFromName(entityConfig.GetType())
if entityClass is None:
self.Log(self.LOG_ERROR, f"Can't find {entityConfig.GetType()} entity, check your configurations.")
self.Log(
self.LOG_ERROR, f"Can't find {entityConfig.GetType()} entity, check your configurations.")
else:
ec = entityClass(entityConfig)
ec.AddMissingDefaultConfigs()
self.Log(self.LOG_DEBUG, f"Full configuration with defaults: {ec.configurations}")
self.Log(
self.LOG_DEBUG, f"Full configuration with defaults: {ec.configurations.ToDict()}")
entities.append(ec) # Entity instance
return entities

Expand All @@ -63,3 +70,12 @@ def LoadEntities(self) -> list[Entity]:
# - for each one:
# - pass the configuration to the warehouse function that uses the configuration to init the Warehouse
# - append the Warehouse to the list

def LoadSettings(self) -> None:
settingsClasses = [AppSettings, Logger]
for settingsClass in settingsClasses:
sc = settingsClass()
sc.configurations = self.configurations.LoadSingleConfig(
sc.NAME, KEY_SETTINGS)
sc.AddMissingDefaultConfigs()
sc.__init__()
9 changes: 0 additions & 9 deletions IoTuring/Logger/LogLevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ def __str__(self) -> str:
def __int__(self) -> int:
return self.number

def get_colored_string(self, string: str) -> str:
""" Get colored text according to LogLevel """
if self.color:
out_string = self.color + string + Colors.reset
else:
out_string = string
return out_string



class LogLevelObject:
""" Base class for loglevel properties """
Expand Down
Loading

0 comments on commit d5c09fd

Please sign in to comment.