Skip to main content

Yet another application settings helper.

Project description

Yet another application settings helper.

Rationale

It’s a common practice to put a settings file in a distribution package with some predefined stuff which can be overridden later in global project’s settings. But there is also a good reason to separate all your settings within your apps just like you do so with common python code: models, views, etc. That’s not a big thing if your project doesn’t come with dozen apps, but if it does, flushing out non-project stuff is a good way to not mess things around and keep them way simple.

Quickstart

Lets say you have an email service application in your project dir and it stores some configuration in settings.py:

MYEMAILSERVICE_USERNAME = 'username'
MYEMAILSERVICE_PASSWORD = 'password'
...

It’s a big temptation to write short USERNAME, but you have to use prefixes to prevent conflicts with other application settings and tell collaborators this is for the email app. And then:

# emails/foo.py
from django.conf import settings.

service = Service(username=settings.MYEMAILSERVICE_USERNAME,
                  password=settings.MYEMAILSERVICE_PASSWORD,
                  ...)

Prefixes are everywhere. You always have to say MYEMAILSERVICE_ in every single place you need to access the settings. How about this one:

# Package settings in emails/conf.py
from pkgconf import Conf

class MyEmailService(Conf):
    USERNAME = 'username'
    PASSWORD = 'password'

    @property
    def DEBUG(self):
        return self.USERNAME.startswith('test_')

There is nothing more to say:

# emails/foo.py
# Note: your MyEmailService class becomes a module,
# you import it directly
from . import conf

service = Service(username=conf.USERNAME, password=conf.PASSWORD, ...)

django-pkgconf wraps your application settings and provides a handy shortcut.

But what about test or dev settings? Just define them like you always do (configuration class name becomes a prefix).

Old style:

# local_settings.py
MYEMAILSERVICE_USERNAME = 'test_username'

Cool style (django-configurations way):

# settings.py
class Prod(Configuration):
    # No email service settings at all
    pass

class Test(Prod):
    MYEMAILSERVICE_USERNAME = 'test_username'

It looks for the required setting in django’s configuration file first and returns original value if it’s not overridden:

# emails/foo.py
from . import conf

conf.USERNAME  # 'test_username'
conf.PASSWORD  # 'password' - returns original value
conf.DEBUG  # True

Since 0.3.0 mixins are supported:

from pkgconf import Conf

class FacebookMixin:
    FACEBOOK_APP_ID = 'foo'
    FACEBOOK_SECRET = 'bar'


class TwitterMixin:
    TWITTER_APP_ID = 'foo'
    TWITTER_SECRET = 'bar'


class InstagramMixin:
    INSTAGRAM_APP_ID = 'foo'
    INSTAGRAM_SECRET = 'bar'


class SocialAppConf(FacebookMixin, TwitterMixin, InstagramMixin, Conf):
    DEBUG = True

Installation

Install the package:

pip install -U django-pkgconf

Read the quickstart.

Compatability

Build Status Codecov

Tested on py 3.5, 3.6, 3.7, 3.8 with django 1.11, 2.2, 3.0.

Powered siblings

There are more advanced apps with (probably) some extra (better?) options: django-appsettings, django-appconf, etc. The reason I’ve published this one is those apps are too big and tricky to do this little work, so you might prefer them instead.

Limitations

  • Due to the code simplicity, property descriptor is used to get data from the settings. That means you can not set (or change) configuration values in action. I don’t know why you would do that, but I have to warn you.

  • Since your app’s settings are defined in separate file, they are not accessible via django.conf.settings (until you override them in your project settings). This application doesn’t create backward compatibility links. You should always use package configuration module.

Changelog

v0.4.0

  • Python 3 only

v0.3.0

  • Added mixins support.

v0.2.1 - 0.2.2

  • Added import * support.

  • __prefix__ is generated automatically if not presented in class. That may help to build graceful exceptions like 'foo_value' was not found in MYAPP_FOO_SETTING.

v0.2.0

  • Added __prefix__ attribute to support prefix-names with underscores.

  • Added instance method and property support.

  • Backward incompatible change: functions must have self as the first argument now.

v0.1.0

  • First public release

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_pkgconf-0.5.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_pkgconf-0.5.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file django_pkgconf-0.5.0.tar.gz.

File metadata

  • Download URL: django_pkgconf-0.5.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for django_pkgconf-0.5.0.tar.gz
Algorithm Hash digest
SHA256 41819d1fab69430d10ae8e361629e69208e3de7dce603563ff18370720bfd2cd
MD5 50ce11da30220b179431c3fe237cf0c3
BLAKE2b-256 46ab014febcb18385ebf4d030f1d0c70db884d1fcdca7034269964f5b5e1de2b

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_pkgconf-0.5.0.tar.gz:

Publisher: publish.yml on byashimov/django-pkgconf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_pkgconf-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: django_pkgconf-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for django_pkgconf-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 67f818f6e70788c47e1f245e118ab35cb853905a930e53ddb94831c768df8640
MD5 581e39c2e2d55022131fa3cca488ca4d
BLAKE2b-256 e8928c2ebfdf6187208392cce970d7af3bb88104f4ceedeb0926321ebb00f1de

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_pkgconf-0.5.0-py3-none-any.whl:

Publisher: publish.yml on byashimov/django-pkgconf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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