Code Monkey home page Code Monkey logo

django-settings-toml's Introduction

Django Settings Toml

This project is used to configure Django projects using a Toml configuration file. This project chooses Toml over other configuration language becuase of it's simplicity and small specification. It is easier to understand and looks very much like INI-style config. We couldn't use INI-style because it is difficult to repsent data structures like dictionaries and lists, and there is no support for nesting.

Changelog

0.0.4

  • Add support for Substitution in maps and lists.
  • Fix loading settings where values are non-string which caused TypeError in v0.0.3.

0.0.3

  • Fix packaging for the new release.

0.0.2

  • Add support for variable substitution using ${VAR} syntax in values.

Usage

To use this in your Django project, Add the following to your settings.py file:

# settings.py
from django_settings_toml import load_settings

load_settings(__name__, ['/etc/project.toml', '~/.project.toml'])

Then, you can run your django project like this:

$ DJANGO_SETTINGS_MODULE=project.settings django-admin runserver

Variable Substitution

Values can refer to other keys defined in the same file using ${VAR} syntax. For example:

# /etc/project.toml

HOSTNAME = 'domain.local'

EMAIL1 = 'aperson@${HOSTNAME}'

EMAIL2 = 'bperson@${HOSTNAME}'

In the above example, the value of EMAIL1 = '[email protected] and similar for EMAIL2.

If they variables that are being referred to aren't defined, the template is left as-is and doesn't raise any errors. For example, in the above example, if HOSTNAME wasn't defined, the value will be EMAIL1 = 'aperson@${HOSTNAME}.

Example Settings

# /etc/project.toml
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'change-this-on-your-production-server'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = false

ADMINS = [
['Mailman Suite Admin', 'root@localhost'],
]

# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'django_q',
'allauth',
]

MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
]

ROOT_URLCONF = 'project.urls'

WSGI_APPLICATION = 'project.wsgi.application'

HOSTNAME = 'project.local'

EMAIL_NAME = 'project1@${HOSTNAME}'

[DATABASES.default]
ENGINE = 'django.db.backends.sqlite3'
NAME = 'mailmansuite.db'

[LOGGING]
version = 1
disable_existing_loggers = false

[LOGGING.filters.require_debug_false]
'()' = 'django.utils.log.RequireDebugFalse'

[LOGGING.handlers.mail_admins]
level = 'ERROR'
filters = ['require_debug_false']
class =  'django.utils.log.AdminEmailHandler'

[LOGGING.handlers.file]
level = 'INFO'
class = 'logging.handlers.WatchedFileHandler'
filename =  'logs/mailmansuite.log'
formatter = 'verbose'

[LOGGING.loggers."django.request"]
handlers = ['mail_admins', 'file']
level = 'ERROR'
propagate = true

[LOGGING.formatters.verbose]
format = '%(levelname)s %(asctime)s %(process)d %(name)s %(message)s'

Gotchas

  • Please make sure that you have writtena valid Toml, you can use TOML Validator or tomlcheck tools to validate the toml file.
  • Please make sure that all smiple KEY = value pairs are in the root namespace (above any [section]) so that they don't get swallowed under one of the maps or arrays. Previously, we have seen ImproperlyConfiguredError for missing keys that were actually defined in the toml file.

LICENSE

The contents of this project is licensed under Apache License 2.0. Please see the LICENSE file for a complete copy of license text.

django-settings-toml's People

Contributors

jayvdb avatar maxking avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

jayvdb csmatar

django-settings-toml's Issues

Add support for overriding specific parts of a Dict

It should be possible to override default values without overriding the whole thing:

# settings.py

LOGGING = {
    'version': 1,
    'handlers: {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler'
        }
    }
}
# /etc/settings.toml

LOGGING.version = 2

Add support for variable substitution

We need something like references and substitution in the configuration language.

[LOGGING.file.handlers]
path: '$BASE_DIR/mailmansuite.log'

Where BASE_DIR is something defined and would default to empty string.

non-string values generate errors

Because of the way we do string substituion, any non-string values generate a TypeError.

  File "/home/maxking/.virtualenvs/test3/lib/python3.7/site-packages/django_settings_toml-0.0.3-py3.7.egg/django_settings_toml.py", line 44, in load_settings
  File "/home/maxking/.virtualenvs/test3/lib/python3.7/site-packages/django_settings_toml-0.0.3-py3.7.egg/django_settings_toml.py", line 17, in update_settings
  File "/usr/lib/python3.7/string.py", line 161, in safe_substitute
    return self.pattern.sub(convert, self.template)
TypeError: expected string or bytes-like object

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.