Package: gestalt-cfg
GestaltGestalt (/ɡəˈSHtält/) -noun- an organized whole that is perceived as more than the sum of its parts.Gestalt is an opinionated, strongly typed, configuration library for Python 3.6+. Gestalt aims to simplify configuration loading, getting/setting, and management by letting the user focus on writing programs, and letting Gestalt check configuration types, location, and environment variable overrides.InstallpipinstallgestaltWhy use Gestalt?In short, Gestalt does the following:Automatically loads configuration files from a directoryAllows for runtime overridesAllows for runtime defaultsAllows for default values when trying to get a configAllows for environment variable overridesType checkseverythingSpecifically, Gestalt enforces configs in the following order:Calls toset_*Environment VariablesConfig File Directories and Single Config FilesDefaults provided inget_*Set default valuesUsageTL;DRfromgestaltimportgestaltg=gestalt.Gestalt()g.add_config_path('./testdata')g.build_config()my_val_1=g.get_string('some.nested.string')my_val_2=g.get_int('someint')my_val_3=g.get_float('somekey',57.29)# If 'somekey' doesn't exist, retrun default valueg.set_bool('my_custom_config',True)g.get_list('my_custom_config')# Raises TypeError, as this key is a bool, and we asked for a listInitializationBeginning to use Gestalt is as simple as:fromgestaltimportgestaltg=gestalt.Gestalt()Loading Configuration FilesConfiguration files are supported in JSON and YAML format, with YAML files being given priority over JSON.Loading a directory of configuration files is done by callingadd_config_pathg.add_config_path('./testdata')Multiple directory paths can be added:g.add_config_path('./testdata2')g.add_config_path('./testdata3')Note that files loaded from a directory are loaded in alphabetical order, with JSON files being loaded first, then YAML files.Individual files can also be loaded:g.add_config_file('./testfile.json')g.add_config_file('./testotherfile.yaml')After all the directory paths and files are added, we can render them:g.build_config()Note that the the last added directory path takes the most precedence, and will override conflicting keys from previous paths. Individual files take precedence over directories. In addition to this, the rendering flattens the config, for example, the configuration:{"yarn":"blue skies","numbers":12345678,"strangenumbers":123.456,"truthy":true,"listing":["dog","cat"],"deep":{"nested1":"hello","nested2":"world"}}Will be rendered to this in the internal data structure:{"yarn":"blue skies","numbers":12345678,"strangenumbers":123.456,"truthy":true,"listing":["dog","cat"],"deep.nested1":"hello","deep.nested2":"world"}The nested values are flattened and delimited by periods, making access simpler.Note, Gestalt will not normalize names in config files, so keys are case sensitive.Nested dictionaries are merged recursively. For example:# config1.json file contents.{"db":{"name":"fake"},"replicas":1,}# config2.json file contents.{"db":{"name":"mydb","password":"password"},}g.add_config_file('./config1.json')g.add_config_file('./config2.json')# Merged result.{"db":{"name":"mydb","password":"password"},"replicas":1,}Environment VariablesEnvironment variable overrides are not enabled by default. To enable it:g.auto_env()With this, Gestalt will check to see if a corresponding environment variable exists, for example if checking for the keysome.nested.key, Gestalt will search forSOME_NESTED_KEY, and attempt to convert it to the desired type.Setting and Getting ValuesTypesCurrently, Gestalt supports 5 basic types:StringIntFloatBoolListFor each of these types, there is a correspondinggetandsetfunction, allowing for Gestalt to guarantee types.SettingTo set a configuration value programmatically:g.set_string('some.k','value')Defined by the function signature:set_string(self,key:str,value:str)->NoneThe same applies to the remaining types that Gestalt supports:set_int(key:str,value:int)->Noneset_float(key:str,value:float)->Noneset_bool(key:str,value:bool)->Noneset_list(key:str,value:List[Any])->NoneNote that all of these functions, in addition from being type hinted, also strongly enforce types, and will raise TypeErrors in the following cases:Setting a key that is not a stringSetting a value that does not match the function signatureOverriding an existing set value of typeawith typebSetting Default ValuesTo set a default configuration value programmatically:g.set_default_string('some.default.k','default value')Defined by the function signatureset_default_string(key:str,value:str)->NoneThe same applies to the remaining types that Gestalt supports:set_default_int(key:str,value:int)->Noneset_default_float(key:str,value:float)->Noneset_default_bool(key:str,value:bool)->Noneset_default_list(key:str,value:List[Any])->NoneNote that all of these functions, in addition from being type hinted, also strongly enforce types, and will raise TypeErrors in the following cases:Setting a key that is not a stringSetting a value that does not match the function signatureOverriding an existing default set value of typeawith typebGetting ValuesTo get a configuration value:g.get_string('some.key')This will attempt to retrieve the given key from the configuration, in order of precedence. If no such key exists in all configurations, aValueErrorwill be raised.In addition to this, a default can be passed:g.get_string('some.key','some default value')Thegetfunction will raiseTypeErrors in the following cases:The key is not a stringThe default value does not match the desired typeThe configuration has the key with a value of typea, when the user desires a value of typebInterpolationGestalt supports interpolation for the config keys and connect them with the correct provider of the choice.To use the interpolation, each value needs to have three partsProviderPathFilterProviders use JSONPath for proper traversal of the secrets and are standard compliant.The interpolation value needs to be set as follows:key:ref+provider://some-path#filterwhereprovider, is the name of the provider,some-pathis the path for the secret in your provider andfilteris used to travese the key once fectched that you want to run on the response if the response is a nested object.The filter is a flattened list with.as the generic delimiter in gestalt after flattening.ProviderGestalt allows third party providers to be used in configuration and fetching respective values as per their system.Working with providers is extremely simple in Gestalt. All that needs to be done is configuration of the provider and gestalt takes care of the rest.To configure providers, use theconfigure_providersmethods. It takes theprovider_nameand the and instance of the class  which is of typeProvider.These should be configured before building the config by calling the methodbuild_config.
Eg. The first behaviour is incorrect in case one uses a provider. The second one is correct and recommended# incorrect wayg.build_config()g.configure_providers("provider_name",provider)# correct wayg.config_providers("provider_name",provider)g.build_config()For more information about the each provider configuration, please read the configuration parameters section for the respective provider.Gestalt supports the following providersVaultVault ProviderVault Provider is a provider support from gestalt to hashicorp vault.
To instatiate your provider, please useconfig_providermethod in gestalt.
Providing the method with a VaultConfig, will configure the provider to connect
with your instance of Vault wherever it is running whether it be local instance
or a cloud instance.Raises a RuntimeError, if the path provided inadd_vault_secret_pathis invalid or the mount is invalid or anything else would be a generic Runtime ErrorConfiguration ParametersVaultConfig is a dataclass of type ProviderClass that takes all the vault configuration needed to
connect to the vault instance.VAULT_ADDR and VAULT_TOKEN are two common environment varibales that are set for working with vault. Hence to work and use the default setup, the Gestalt Vault configuration can read the values from those environment variables on your behalf.ParameterDatatypeDefaultRequiredrolestringNone- [x]jwtstringNone- [x]urlstringVAULT_ADDR- [ ]tokenstringVAULT_TOKEN- [ ]certTuple[string, string]None- [ ]verifyboolTrue- [ ]For kubernetes authentication, one needs to provide `role` and `jwt` as part of the configuration process.Dynamic SecretsCurrently only dynamic secret support is generation of credentials for database. This database must be pre-configred in vault using either Vault UI or Terraform
to maintain ease of library.To get a dynamic secret from the database, use the functiongenerate_database_dynamic_secretwhich will raise Runtime exceptions if the proper setup is not done
otherwise update gestalt config map with the username and password