Skip to main content

Settings library for human beings

Project description

Pysettings YAML

This library extends over the concepts of Decouple and Split Settings to create a library that is simple to use and expresses your settings in a yaml file for higher expressivity.

Installation

pip install pysettings-yaml

Usage

Define a YAML file with the definition of your settings, for example, a settings.yaml in the same path as your settings module:

settings:
  SAMPLE_SETTING_BOOL:  # Each entry inside the settings section is the name of your setting
    origins:
      - name: env
      - name: direct
        value: true
  SAMPLE_SETTING_STR:
    origins:
      - name: direct
        value: banana
      - name: env

Then you can use this file like this in a settings.py:

from split_settings.tools import optional
from pathlib import Path

from pysettings_yaml import get_config

BASE_DIR = Path(__file__).parent

setting_files = [
    BASE_DIR / "settings.yaml",
]


config = get_config(setting_files)

SAMPLE_SETTING_BOOL = config("SAMPLE_SETTING_BOOL", cast=bool)
SAMPLE_SETTING_STR = config("SAMPLE_SETTING_STR")

print(SAMPLE_SETTING_BOOL)
print(SAMPLE_SETTING_STR)  # This will print banana

By default the library supports two origin names: env (to receive the value of your setting using the decouple library) or direct (to provide a constant value), but you can add your own providers easily (refer to the "Adding custom providers" section).

Origin are evaluated in strict order. In those examples, SAMPLE_SETTING_BOOL will evaluate first the env origin and, if that value is None, it will process the next origin, which would be direct. In contrast, for SAMPLE_SETTING_STR, the first origin is the direct origin. Since direct can never return None, the env origin will never be evaluated.

Overriding settings

You can define several setting files to be processed in order. All of them will be merged together, so any subsequent setting file will override the settings with the same name defined in a previous file of the list. Very useful if you need to define different origins for your settings depending on your deployment environment:

from split_settings.tools import optional
from pathlib import Path
import os

from pysettings_yaml import get_config
ENVIRONMENT = os.environ.get("ENVIRONMENT", "dev")

BASE_DIR = Path(__file__).parent

setting_files = [
    BASE_DIR / "settings.yaml",
    BASE_DIR / f"settings.{ENVIRONMENT}.yaml",
    optional(BASE_DIR / "settings.nonexistent.yaml")
]


config = get_config(setting_files)

SAMPLE_SETTING_BOOL = config("SAMPLE_SETTING_BOOL", cast=bool)
SAMPLE_SETTING_STR = config("SAMPLE_SETTING_STR")

Any path wrapped with the optional function will not raise an exception should that file not exist in the path you specify.

Adding custom providers

If you need other providers (for example for getting your settings from a vault like Amazon SSM) you can implement your own providers like this:

from typing import Union, Optional

from pysettings_yaml.providers.interfaces import SettingsProvider, OriginModel, NoValue


class AWSModel(OriginModel):
    path: str
    decrypt: bool


class SampleAWSSettingsProvider(SettingsProvider):
    name = "aws"
    schema = AWSModel

    def get(
        self, setting_name: str, origin_data: AWSModel
    ) -> Union[Optional[str], NoValue]:
        return f"path: {origin_data.path}, decrypt: {origin_data.decrypt}"

Just make sure to:

  • Provide the name and a pydantic schema
  • Implement the get method

Then you can obtain your config providing your SettingProvider and use it in your yaml:

config = get_config(setting_files, additional_providers=[SampleAWSSettingsProvider()])
settings:
  SAMPLE_SETTING:
    origins:
      - name: aws
        path: some/path
        decrypt: true
      - name: env

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

pysettings-yaml-0.1.2.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

pysettings_yaml-0.1.2-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file pysettings-yaml-0.1.2.tar.gz.

File metadata

  • Download URL: pysettings-yaml-0.1.2.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.10 Linux/5.8.0-55-generic

File hashes

Hashes for pysettings-yaml-0.1.2.tar.gz
Algorithm Hash digest
SHA256 4b891a0f58f6292dfa94624b9d213015ec5e03e40fd2529a6c4b4d8a6ae8ecb8
MD5 8541824798cf2dd0d8ad8409464d517a
BLAKE2b-256 a25f8a8a3388e86833330395a2b2d731fb1487834b1793161f1b3d76f7221d32

See more details on using hashes here.

File details

Details for the file pysettings_yaml-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pysettings_yaml-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.10 Linux/5.8.0-55-generic

File hashes

Hashes for pysettings_yaml-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a90a7a682f0bd3112dbbd6b4281a3d606b849eb63ae1f1775ba5f891282c3f26
MD5 e6370d84ed4f2d652b0f828f3138ac14
BLAKE2b-256 3f8bdca73de908d8ccfc27e38a44db59c585324141d54444115f1ccae5d9e21b

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