Giter Club home page Giter Club logo

pyramid_settings's Introduction

pyramid_settings

Replaces most of your configuraton ini-files with YAML or JSON that can be reused or extended.

So why would you want this?

  • If you're somewhat worried about that ini-file that seems to contain more and more sensitive information required by each package you install.

  • In case you really dislike ini-files and have found some YAML love.

  • You're starting to realize that manually editing ini-files, switching on and off components is getting quite tedious.

  • You want to script deployment easier.

  • You don't want to muck about with converting string values passed to settings.

  • You have some other file format you want to add an extension for.

Basic usage

The most basic usage would be to tell pyramid to include pyramid_settings, and then direct the rest of the configuration to that file.

Your paster-ini file look something like this:

pyramid.includes =
    pyramid_settings
    
pyramid_settings.files =
    basics.yaml
    development.yaml
    var/secretstuff.yaml

Order matters here, so development.yaml will overwrite similar values within basics.yaml.

Your configuration files will be added to the settings dict present at registry.settings within Pyramid.

Values prefixed with pyramid_settings won't be overwritten or included in the settings var, since they're used by this package.

Directives within loaded files

The included settings file themselves may contain information used load or include other packages as well.

pyramid_settings.files

Other settings files to load and include. Uses relative path from the file that references them.

pyramid_settings.includes

Other packages to include after the current file has been loaded.

So your development.yaml might look something like this

pyramid_settings.files:
  - redis.yaml

And then the referenced file redis.yaml

redis.sessions.secret: <somethingsecret>
redis.sessions.timeout = 600000

pyramid_settings.includes: pyramid_redis_sessions

The pyramid_redis_sessions package will be loaded after the injection of the required settings.

Advanced: Writing your own loader

In case you want other formats than yaml and json, simply subclass pyramid_settings.models.BaseSettingsLoader.

This simple example should give you an idea.

import json

from pyramid_settings.models import BaseSettingsLoader


class MySpecialJSONLoader(BaseSettingsLoader):
    """ A custom loader
    """

    def load(self, filename):
        """ Load data from 'filename' and pass it
            to the update function as a dict.
        """
        with open(filename) as f:
            data = json.load(f)
        self.update(data, filename)

To make it discoverable by the pyramid_settings package, register it as a named adapter. The name in this case is the same thing as the file ending.

So when including your package:

def includeme(config):
    config.registry.registerAdapter(MySpecialJSONLoader, name='specialjson')

The above example would use this adapter for any file ending with 'specialjson' for instance 'mysettings.specialjson'.

Note that this must be done before including pyramid_settings.

If you need to override any of the included json and yaml adapters, you either need to run config.commit() or config.registry.unregisterAdapter first. (Since Pyramid has detection for conflicting configuration)

See the Pyramid docs for more information.

Bugs or suggestions?

https://github.com/robinharms/pyramid_settings

pyramid_settings's People

Contributors

robinharms avatar

Watchers

 avatar  avatar

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.