Skip to main content

A Django util for managing app settings.

Project description

Django Zero Settings

a 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 how django-rest-framework configures its settings, but with a few more features.

Install

pip install django-zero-settings

Usages

create a settings object like this:

from zero_settings import ZeroSettings

app_settings = ZeroSettings(
    key="APP",
    defaults={
        "TOKEN": "token"
    },
)

then you can import app_settings and use it:

from app.settings import app_settings

print(app_settings.TOKEN)

Args

ZeroSettings can get following args:

arg desc
key the settings key which users will define settings with, is required and must be a string.
defaults default settings for the app, required and must be a dict.
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.
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."
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."
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."
use_cache a boolean that defines whether to use cache or not
strict_defaults a boolean that defines whether to be strict on defaults or not, if true, only default keys are valid in user settings
pre_check_defaults a boolean that defines whether to pre check defaults or not
pre_check_imports a boolean that defines whether to pre check imports or not
pre_check_removed a boolean that defines whether to pre check removed or not

Import Strings

with following class and methods at app.utils:

class Token:
    @staticmethod
    def get_token():
        return "token"

def validate_value(token):
    return token == "token"

def validate_length(token):
    return len(token) == 5

you can create an app_settings like this:

from zero_settings import ZeroSettings

app_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 import app_settings and use it:

from app.settings import app_settings

token = app_setting.TOKEN_CLASS.get_token()
for validator in app_settings.TOKEN_VALIDATORS:
    validator(token)

Removed Settings

removed settings can be configured like:

from zero_settings import ZeroSettings

app_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 the URL key, a RuntimeError will be raised.

Cache

ZeroSettings 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.

from zero_settings import ZeroSettings

app_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:

from django.test import TestCase
from django.conf import settings as django_settings
from app.settings import app_settings

@override_settings(APP={"TOKEN": "new_token"})
class MyTestCase(TestCase):
    def test_something(self):
        print(app_settings.TOKEN)                            # new_token
        with self.settings(APP={"TOKEN": "other_token"}):
            app_settings._clear_cache()
            print(django_settings.APP["TOKEN"])              # other_token
            print(app_settings.SETTING1)                     # other_token
            self.assertEqual(django_settings.APP["TOKEN"], app_settings.SETTING1)

Contribution & Tests

Contributions are warmly accepted! change and make it better as you wish.

ZeroSettings use tox to run tests for different envs, to run tests:

$ pip install tox
$ tox

it will create and run tests for following Python and Django versions:

Python: 3.5, 3.6, 3.7, 3.8, 3.9
Django: 2.0, 2.2, 3.0, 3.1, 3.2

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-zero-settings-0.1.7.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

django_zero_settings-0.1.7-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file django-zero-settings-0.1.7.tar.gz.

File metadata

  • Download URL: django-zero-settings-0.1.7.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for django-zero-settings-0.1.7.tar.gz
Algorithm Hash digest
SHA256 e417cbc1cb8e592f529743fbe3df5e19ffb6824fcc6efc91dca06683855e0ec4
MD5 b785db2390ef57401185d08a0102ae9b
BLAKE2b-256 a561d623c86746eacb3c79defc9194d65ae0cb33ad3e975e54a56307f8db1517

See more details on using hashes here.

File details

Details for the file django_zero_settings-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: django_zero_settings-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for django_zero_settings-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 60b4eba63a10ad8892bb33ea7baa7e9a04ef308f9701ea58aff465591e42fcbb
MD5 85d208ef087f8d52957f68791b0aac6c
BLAKE2b-256 9c9afde8647afdbe192e6b1107a6cf52e3a9e67e69dccdb2a633aa4fc5461832

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page