{"id":26519975,"url":"https://github.com/aasmpro/django-zero-settings","last_synced_at":"2025-03-21T11:37:07.025Z","repository":{"id":57422715,"uuid":"397603008","full_name":"aasmpro/django-zero-settings","owner":"aasmpro","description":"a Django util for managing app settings.","archived":false,"fork":false,"pushed_at":"2022-09-21T14:47:30.000Z","size":57,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-01T08:11:59.159Z","etag":null,"topics":["django","hacktoberfest","python"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/django-zero-settings/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aasmpro.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-18T13:03:55.000Z","updated_at":"2025-02-10T00:30:16.000Z","dependencies_parsed_at":"2022-09-04T05:00:32.631Z","dependency_job_id":null,"html_url":"https://github.com/aasmpro/django-zero-settings","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aasmpro%2Fdjango-zero-settings","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aasmpro%2Fdjango-zero-settings/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aasmpro%2Fdjango-zero-settings/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aasmpro%2Fdjango-zero-settings/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aasmpro","download_url":"https://codeload.github.com/aasmpro/django-zero-settings/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244794381,"owners_count":20511510,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","hacktoberfest","python"],"created_at":"2025-03-21T11:37:05.192Z","updated_at":"2025-03-21T11:37:06.999Z","avatar_url":"https://github.com/aasmpro.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Tests](https://github.com/aasmpro/django-zero-settings/actions/workflows/tests.yml/badge.svg)\n![PYPI](https://github.com/aasmpro/django-zero-settings/actions/workflows/publish.yml/badge.svg)\n# Django Zero Settings\na Django util for managing app settings.\n\nwhen 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.\n\nthis 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.\n\nthis is actually how [django-rest-framework](https://github.com/encode/django-rest-framework/blob/master/rest_framework/settings.py) configures its settings, but with a few more features.\n\n## Supported Versions\n- 3.5 \u003c= Python \u003c= 3.10\n- 2.0 \u003c= Django \u003c= 4.1\n\n## Install\n```\npip install django-zero-settings\n```\n\n## Usages\ncreate a settings object like this:\n```python\nfrom zero_settings import ZeroSettings\n\napp_settings = ZeroSettings(\n    key=\"APP\",\n    defaults={\n        \"TOKEN\": \"token\"\n    },\n)\n```\n\nthen you can import `app_settings` and use it:\n```python\nfrom app.settings import app_settings\n\nprint(app_settings.TOKEN)\n```\n\n### Args\n`ZeroSettings` can get following args:\n\n| arg                  | desc                                                                                                                                                                                                                                                                                                                                                                                                      |\n| -------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| `key`                | the settings key which users will define settings with, is required and must be a string.                                                                                                                                                                                                                                                                                                                 |\n| `defaults`           | default settings for the app, required and must be a dict.                                                                                                                                                                                                                                                                                                                                                |\n| `user_settings`      | you can also set user settings manually, in this case, user settings with `key` will not be loaded. is optional and can be a dict.                                                                                                                                                                                                                                                                        |\n| `import_strings`     | a 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.\"`                                                                                                                                                                       |\n| `removed_settings`   | a 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. the `msg` part of dict is the error message. on `None` or empty strings, it generates the default message which is `\"The 'APP.KEY' setting has been removed.\"` |\n| `settings_doc`       | a string that locates the settings document path, the value will be used to generate `removed_settings` error with a message like: `\"Please refer to 'https://app.com/doc/settings' for available settings.\"`                                                                                                                                                                                             |\n| `use_cache`          | a boolean that defines whether to use cache or not                                                                                                                                                                                                                                                                                                                                                        |\n| `strict_defaults`    | a boolean that defines whether to be strict on defaults or not, if true, only default keys are valid in user settings                                                                                                                                                                                                                                                                                     |\n| `pre_check_defaults` | a boolean that defines whether to pre check defaults or not                                                                                                                                                                                                                                                                                                                                               |\n| `pre_check_imports`  | a boolean that defines whether to pre check imports or not                                                                                                                                                                                                                                                                                                                                                |\n| `pre_check_removed`  | a boolean that defines whether to pre check removed or not                                                                                                                                                                                                                                                                                                                                                |\n\n\n### Import Strings\nwith following class and methods at `app.utils`:\n```python\nclass Token:\n    @staticmethod\n    def get_token():\n        return \"token\"\n\ndef validate_value(token):\n    return token == \"token\"\n\ndef validate_length(token):\n    return len(token) == 5\n```\n\nyou can create an `app_settings` like this:\n```python\nfrom zero_settings import ZeroSettings\n\napp_settings = ZeroSettings(\n    key=\"APP\",\n    defaults={\n        \"TOKEN_CLASS\": \"app.utils.Token\",\n        \"TOKEN_VALIDATORS\": [\n            \"app.utils.validate_value\",\n            \"app.utils.validate_length\",\n        ]\n    },\n    import_strings=[\n        \"TOKEN_CLASS\",\n        \"TOKEN_VALIDATORS\",\n    ]\n)\n```\n\nthen you can import `app_settings` and use it:\n```python\nfrom app.settings import app_settings\n\ntoken = app_setting.TOKEN_CLASS.get_token()\nfor validator in app_settings.TOKEN_VALIDATORS:\n    validator(token)\n```\n\n### Removed Settings\nremoved settings can be configured like:\n```python\nfrom zero_settings import ZeroSettings\n\napp_settings = ZeroSettings(\n    key=\"APP\",\n    defaults={\n        \"TOKEN\": \"token\",\n        \"URL\": None, # you need to include the key in defaults too.\n    },\n    removed_settings={\n        \"URL\": None, # or \"\"\n        # or\n        \"URL\": \"URL had been removed from settings.\"\n    }\n)\n```\nthen if user tries to get the `URL` key, a `RuntimeError` will be raised.\n\n\n### Cache\nZeroSettings cache results on first attempt to get a key, if `use_cache` is `True`, as it will `setattr` that value to prevent later calls get an `AttributeError` from `__getattribute__`. to prevent this functionality, you can set `use_cache` to `False`.\n```python\nfrom zero_settings import ZeroSettings\n\napp_settings = ZeroSettings(\n    key=\"APP\",\n    defaults={\n        \"TOKEN\": \"token\"\n    },\n    use_cache=False\n)\n```\nalso 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:\n```python\nfrom django.test import TestCase\nfrom django.conf import settings as django_settings\nfrom app.settings import app_settings\n\n@override_settings(APP={\"TOKEN\": \"new_token\"})\nclass MyTestCase(TestCase):\n    def test_something(self):\n        print(app_settings.TOKEN)                            # new_token\n        with self.settings(APP={\"TOKEN\": \"other_token\"}):\n            app_settings._clear_cache()\n            print(django_settings.APP[\"TOKEN\"])              # other_token\n            print(app_settings.TOKEN)                        # other_token\n            self.assertEqual(django_settings.APP[\"TOKEN\"], app_settings.TOKEN)\n```\n\n\n## Contribution \u0026 Tests\nContributions are warmly accepted! change and make it better as you wish.\n\nZeroSettings use `tox` to run tests for different envs, to run tests:\n```\n$ pip install tox\n$ tox\n```\nit will create and run tests for following Python and Django versions:\n```\nPython: 3.5, 3.6, 3.7, 3.8, 3.9, 3.10\nDjango: 2.0, 2.2, 3.0, 3.1, 3.2, 4.1\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faasmpro%2Fdjango-zero-settings","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faasmpro%2Fdjango-zero-settings","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faasmpro%2Fdjango-zero-settings/lists"}