Skip to main content

Load and validate configurations using jsonschema

Project description

Schema config

PyPI GitLab PyPI - Implementation PyPI - Python Version PyPI - Wheel PyPI - Status

Install

Requires CPython/PyPy 3.6 minimum

pip3 install schema-config~=[version]

Warning: In this early versions (0.X.Y) minor versions may introduce major breaking changes!

Usage

Loading a configuration

from pkg_resources import resource_string
from os import environ

from schema_config import Configurator

schema: bytes = resource_string(__name__, "config.schema.json")
factory: Configurator = Configurator.from_string(schema)

# The order is important, the first file will be loaded
if "CONFIG" in environ:
    # loads the configuration from a yaml file (typings come from YAML)
    factory.add_file(environ["CONFIG"])
else:
    # Search for your programm's configurations
    factory.add_file("/etc/programm/config.yaml")
    factory.add_file("./programm.yaml")

# Get your configuration object
cfg = factory.load_config()

Specifying a configuration schema

schema-config is configured using jsonschema.

A possible schema could be:

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "properties": {
        "database": {
            "type": "object",
            "properties": {
                "host": {
                    "type": "string"
                },
                "port": {
                    "type": "number"
                },
                "password": {
                    "type": "string"
                }
            },
            "required": ["host"]
        }
    },
    "required": ["database"]
}

Configuring your program

With the examples above a user could configure the program like this: /etc/program/config.yaml:

database:
    host: "example.com"
    port: $json{DB_PORT}
    password: $string{DB_PASSWORD}

With an environment variable (currently quotes are needed):

DB_PORT="9999"
DB_PASSWORD="test"

Both would be merged in a Configuration object similar to:

{
    "database": {
        "host": "example.com",
        "port": 9999,
        "password": "test"
    }
}

The Configurator class

Creation

From a file

factory = Configurator.from_file("<file name>")

From a string

factory = Configurator.from_string("<json schema as a string>")

From a dict

factory = Configurator(json_schema_as_a_dict)

Adding files

factory.add_file("<file path>")

Warning: The order is important! The first one will be loaded

Getting the configuration

cfg = factory.load_config()  # type: Configuration

The Configuration class

The Configuration class is something like an easier dict.

Getting items

With KeyError if not available

database = cfg["database"]
host = cfg["database.host"]

With default:

host = cfg.get("database.host", "localhost")
password = cfg.get("database.password")  # Default: None

Checking for items

cfg.has("database.host")
"database.host" in cfg

Setting items

cfg["database"] = {"host": "example.com"}
cfg["database.host"] = "example.com"

Other useful abilities

Creating a new Configuration

cfg = Configuration(some_dict)

Getting a dict out of it

dict_cfg = cfg.raw

Issues & Suggestions

If you have any issues, suggestions, licensing problems, etc. just open an issue in the gitlab repo

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for schema-config, version 0.3.0
Filename, size File type Python version Upload date Hashes
Filename, size schema_config-0.3.0-py3-none-any.whl (5.0 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size schema-config-0.3.0.tar.gz (5.2 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page