6.2 plasTeX.ConfigManager — plasTeX Configuration

The configuration system in plasTeX that parses the command-line options and configuration files is very flexible. While many options are setup by the plasTeX framework, it is possible for you to add your own options. This is useful if you have macros that may need to be configured by configurable options, or if you write a renderer that surfaces special options to control it.

The config files that ConfigManager supports are standard INI-style files. This is the same format supported by Python’s ConfigParser. However, this API has been extended with some dictionary-like behaviors to make it more Python friendly.

In addition to the config files, ConfigManager can also parse command-line options and merge the options from the command-line into the options set by the given config files. In fact, when adding options to a ConfigManager, you specify both how they appear in the config file as well as how they appear on the command-line. Below is a basic example.

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 system-level config file
c.read('/etc/myconfig.ini')

# Read user-level config file
c.read('~/myconfig.ini')

# Parse the current command-line arguments
opts, args = c.getopt(sys.argv[1:])

# Print the value of the 'verbose' option in the 'debugging' section
print c['debugging']['verbose']

One interesting thing to note about retrieving values from a ConfigManager is that you get the value of the option rather than the option instance that you put in. For example, in the code above. A BooleanOption in put into the ‘verbose’ option slot, but when it is retrieved in the print statement at the end, it prints out a boolean value. This is true of all option types. You can access the option instance in the data attribute of the section (e.g. c[’debugging’].data[’verbose’]).