Package: pysettings-validator
pysettingsPysettings is a Python package to store your application settings. Compared to some
settings managers, this package has been inspired byDjango Rest Frameworks validatorswhere you can validate the user input beforehand.That simplifies your code because settings don't need to be validated in your application
logic. Available features are:Store your application settings without using global objects.Extend your settings using aBaseSettingsclass. The resulting class can be validated
using asettings.is_valid()method.Fields are represented with anOptionfield that takesvalidatorsas parameter.
It's possible to set adefaultvalue if the option is not set by users.Out of the box validators:not_null,is_https_url.It's possible to add custom validators as functions.RequirementsPython 3.7+Getting Startedpysettingsis available on PyPI:$pipinstallpysettings-validatorCreate your Settingsfrompysettings.baseimportBaseSettingsfrompysettings.optionsimportOptionfrompysettings.validatorsimportis_https_url# Class definitionclassSettings(BaseSettings):url=Option(validators=[is_https_url])description=Option()# Use settings in your applicationsettings=Settings()settings.url="https://example.com"settings.description="A shiny Website!"settings.is_valid()# returns (True, [])Settings APIsettingsinstance doesn't allow to set attributes not defined asOption. If you
try to set a setting that is not defined, aOptionNotAvailableexception is raised:classSettings(BaseSettings):description=Option()# Use settings in your applicationsettings=Settings()settings.url="https://example.com"# raise `OptionNotAvailable`is_valid()exposes araise_exception=Truekwarg in case you prefer to not raise
exceptions in your code:classSettings(BaseSettings):url=Option(validators=[is_https_url])# Use settings in your applicationsettings=Settings()settings.url="http://example.com"settings.is_valid()# raise ConfigNotValid exceptionsettings.is_valid(raise_exception=False)# return (False, [{'url': [{'is_https_url': 'The schema must be HTTPS'}]}])Create a Custom Validator# app/validators.pyfrompysettings.exceptionsimportValidationErrordefis_a_boolean(value):ifisinstance(value,bool):returnTrueelse:raiseValidationError("The value must a Boolean")# app/settings.pyfrom.validatorsimportis_a_booleanclassSettings(BaseSettings):dry_run=Option(validators=[is_a_boolean])description=Option()# app/main.pysettings=Settings()settings.dry_run="Yes"settings.description="Dry run mode!"settings.is_valid()# raises ConfigNotValid exceptionTest your Settings (pytest)If you need to change some of your settings during tests, you can use the following snippet
to restore the previous settings after each test:# tests/conftest.pyfromapp.configimportsettingsasapp_settings# Import your global settingsfrompysettings.testimportSettingsWrapper# Import settings wrapper@pytest.fixture(scope="function")defsettings():wrapper=SettingsWrapper(app_settings)# (Optional) Include test overrideswrapper.DATABASE_URL="sqlite://"yieldwrapper# Restore original settingswrapper.finalize()# tests/test_settings.pydeftest_settings(settings):# Change settings for this test onlysettings.BATCH_SIZE=100# ... Test your code ...DevelopmentWe accept external contributions even though the project is mostly designed for personal
needs. If you think some parts can be exposed with a more generic interface, feel free
to open a GitHub issue and to discuss your suggestion.Coding GuidelinesWe useflake8as a style guide enforcement. That said, we also useblackto
reformat our code, keeping a well defined style even for quotes, multi-lines blocks and other.
Before submitting your code, be sure to launchblackto reformat your PR.Testingtoxis used to execute the following test matrix:lint: launchesflake8andblack --checkto be sure the code honors our style guidelinepy{3.7,3.8,3.9,3.10,3.11}: launchespy.testto execute tests with different Python versions.To launch the full test matrix, just:$tox