Skip to main content

Handle configuration for multiple libraries

Project description

Gifnoc

Gifnoc is a unified configuration system for Python modules.

The main objective of gifnoc is to unify configuration files, environment variables and command-line options, across multiple modules. For example, module A and module B can both define their own configuration models through gifnoc, map some environment variables to keys in that configuration, and then you may configure A and B in the same file.

Gifnoc also aims to validate configuration through a typed model based on dataclasses and implemented by the serieux package, a dependency of gifnoc.

Features

  • Typed configuration using dataclasses and serieux
  • Use a single configuration tree for multiple libraries
  • Multiple configuration files can be easily merged
  • Easily embed configuration files in each other

Example

main.py

from dataclasses import dataclass, field
import gifnoc

@dataclass
class User:
    name: str
    email: str
    admin: bool

@dataclass
class Server:
    # Server post
    port: int = 8080
    # Server hostname
    host: str = "localhost"
    # List of users
    users: list[User] = field(default_factory=list)

# Define a configuration key
server_config = gifnoc.define("server", Server)

if __name__ == "__main__":
    # Load configuration from the file in $APP_CONFIG, if it exists
    # set_sources overrides any previous configuration
    # Each source can be a dictionary, ${envfile:...}, a Path object
    gifnoc.set_sources("${envfile:APP_CONFIG}")

    # Overlay configuration from environment variables
    # add_overlay adds on top of existing configuration
    gifnoc.add_overlay({
        "server.port": "${env:APP_PORT}",
        "server.host": "${env:APP_HOST}",
    })

    # Overlay configuration with --config, add options with mapping
    # --port will automatically use the type and documentation of Server.port
    gifnoc.cli(mapping={"server.port": "--port"})

    # server_config dynamically updates as sources/overlays are changed
    print("Port:", server_config.port)
    print("Host:", server_config.host)

    # You can apply local changes with a context manager, e.g. for testing:
    with gifnoc.overlay({"server.port": 90909}):
        print("Port:", server_config.port)  # Port: 90909

config.yaml

server:
  port: 1234
  host: here
  users:
    - name: Olivier
      email: ob@here
      admin: true
    # You can write a file path instead of an object
    - mysterio.yaml

mysterio.yaml

name: Mysterio
email: me@myster.io
admin: false

Usage:

python main.py --config config.yaml
APP_CONFIG=config.yaml python main.py
APP_PORT=8903 python main.py --config config.yaml
python main.py --config config.yaml --port 8903

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

gifnoc-0.6.3.tar.gz (45.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gifnoc-0.6.3-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file gifnoc-0.6.3.tar.gz.

File metadata

  • Download URL: gifnoc-0.6.3.tar.gz
  • Upload date:
  • Size: 45.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for gifnoc-0.6.3.tar.gz
Algorithm Hash digest
SHA256 7c795da2459772911b381929d7b2d366b2b6007e6a367652d8f68f26414e9c98
MD5 8b848545b4b2d2f17481293d13698a91
BLAKE2b-256 bc764e249bfc8f530b3fefecefa50871639f75d2bec156a7e2ac9971e9abec1f

See more details on using hashes here.

Provenance

The following attestation bundles were made for gifnoc-0.6.3.tar.gz:

Publisher: publish.yml on breuleux/gifnoc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gifnoc-0.6.3-py3-none-any.whl.

File metadata

  • Download URL: gifnoc-0.6.3-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for gifnoc-0.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 05b71f27ec2a9f6515aeecf2faa6377f6e5cf86461f2aaff9340726135eef4ff
MD5 b16b4d3949238d36f80b74f4ebd3b6b7
BLAKE2b-256 c895797b37187bdc0dbde7656318a98883a7f46a5925c736a827bf3f4916e44c

See more details on using hashes here.

Provenance

The following attestation bundles were made for gifnoc-0.6.3-py3-none-any.whl:

Publisher: publish.yml on breuleux/gifnoc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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