ConfigOverloader¶
Motivation¶
I often use different config files depend not only on “env”, but also “role”, “node” I save each config in each repository. But I found it frustrating because the most part is the same. It should be saved 1 repository.
Idea¶
As the name says, “overloading” is main idea. Originally python’s ConfigParser has overloading function. This library just helps to glob files based on directory and file name rules.
Usage¶
Here is basic example. If your directory structure is like this
directory structure:
├── example_a.ini
├── env
│ └── production
│ └── example_a.ini
├── role
│ └── read_api
│ └── example_a.ini
└── node
└── xxx101
└── example_a.ini
import configoverloader as co
from ConfigParser import ConfigParser
# register context
co.register_context(
env="production",
role="read_api",
node="xxx101")
# get files
path_list = co.get_filenames( "YOUR_BASE_DIR/example_a.ini")
print(path_list)
# Output:
# [ 'YOUR_BASE_DIR/example_a.ini',
# 'YOUR_BASE_DIR/env/production/example_a.ini',
# 'YOUR_BASE_DIR/env/production/example_a.ini',
# 'YOUR_BASE_DIR/node/xxx101/example_a.ini']
#
# then you can call ConfigParser.read()
cfg = ConfigParser()
cfg.read(path_list)
You can pass stream instead of filename like this
with open( "YOUR_BASE_DIR/example_a.ini") as fp:
path_list = co.get_filenames(fp)
LICENSE¶
MIT