Consensus.ConfigManager module
Usage:
To use the ConfigManager class, import it from the Consensus package, create an instance, and call its methods to load, save, update, and reset the configuration.
Example:
# Import the ConfigManager class
from Consensus.ConfigManager import ConfigManager
# Create an instance of ConfigManager
config_manager = ConfigManager()
# Access the current configuration file path
config_file_path = config_manager.config_file
# Access the default configuration
default_config = config_manager.DEFAULT_CONFIG
# Define a new configuration
new_config = {
'nomis_api_key': 'your_api_key',
'lg_inform_key': 'your_lg_inform_key',
'lg_inform_secret': 'your_lg_inform_secret',
'proxies': {
'http': 'your_http_proxy',
'https': 'your_https_proxy'
}
}
# Save the new configuration
config_manager.save_config(new_config)
# Update specific values in the configuration
updates = {
'nomis_api_key': 'new_api_key',
'proxies.http': 'new_http_proxy'
}
config_manager.update_config(updates)
# Reset the configuration to default values
config_manager.reset_config()
Note that new configuration files are always saved in Consensus/config/config.json
.
- class Consensus.ConfigManager.ConfigManager(config_file=None)
Bases:
object
A class to manage loading, saving, updating, and resetting a JSON configuration file.
This class provides an interface to manage a configuration file stored in JSON format, allowing users to define their configuration settings and update them as needed.
- config_file
The path to the configuration file.
- Type:
str
- DEFAULT_CONFIG
The default configuration values.
- Type:
Dict[str, Any]
- __init__(config_file
str = None): Initializes the ConfigManager with a specified or default config file.
- save_config(config
Dict[str, Any]): Saves a configuration dictionary to the config file.
- update_config(updates
Dict[str, Any]): Updates configuration with specified key-value pairs, supporting nested keys.
- reset_config()
Resets the configuration file to default values.
- DEFAULT_CONFIG = {'lg_inform_key': '', 'lg_inform_secret': '', 'nomis_api_key': '', 'proxies': {'http': '', 'https': ''}}
- __init__(config_file=None)
Initializes the ConfigManager with a specified or default config file.
- Parameters:
config_file (str) – Path to the configuration file. If not provided, defaults to ‘config/config.json’ within the package.
- reset_config()
Resets the configuration file to default values.
- Return type:
None
- Returns:
None
- save_config(config)
Saves a configuration dictionary to the config file.
- Parameters:
config (Dict[str, Any]) – The configuration dictionary to save.
- Return type:
None
- Returns:
None
- update_config(updates)
Updates the configuration with specified key-value pairs, supporting nested keys using dot notation.
- Parameters:
updates (Dict[str, Any]) – A dictionary of key-value pairs to update in the config. Nested keys can be specified in dot notation (e.g., ‘proxies.http’).
- Return type:
None
- Returns:
None