Config management with xoa.cfgm#
The xoa.cfgm module simplifies and extends the
configobj
configuration file reader and validator package. It is what xoa uses for its own
options and for its naming specifications (see Metadata, CF and naming conventions).
It makes global usage a little easier and adds support for plural forms,
for command line options and for rst declarations of the options.
The tutorial Manage a configuration with validation and command line options goes through all of this with code.
Concepts#
Specifications describe the options: a section is in brackets, and an option is a
name=function(parameters, default=value) # help. They are typically in a static file next to the module, which initializes aConfigManager.Validation functions convert and check the values: the ones of configobj, like
string,floatoroption, and the ones of xoa, likefigsize,cmapor the dates.xoa.cfgm.print_validation_functions()lists them andxoa.cfgm.register_validation_functions()adds yours.Plural forms validate lists:
floats(n=2)is a list of two floats.Precedence: the command line options, if any, override the user configuration file, which overrides the defaults of the specifications.
The workflow is to create the manager from the specifications, then call
load() to validate a user configuration, or
arg_parse() to also get the command line options:
from xoa import cfgm
CFGM = cfgm.ConfigManager("myspecs.cfg")
cfg = CFGM.load("user.cfg") # validated, with defaults
cfg, args = CFGM.arg_parse(parser=parser, getargs=True, cfgfile="user.cfg")
xoa.cfgm.cfgargparse() does both in a single call.
Command line options#
arg_parse() adds one option per option of the specifications,
named after its section, like --logger-level. They are set to None by default so that
the ones that are not passed do not override the user configuration. It also adds
--cfgfile for the user configuration file, and --short-help and --long-help
in addition to --help.
Documenting the options#
to_rst() exports the specifications as Sphinx declarations
of options and sections, which need two object types to be declared:
app.add_object_type('confopt', 'confopt',
objname='configuration option',
indextemplate='pair: %s; configuration option')
app.add_object_type('confsec', 'confsec',
objname='configuration section',
indextemplate='pair: %s; configuration section')
The names of these types are parameters of the cfg2rst() function.
Fortunately, xoa.cfgm also comes as a Sphinx extension:
Add
"xoa.cfgm"to the list of Sphinx extensions that are declared in theconf.pyfile.Declare the following Sphinx config variables in the
conf.py:- cfgm_get_cfgm_func#
Function that returns a
xoa.cfgm.ConfigManagerinstance.
- cfgm_rst_file#
Name of the outfile file in which rst declarations are written.
Pitfalls#
Keys that are not in the specifications are kept as unvalidated strings. A typo in a user file, like
xminmaxinstead ofxlim, does not raise an error, and the default value ofxlimis used.Validation errors name the option and are
XoaValidateError.