
As a user completely new to oslo_config (well, I had only experienced it in the already-implemented context of the nova project), I would like to have had a reference document that provided a step-by-step guide to writing a simple test script I could use to noodle around. It might have gone something like this. Change-Id: I02ff56f47ac8b93e0ee37eb55b53552059e4da27
2.0 KiB
2.0 KiB
oslo.config Quick Start!
Are you brand new to oslo.config? This brief tutorial will get you started understanding some of the fundamentals.
Prerequisites
- A plain text editor or Python-enabled IDE
- A Python interpreter
- A command shell from which the interpreter can be invoked
- The oslo_config library in your Python path.
Test Script
Put this in a file called oslocfgtest.py
.
# The sys module lets you get at the command line arguments.
import sys
# Load up the cfg module, which contains all the classes and methods
# you'll need.
from oslo_config import cfg
# Define an option group
= cfg.OptGroup('mygroup')
grp
# Define a couple of options
= [cfg.StrOpt('option1'),
opts 'option2', default=42)]
cfg.IntOpt(
# Register your config group
cfg.CONF.register_group(grp)
# Register your options within the config group
=grp)
cfg.CONF.register_opts(opts, group
# Process command line arguments. The arguments tell CONF where to
# find your config file, which it loads and parses to populate itself.
1:])
cfg.CONF(sys.argv[
# Now you can access the values from the config file as
# CONF.<group>.<opt>
print("The value of option1 is %s" % cfg.CONF.mygroup.option1)
print("The value of option2 is %d" % cfg.CONF.mygroup.option2)
Conf File
Put this in a file called oslocfgtest.conf
in the same
directory as oslocfgtest.py
.
[mygroup]
option1 = foo
# Comment out option2 to test the default value
# option2 = 123
Run It!
From your command shell, in the same directory as your script and conf, invoke:
python oslocfgtest.py --config-file oslocfgtest.conf
Revel in the output being exactly as expected. If you've done everything right, you should see:
The value of option1 is foo
The value of option2 is 42
Now go play with some more advanced option settings!