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
File details
Details for the file spconfigreader-0.3.3.tar.gz
.
File metadata
- Download URL: spconfigreader-0.3.3.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/46.1.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c5fb28cd26fcc67bee7f8817f8e146d072fb377c72fca96fd4ccd81d3c944ca8 |
|
MD5 | 3facdbe1ace5b24dc38ee5f9810693b1 |
|
BLAKE2b-256 | 5be7c4a30491f13a51f9242321ab91d2f9eed93aa90f535333be575a76ab8b74 |