Package: dyacon
Russian version:README.ru.mdDyaconDyacon is a library for working with YAML configurations in Python. It provides tools for convenient configuration loading and processing and supports loading values from environment variables and other sources, such as Google Secret Manager.Using multiple configuration files allows you to store multiple sets of settings for one service simultaneously, making it easy to switch between them for different purposes, such as testing or debugging.InstallationYou can installdyaconusing pip:pipinstalldyaconIf you want to use Google Secret Manager support, install the library with the appropriate options and set theGOOGLE_APPLICATION_CREDENTIALSenvironment variable to the path of your Google Cloud service account file:pipinstall"dyacon[google]"Why Use Dyacon?The Dyacon library is designed to simplify working with YAML configurations in your Python applications. It offers the following features:Combining Multiple YAML Files into One: Dyacon allows you to create a hierarchy of configurations by collecting data from multiple files. This is convenient for splitting configuration into different files and components. For more details, read theLoading Configurationsection.Loading Values from Environment Variables: Dyacon allows you to include values from environment variables in your configuration. You can use the special syntax${env_variable_name:default_value}to specify the name of the environment variable to look for and a default value.# config/config.yamltoken:"${API_TOKEN:default_value}"# config.pyfromdataclassesimportdataclassfromdyaconimportread_config,Load@dataclassclassConfig:token:str=Load()config=read_config(Config)Loading Values from Google Secret Manager: If you work in the Google Cloud, Dyacon provides a loader for Google Secret Manager. You can load confidential data such as passwords and keys from Secret Manager using the syntax${env_variable_name:project_id:default_value}.# config/config.yamltoken:"${API_TOKEN:my-project:default_value}"# config.pyfromdataclassesimportdataclassfromdyaconimportread_config,Loadfromdyacon.loaders.externalimportGoogleCloudSecretLoader@dataclassclassConfig:token:str=Load([GoogleCloudSecretLoader()])config=read_config(Config)UsageLoading ConfigurationDyacon provides tools for loading configuration from a YAML file and converting it into Python objects. A target configuration file must be created in which other files will be included, if necessary. For example, in the example below, the fileconfig/config.yamlincludes the filedb/local.yaml. If you want to use settings for a production environment, you can create a file with a different name, such asdb/prod.yaml, and include it inconfig/config.yamlusing the!include db/prod.yamldirective. The path in the!includedirective should be relative to the file in which it is used.As a result, we would only need to change one line in the configuration file to switch to different settings.# config/config.yamldb:!includedb/local.yaml# config/db/local.yamlhost:localhostport:3306name:local_db# config.pyfromdataclassesimportdataclassfromdyaconimportread_config@dataclassclassDbConfig:host:strport:intname:str@dataclassclassConfig:db:DbConfigconfig=read_config(Config)print(config.db.host)# localhostTheread_configfunction takes an optionalpathargument that specifies the target YAML file to load. If the argument is not provided, Dyacon will look for theconfig/config.yamlfile relative to the current working directory.Load DescriptorTheLoaddescriptor allows you to inject automatic configuration value loading from various sources.Loadtakes a list of loaders that will be used to load values. By default, theEnvLoaderloader is used, which loads values from environment variables. You can add other loaders to load values from other sources.The descriptor accepts optional argumentsdefault,default_factory, andrequired.If a field in the configuration class is marked with theLoaddescriptor, but that field is not present in the configuration YAML file, therequiredargument will be checked. If therequiredvalue is set toTrue, aValueErrorexception will be raised. Otherwise, the value obtained fromdefault_factoryordefaultwill be used. The function provided indefault_factorywill be called without arguments.# config.pyfromdataclassesimportdataclassfromdyaconimportread_config,Load@dataclassclassConfig:token:str=Load(default_factory=lambda:"default_value")config=read_config(Config)LoadersDyacon provides several built-in loaders that can be used with theLoaddescriptor.EnvLoaderTheEnvLoaderloader loads values from environment variables. To load values, use the${env_variable_name:default_value}syntax. If the environment variable is not set, the default value will be used.GoogleCloudSecretLoaderTheGoogleCloudSecretLoaderloader loads values from Google Secret Manager. To load values, use the!{env_variable_name:project_id:default_value}syntax. To use this loader, you need to install thegoogle-cloud-secret-managerlibrary with the following command:pipinstall"dyacon[google]"orpipinstallgoogle-cloud-secret-managerYou also need to set theGOOGLE_APPLICATION_CREDENTIALSenvironment variable to the path of your Google Cloud service account file.To use theGoogleCloudSecretLoaderloader, pass it in the list of loaders for theLoaddescriptor. You can also implement your own descriptor by inheriting fromLoadand overriding the__init__method.fromtypingimportList,Optional,Callable,AnyfromdyaconimportLoad,Loaderfromdyacon.loaders.externalimportGoogleCloudSecretLoaderclassNewLoad(Load):def__init__(self,loaders:Optional[List[Loader]]=None,*,default:Optional[str]=None,default_factory:Optional[Callable[[],Any]]=None,required:bool=False,):super().__init__(loaders,default=default,default_factory=default_factory,required=required,)self.loaders.append(GoogleCloudSecretLoader())Contributions and FeedbackIf you have ideas, suggestions, or find bugs, please share them onGitHub. Your contributions are welcome!LicenseDyacon is distributed under the MIT license. See theLICENSEfile for details.Author:Aleksei Morozov|GitHub