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()
Check if the configuration file has changed. If it has, reload the configuration.
Returns:
Name | Type | Description |
---|---|---|
result |
bool
|
True if the configuration has changed, False 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. |
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 |
select_file_location(file_name)
Selects the file location for the given file name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
The name of the file to locate. Can be just a file name, or a relative or absolute path. |
required |
Returns:
Name | Type | Description |
---|---|---|
file_path |
Path
|
The full path to the file as a Path object. If the file does not exist in the current directory, it will look in the script directory. |