Package: app-settings
Simplify usage of YAML files for application configuration.Inspired by Ruby Gem “config”https://github.com/railsconfig/configBut
not all features implemented yet.Usage exampleInstallation:pip install app_settingsCreate sample app with following structure:/my_appmy_app.py/configsettings.yml# at least one *.yml file requiredadditional_settings.yml/settings# use this only in case if you need different settings for your environmentsproduction.ymlproduction.db.ymldevelopment.ymldevelopment.db.ymlAppSettings– singleton, so it is not possible to create more than
one instance of config object.By default, script will try to load all./config/*.ymland, if exist./config/settings/${APP_ENV}*.ymlconfiguration files.Take note, that all variables defined in./config/*.ymlfiles will
be overridden by variables from./config/settings/${APP_ENV}.yml.Also,./config/*.ymlloaded in alphabetical order, so if you will
define variable VAR inadditional_settings.ymlit will be redefined
by VAR fromsettings.yml.If you have settings, which not depends on the environment, simply use./config/{filename}.yml; In case, when you need settings, which
depends on the environment, use./config/settings/{ENV}.yml.Environment (development/stage/production etc)If your application use more than one environment, for exampledevelopmentandproduction, you can specify what config file to
load by setting env variable# before your application starts# export APP_ENV='production' or APP_ENV='production' python my_app.pyfromapp_settingsimportAppSettingscfg=AppSettings()By default, if noAPP_ENVis given, file./config/settings/development.ymlwill be loaded, that’s why this
file is required.Also it is possible to redefine name of variable.# export TEST_ENV='production'fromapp_settingsimportAppSettingscfg=AppSettings(env_name='TEST_ENV')Working with environment variablesIt is possible to redefine or set settings from environment variables.
AppSettings will read all env variables withSETTINGSprefix (by
default).# settings.ymllevel1:redefined:'redefined'not_redefined:'notredefined'# export SETTINGS__MY_VAR='test'# export SETTINGS__LEVEL1__REDEFINED='val'fromapp_settingsimportAppSettingscfg=AppSettings()print(cfg.my_var)# 'test'print(cfg.level1.redefined)# 'val'print(cfg.level1.not_redefined)# 'not redefined'You can setup your own prefix:# export MYPREFIX__MY_VAR='test'# export MYPREFIX__LEVEL1__REDEFINED='val'fromapp_settingsimportAppSettingscfg=AppSettings(prefix='myprefix')print(cfg.my_var)# 'test'print(cfg.level1.redefined)# 'val'Also it is possible to setup environment variable splitter (default:__).# export SETTINGS.MY_VAR='test'# export SETTINGS.LEVEL1.REDEFINED='val'fromapp_settingsimportAppSettingscfg=AppSettings(splitter='.')print(cfg.my_var)# 'test'print(cfg.level1.redefined)# 'val'If you don’t need to set/redefine settings from environment variables,
useuse_envflag.fromapp_settingsimportAppSettingscfg=AppSettings(use_env=False)Suppress KeyError exceptionIn case, if you don’t want to receive KeyError exception if key not
defined in file, you can useraise_errorflag. By default: Truefromapp_settingsimportAppSettingscfg=AppSettings(raise_error=False)key=cfg.this_value_not_exist# key == NoneConfig pathYou can redefine default config pathfromapp_settingsimportAppSettingscfg=AppSettings(configs_path='my_config_path')Run testscdapp_settingspython-mpytest-v--alluredir=./tests/results-n`nproc`--cov=app_settings--cov-config.coveragerc./testsSample usage for testsRun these commands to create sample files structure$cd<yourprojectdir>$mkdir-pconfig/settings&&\touchconfig/settings.yml&&\touchconfig/settings/{production.yml,testing.yml}&&\echo"implicity_wait: 5">config/settings.yml&&\echo'search_text: "production environment"'>config/settings/production.yml&&\echo'search_text: "testing environment"'>config/settings/testing.yml$touchtest_with_app_settings.pyInstall all python requirements:pipinstallseleniumpytestapp_settingsCopy code totest_with_app_settings.py### Example, don't use it in your codeimportosos.environ['TEST_ENV']='production'### exampleimportpytestfromapp_settingsimportAppSettingsfromseleniumimportwebdriver@pytest.fixture(scope='session')defsettings():cfg=AppSettings(env_name='TEST_ENV')returncfg@pytest.fixturedefbrowser(settings):driver=webdriver.Chrome()driver.implicitly_wait(settings.implicity_wait)yielddriverdriver.close()deftest_example(browser,settings):browser.get("https://ya.ru")search_field=browser.find_element_by_id('text')search_field.send_keys(settings.search_text)# depending on envsearch_button=browser.find_element_by_tag_name('button')search_button.click()browser.find_elements_by_css_selector("div ul li")TODOAdd reload feature