Quickstart
Install
To install the configoose module, use the command
python -m pip install git+https://github.com/Gribouillis/configoose.git
Initialization
configoose manages a database. To initialize it you must create
a new directory, called a marina.
The syntax depends on your operating system:
mkdir /some/new/directory
It is then necessary to configure configoose so that it uses this
directory for its database. Launch the command
python -m configoose conf --marina /some/new/directory
This command creates a new file named configooseconf.py
in the first site-packages directory of your Python
interpreter.
If you don’t have writing permission in the site-packages
directory, you can alternatively write a userconfigooseconf.py
file in your user site-packages directory. For this,
pass the --user switch to the previous command.
You could also prefer another directory
on the Python module search path to install
the configooseconf.py file.
For this pass the --dest /your/preferred/directory option
to the previous command.
You can check that everything went well by running the following command
python -m configoose marina-list
The last line of output should resemble
MarinaDirInOs(PosixPath('/some/new/directory'), tags={'initial'})
Simple Example
Create a configuration file
Let us create a file using ConfigParser for configuration. Type the following
command in a terminal, it will create a file spam.cfg in your
home directory. You can use any other directory or file name if you wish.
python -m configoose template configoose.protocol.configparser.Protocol -o ~/spam.cfg
Now edit the new file and populate it with configuration data in the syntax of configparser. The file is an ordinary configparser file, except that there is a small Python dictionary at the beginning, called a preamble. Leave this dictionary as it is. Your file’s content should resemble this one
{
"address" : "s8dzw5y5anduiodmrdrxdgxaa",
"protopath" : "configoose.protocol.configparser.Protocol",
}
[spam]
ham =
slice A
slice B
slice C
[more]
eggs = 1000
Register the configuration file in configoose’s database
Run the command
python -m configoose moor initial ~/spam.cfg
This step associates in the configoose database the address contained in the configuration file to the location of the file.
Create a program that uses the configuration file
With a code editor, create the following program
from configoose import Configurator
cfg = Configurator("s8dzw5y5anduiodmrdrxdgxaa")
@cfg.add_protocol("configoose.protocol.configparser.Protocol")
def handler(ap, preamble, parser):
print('ham = ', parser['spam']['ham'])
print(f"There are {parser['more']['eggs']} eggs!")
cfg.run()
Run the program, its output shows that it read the configuration file correctly
ham =
slice A
slice B
slice C
There are 1000 eggs!