Package: pysettings-yaml
Pysettings YAMLThis library extends over the concepts ofDecoupleandSplit Settingsto create a library that is simple to use and expresses your settings in a
yaml file for higher expressivity.Installationpip install pysettings-yamlUsageDefine a YAML file with the definition of your settings, for example,
asettings.yamlin the same path as your settings module:settings:SAMPLE_SETTING_BOOL:# Each entry inside the settings section is the name of your settingorigins:-name:env-name:directvalue:trueSAMPLE_SETTING_STR:origins:-name:directvalue:banana-name:envThen you can use this file like this in asettings.py:fromsplit_settings.toolsimportoptionalfrompathlibimportPathfrompysettings_yamlimportget_configBASE_DIR=Path(__file__).parentsetting_files=[BASE_DIR/"settings.yaml",]config=get_config(setting_files)SAMPLE_SETTING_BOOL=config("SAMPLE_SETTING_BOOL",cast=bool)SAMPLE_SETTING_STR=config("SAMPLE_SETTING_STR")print(SAMPLE_SETTING_BOOL)print(SAMPLE_SETTING_STR)# This will print bananaBy default the library supports two origin names:env(to receive the value of your setting using thedecouplelibrary)
ordirect(to provide a constant value), but you can add your own providers easily
(refer to the "Adding custom providers" section).Origin are evaluated in strict order. In those examples,SAMPLE_SETTING_BOOLwill evaluate first theenvorigin and, if that value isNone, it will
process the next origin, which would bedirect. In contrast, forSAMPLE_SETTING_STR,
the first origin is thedirectorigin. Since direct can never returnNone,
the env origin will never be evaluated.Overriding settingsYou can define several setting files to be processed in order.
All of them will be merged together, so any subsequent setting file
will override the settings with the same name defined in a previous file
of the list. Very useful if you need to define different origins
for your settings depending on your deployment environment:fromsplit_settings.toolsimportoptionalfrompathlibimportPathimportosfrompysettings_yamlimportget_configENVIRONMENT=os.environ.get("ENVIRONMENT","dev")BASE_DIR=Path(__file__).parentsetting_files=[BASE_DIR/"settings.yaml",BASE_DIR/f"settings.{ENVIRONMENT}.yaml",optional(BASE_DIR/"settings.nonexistent.yaml")]config=get_config(setting_files)SAMPLE_SETTING_BOOL=config("SAMPLE_SETTING_BOOL",cast=bool)SAMPLE_SETTING_STR=config("SAMPLE_SETTING_STR")Any path wrapped with theoptionalfunction will not raise an
exception should that file not exist in the path you specify.Adding custom providersIf you need other providers (for example for getting your settings from a vault
like Amazon SSM) you can implement your own providers like this:fromtypingimportUnion,Optionalfrompysettings_yaml.providers.interfacesimportSettingsProvider,OriginModel,NoValueclassAWSModel(OriginModel):path:strdecrypt:boolclassSampleAWSSettingsProvider(SettingsProvider):name="aws"schema=AWSModeldefget(self,setting_name:str,origin_data:AWSModel)->Union[Optional[str],NoValue]:returnf"path:{origin_data.path}, decrypt:{origin_data.decrypt}"Just make sure to:Provide thenameand a pydanticschemaImplement thegetmethodThen you can obtain your config providing yourSettingProviderand use it in your yaml:config=get_config(setting_files,additional_providers=[SampleAWSSettingsProvider()])settings:SAMPLE_SETTING:origins:-name:awspath:some/pathdecrypt:true-name:env