Skip to main content

Yaml Settings Configuration Module

Project description

A Settings Configuration Module.

https://travis-ci.org/KyleJamesWalker/yamlsettings.svg?branch=master https://codecov.io/gh/KyleJamesWalker/yamlsettings/branch/master/graph/badge.svg

A library to help manage project settings, without having to worry about accidentally checking non-public information, like api keys. Along with environment variable support.

Example setup

Python Code - Base Functions

import yamlsettings


app_settings = yamlsettings.load('defaults.yaml')
app_settings.update(yamlsettings.load('settings.yaml'))
yamlsettings.update_from_env(app_settings)
user = app_settings.myproj.databases.primary_sql.user

defaults.yml - Default Settings for Project (tracked)

---
# Program Defaults, do not edit this file!!!
# All values should be overridden in the following ways:
# 1. In the 'settings.yaml' file.
# 2. With environment variables. Example myproj.databases.primary_sql.user can
#    be overridden with MYPROJ_DATABASES_PRIMARY_SQL_USER.
myproj:
  databases:
    primary_sql:
      user: my_user
      passwd: password_here
      host: db-bouncer-01.postgres.com:5432
      db: postgres
      compress: true
      engine: postgresql
    splunk:
      user: splunk_user_here
      passwd: password here
      host: splunk.com
      port: 8089
    redis:
      redis_host: 127.0.0.1
      redis_port: 6379
  flask_config:
    DEBUG: False
    SECRET_KEY: hard key to guess and keep values secret
  debug_sql: false
  debug_profiler: false
  cache_routes: true
  logging_config:
    version: 1
    disable_existing_loggers: False
    formatters:
      light:
        format: '%(asctime)s [%(levelname).1s] %(name)s: %(message)s'
        datefmt: '%Y-%m-%d %H:%M:%S'
      verbose:
        format: '%(asctime)s %(levelname) 8s(%(name)s): %(message)s'
        datefmt: ''
    handlers:
      console:
        class: logging.StreamHandler
        level: DEBUG
        formatter: light
        stream: ext://sys.stdout
      slack:
        class: api.slackLogHandler.BufferingSlackWebHookHandler
        level: INFO
        formatter: light
        capacity: 100
        organization: KyleJamesWalker
        token: need_this
        channel: '#services'
        username: my-proj-logger
        icon_emoji: ':happy_panda:'
      noid:
        class: logging.NullHandler
    loggers:
      requests:
        level: NOTSET
        handlers: [noid]
        propagate: no
    root:
      level: NOTSET
      handlers:
        - console

settings.yml - Custom Settings (untracked)

---
myproj:
  databases:
    primary_sql:
      user: root
      passwd: god
    splunk:
      user: real_user
      passwd: pa$$word
  flask_config:
    SECRET_KEY: sdfasjksdfASFAS23423f@#$%!$#VR@%UQ%
  logging_config:
    handlers:
      slack:
        token: 123243294832104981209
    root:
      handlers:
        - console
        - slack

Example package resource loading

"""Parameters that can be passed are:
 resource: The resource to load from the package (default: settings.yaml)
 env: When set the yamldict will update with env variables (default: true)
 prefix: Prefix for environment loading (default: None)
 persist: When set the yamldict will only be loaded once. (default: true)
"""
yamlsettings.load('package://example')
yamlsettings.load('package://example?resource=diff.yaml')
yamlsettings.load('package://example?prefix=MY_FUN&persist=false')

Plugins

This project also supports plugins. The base project has two plugins:

  • file: Loads from the file system.

  • package: Loads settings from a package resource.

Example Plugin:

setup.py

from setuptools import setup

setup(
    name='yamlsettings-example',
    version='1.0.0',
    author='Kyle Walker',
    author_email='KyleJamesWalker@gmail.com',
    description='Quick Example',
    requirements=['yamlsettings'],
    py_modules=['yamlsettings_example'],
    entry_points={
        'yamlsettings10': [
            'ext = yamlsettings_example:ZxcExtension',
        ],
    },
)

yamlsettings_example.py

from yamlsettings.extensions.base import YamlSettingsExtension


class ZxcExtension(YamlSettingsExtension):
    """Quick Example Plugin

    Standard file opener, but will merge in values passed to kwargs
    """
    protocols = ['zxc']

    @classmethod
    def load_target(cls, scheme, path, fragment, username,
                    password, hostname, port, query,
                    load_method, **kwargs):
        full_path = (hostname or '') + path
        obj = load_method(open(full_path, **query))

        # Load all returns a generator list of configurations
        many = isinstance(obj, types.GeneratorType)
        obj = list(obj) if many else obj

        if many:
            for x in obj:
                x.update(kwargs)
        else:
            obj.update(kwargs)

        return obj

usage

import yamlsettings
yamlsettings.load("zxc://defaults.yaml", foo='bar')

# Note: this is automatically detected when the extension is installed
# alternatively the extension can be manually registered with:
yamlsettings.registry.add(ZxcExtension)

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

yamlsettings-2.1.2.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

yamlsettings-2.1.2-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file yamlsettings-2.1.2.tar.gz.

File metadata

  • Download URL: yamlsettings-2.1.2.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for yamlsettings-2.1.2.tar.gz
Algorithm Hash digest
SHA256 c86da6bc70e1a9051e582e47b273b703d8ecb49d0850da04c7774680d85aa22a
MD5 710401ee4080875735f3e0c3005276bd
BLAKE2b-256 00f3b6197c94ae1296855ed8e8ac85004d41a01990a9bf3f7cb32f2c854fc3cf

See more details on using hashes here.

File details

Details for the file yamlsettings-2.1.2-py3-none-any.whl.

File metadata

  • Download URL: yamlsettings-2.1.2-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for yamlsettings-2.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 848399ff432a4c998f6dde5778d6669ef58a0c7d9aa4187621cec6a3ffa1bfa0
MD5 f8df7c864e62fa1bf07ca147c6b015f8
BLAKE2b-256 2a6157435f84f3bb3c68c958be0c47cb696cbf2b762df3888b0c9d59fd1c5bb4

See more details on using hashes here.

Supported by

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