Package: django-zero-settings
Django Zero Settingsa Django util for managing app settings.when u create a package for Django, usually if your app can be configured, needs to use Django settings. so you will have some defaults which can be overridden by the user.this package helps you to specify defaults, and the key that users must use for configuring settings, then it will load user settings, update defaults, and import string notations.this is actually howdjango-rest-frameworkconfigures its settings, but with a few more features.Supported Versions3.5 <= Python <= 3.102.0 <= Django <= 4.1Installpip install django-zero-settingsUsagescreate a settings object like this:fromzero_settingsimportZeroSettingsapp_settings=ZeroSettings(key="APP",defaults={"TOKEN":"token"},)then you can importapp_settingsand use it:fromapp.settingsimportapp_settingsprint(app_settings.TOKEN)ArgsZeroSettingscan get following args:argdesckeythe settings key which users will define settings with, is required and must be a string.defaultsdefault settings for the app, required and must be a dict.user_settingsyou can also set user settings manually, in this case, user settings withkeywill not be loaded. is optional and can be a dict.import_stringsa list of setting keys that must be imported, import strings is lazy checked and will raise ImportError on exceptions like:"Could not import 'app.utils.Token' for setting 'APP.TOKEN_CLASS'. ImportError: path does not exist."removed_settingsa dict of settings which had been removed, in{"KEY": "msg"}format. it will raise RuntimeError if a setting is in removed_settings. note that these keys must be also on defaults too, otherwise, it will raise AttributeError instead. themsgpart of dict is the error message. onNoneor empty strings, it generates the default message which is"The 'APP.KEY' setting has been removed."settings_doca string that locates the settings document path, the value will be used to generateremoved_settingserror with a message like:"Please refer to 'https://app.com/doc/settings' for available settings."use_cachea boolean that defines whether to use cache or notstrict_defaultsa boolean that defines whether to be strict on defaults or not, if true, only default keys are valid in user settingspre_check_defaultsa boolean that defines whether to pre check defaults or notpre_check_importsa boolean that defines whether to pre check imports or notpre_check_removeda boolean that defines whether to pre check removed or notImport Stringswith following class and methods atapp.utils:classToken:@staticmethoddefget_token():return"token"defvalidate_value(token):returntoken=="token"defvalidate_length(token):returnlen(token)==5you can create anapp_settingslike this:fromzero_settingsimportZeroSettingsapp_settings=ZeroSettings(key="APP",defaults={"TOKEN_CLASS":"app.utils.Token","TOKEN_VALIDATORS":["app.utils.validate_value","app.utils.validate_length",]},import_strings=["TOKEN_CLASS","TOKEN_VALIDATORS",])then you can importapp_settingsand use it:fromapp.settingsimportapp_settingstoken=app_setting.TOKEN_CLASS.get_token()forvalidatorinapp_settings.TOKEN_VALIDATORS:validator(token)Removed Settingsremoved settings can be configured like:fromzero_settingsimportZeroSettingsapp_settings=ZeroSettings(key="APP",defaults={"TOKEN":"token","URL":None,# you need to include the key in defaults too.},removed_settings={"URL":None,# or ""# or"URL":"URL had been removed from settings."})then if user tries to get theURLkey, aRuntimeErrorwill be raised.CacheZeroSettings cache results on first attempt to get a key, ifuse_cacheisTrue, as it willsetattrthat value to prevent later calls get anAttributeErrorfrom__getattribute__. to prevent this functionality, you can setuse_cachetoFalse.fromzero_settingsimportZeroSettingsapp_settings=ZeroSettings(key="APP",defaults={"TOKEN":"token"},use_cache=False)also there is a_clear_cache()method, which let you to clear cache manually. a simple use case can be in tests, when you want cached keys been cleared:fromdjango.testimportTestCasefromdjango.confimportsettingsasdjango_settingsfromapp.settingsimportapp_settings@override_settings(APP={"TOKEN":"new_token"})classMyTestCase(TestCase):deftest_something(self):print(app_settings.TOKEN)# new_tokenwithself.settings(APP={"TOKEN":"other_token"}):app_settings._clear_cache()print(django_settings.APP["TOKEN"])# other_tokenprint(app_settings.TOKEN)# other_tokenself.assertEqual(django_settings.APP["TOKEN"],app_settings.TOKEN)Contribution & TestsContributions are warmly accepted! change and make it better as you wish.ZeroSettings usetoxto run tests for different envs, to run tests:$ pip install tox
$ toxit will create and run tests for following Python and Django versions:Python: 3.5, 3.6, 3.7, 3.8, 3.9, 3.10
Django: 2.0, 2.2, 3.0, 3.1, 3.2, 4.1