spconfigreader

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


License
MIT
Install
pip install spconfigreader==0.3.3

Documentation

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