plasTeX 3.0 — A Python Framework for Processing LaTeX Documents

6.2 plasTeX.ConfigManager — plasTeX Configuration

A ConfigManager manages the configuration options for plastex. From the end user’s point of view, this behaves like a 2-layered nested dict, so that configuration options can be retrieved as config["general"]["theme"], for example. However, ConfigManager is much more powerful than a nested dict. It supports reading in configuration options from .ini files and command line arguments.

The former is performed via the read function, and the latter is via the registerArgparse and updateFromDict functions. The first function adds the options to an ArgumentParser from argparse, and the latter reads in the values obtained from argparse. The interaction looks as follows:


config = ConfigManager()

# Set up the config manager here

# Construct an ArgumentParser
parser = ArgumentParser("plasTeX")

# This function adds a command line option to `parser` for each
# configuration item in `config`
config.registerArgparse(parser)

# We now let the parser parse the arguments in `sys.argv`
data = vars(parser.parse_args())

# Finally, we let `config` read the values back in
config.updateFromDict(data)

The following sample code shows how one would set up a ‘ConfigManager‘ object:

from plasTeX.ConfigManager import *
c = ConfigManager()

# Create a new section in the config file.  This corresponds to the
# [ sectionname ] sections in an INI file.  The returned value is 
# a reference to the new section
d = c.add_section('debugging')

# Add an option to the 'debugging' section called 'verbose'.
# This corresponds to the config file setting:
#
# [debugging]
# verbose = no
#
d['verbose'] = BooleanOption(
    """ Increase level of debugging information """,
    options = '-v --verbose !-q !--quiet',
    default = False,
)

# Read config files
c.loadFromFiles(['/etc/myconfig.ini', '~/myconfig.ini')

The configuration system supports interpolation. That is, in options whose value is a string (or a list of strings), we replace all instances of %(foo)s with the value of the option foo. The formatting is done with pythons %-formatting and all %-formatting features are supported. Note that we only specify the name of the option, and not the section it belongs to. For example,

plastex --renderer=HTML5 --theme=%(renderer)stheme

sets the theme to HTML5theme.

Interpolation is performed when you access the value of an option, not when the value is set. For example, if we modify the renderer variable in runtime and then access theme, it changes accordingly. Specifically, this is performed in ConfigSection.__getitem__.