I wish to use Python code for config files. These config files are exec(ed). So basically the idea is to define configuaration like this:
section_1 = {
"username": "peter",
"password": "paul"
}
Side note: this is not a discussion about the pros/cons of Python code as configuration files. I like existing discussion and especially Matteo's comment. So this is not about Python as config files.
I want to "misuse" Python for configuration if you like. And I like and need the hierarchical data structure.
What I wish to offer to developers/operators managing the config is to be able to have the option to use the dot notation in key/value assignment.
So my question is: Is it possible (and if YES, then HOW) to implement the following feature?
x.y.z = 1
f.g.h.i.j = {"a": b}
Variables x and f are not defined. I would be totally happy to provide a global object (say g) in the config like this:
from app.config.directive import g
g.x.y.z = 1
g.f.g.h.i.j = {"a": b}
Is it possible to implement this with Python? Objective is to access the defined data structure with something like this:
g["x"]["y"]["z"] == 1
isinstance(g["f"]["g"]["h"]["i"]["j"], dict) == True
I do know that this is a bad idea from Python perspective. This is certainly not pythonic. Please remember: I wish to use Python for config management and basically "extend" Python with some special directives.
I hope you see what I mean. Thanks in advance.