Skip to main content

A utitlity to read config from yaml-files and environment (like Spring for Java)

Project description

Spring Confing Reader

Table of Contents

General Information

There are several config readers for Python but no one matches my requirements. so, i created my own.

The focus is to read configuration from a YAML file with the option to override it with environment variables, which is needed for working with Docker.

Inspiration was the configuation in the Java framework Spring, which leads to the name.

Configfile

As default, {root}/config.yml is the configfile. This can be changed with the following code before the first use.

configreader.__configfile__ = "path/to/file.yml"

Function overview

get(key: str, default: str = None)
getObject(obj: object, path: str = None)

Usage

object

first, create a class with all fields you want to read. They need either a type annotation or must be initialized. Subclasses must always be initalized.

The variable name should match the name in the YAML file.

At this point, default values can also be defined. If a value is given in the YAML, it will be overrwritten.

An additional field it the path field, wich contains the root path in the YAML file. This can also be specified in the getObject() method later

As example:

config.yml

datasource:
  server: "mssql.server"
  test:
    val1: "foo"
    val2: "bar"

code

class Test:
    val1: str
    val2: str

class Datasource:
    __path__: str = "datasource"

    server: str
    port: int = 1433
    protocol: str = "tcp"
    test = Test()

To read the values, use the getObject() method in one of this ways:

datasource = configreader.getObject(Datasource())
datasource = Datasource()
configreader.getObject(datasource)

single entry

To get only a single entry, use the get() function

val = get("path.of.entry")

second parameter is a default value if no entry is found in the YAML file.

Override with Environment Vairables

To override a entry in the YAML, set an environment variable with the same key but with upper letters and with _ instead of .

Example:

path.to.entry -> PATH_TO_ENTRY

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

spconfigreader-0.3.3.tar.gz (3.8 kB view hashes)

Uploaded Source

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