Skip to main content

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

Metadata

Release files for pysettings-yaml 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pysettings-yaml 0.1.2
File Size Uploaded Provenance Actions
pysettings-yaml-0.1.2.tar.gz 5.8 kB Without provenance Details for pysettings-yaml-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pysettings-yaml 0.1.2
File Interpreter ABI Platform Actions
pysettings_yaml-0.1.2-py3-none-any.whl Python 3 none any Details for pysettings_yaml-0.1.2-py3-none-any.whl

Total release size: 12.3 kB

Release files / pysettings-yaml-0.1.2.tar.gz

Download URL pysettings-yaml-0.1.2.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4b891a0f58f6292dfa94624b9d213015ec5e03e40fd2529a6c4b4d8a6ae8ecb8
BLAKE2b-256 checksum
How to use checksums
a25f8a8a3388e86833330395a2b2d731fb1487834b1793161f1b3d76f7221d32
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.8.10 Linux/5.8.0-55-generic

Release files / pysettings_yaml-0.1.2-py3-none-any.whl

Download URL pysettings_yaml-0.1.2-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a90a7a682f0bd3112dbbd6b4281a3d606b849eb63ae1f1775ba5f891282c3f26
BLAKE2b-256 checksum
How to use checksums
3f8bdca73de908d8ccfc27e38a44db59c585324141d54444115f1ccae5d9e21b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.8.10 Linux/5.8.0-55-generic

Security for pysettings-yaml 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page