Config Manager
Loads the configuration from a YAML file, validates it, and provides access to the configuration values.
__init__(config_file, default_config=None, validation_schema=None, placeholders=None)
Initializes the configuration manager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_file
|
str
|
The relative or absolute path to the configuration file. |
required |
default_config
|
Optional[dict]
|
A default configuration dict to use if the config file does not exist. |
None
|
validation_schema
|
Optional[dict]
|
A cerberus style validation schema dict to validate the config file against. |
None
|
placeholders
|
Optional[dict]
|
A dictionary of placeholders to check in the config. If any of these are found, a exception will be raised. |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If the config file does not exist and no default config is provided, or if there are YAML errors in the config file. |
check_for_config_changes(last_check)
Check if the configuration file has changed. If it has, reload the configuration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
last_check
|
datetime
|
The last time the config was checked. |
required |
Returns:
Name | Type | Description |
---|---|---|
result |
datetime | None
|
The new last modified time if the config has changed and was reloaded, None otherwise. |
check_for_placeholders(placeholders)
Recursively scan self._config for any instances of a key found in placeholders.
If the keys and values match (including nested), return True.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
placeholders
|
dict
|
A dictionary of placeholders to check in the config. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If any placeholder is found in the config file, an exception will be raised with a message indicating the placeholder and its value. |
Returns:
Name | Type | Description |
---|---|---|
result |
bool
|
True if any placeholders are found in the config, otherwise False. |
get(*keys, default=None)
Retrieve a value from the config dictionary using a sequence of nested keys.
Example
value = config_mgr.get("DeviceType", "WebsiteAccessKey")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keys
|
*keys
|
Sequence of keys to traverse the config dictionary. |
()
|
default
|
Optional[variable]
|
Value to return if the key path does not exist. |
None
|
Returns:
Name | Type | Description |
---|---|---|
value |
variable
|
The value if found, otherwise the default. |
get_email_settings(config_section='Email')
Returns the email settings from the config file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_section
|
Optional[str]
|
The section in the config file where email settings are stored. |
'Email'
|
Returns:
Name | Type | Description |
---|---|---|
settings |
dict
|
A dictionary of email settings or None if email is disabled or not configured correctly. |
get_logger_settings(config_section='Files')
Returns the logger settings from the config file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_section
|
Optional[str]
|
The section in the config file where logger settings are stored. |
'Files'
|
Returns:
Name | Type | Description |
---|---|---|
settings |
dict
|
A dictionary of logger settings that can be passed to the SCLogger() class initialization. |
get_shelly_settings(config_section='ShellyDevices')
Returns the the settings for one or more Shelly Smart Switches.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_section
|
Optional[str]
|
The section in the config file where settings are stored. |
'ShellyDevices'
|
Returns:
Name | Type | Description |
---|---|---|
settings |
list[dict]
|
A list of dict objects, each one represeting a device. Returns an empty list if no devices are configured or the section does not exist. |
load_config()
Load the configuration from the config file specified to the init method.
Raises:
Type | Description |
---|---|
RuntimeError
|
If there are YAML errors in the config file, if placeholders are found, or if validation fails. |
Returns:
Name | Type | Description |
---|---|---|
result |
bool
|
True if the configuration was loaded successfully, otherwise False. |
register_logger(logger_function)
Registers a logger function to be used for logging messages.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logger_function
|
Callable
|
The function to use for logging messages. |
required |