6.2.1 ConfigManager Objects

class ConfigManager(defaults={ })

Instantiate a configuration class for plasTeX that parses the command-line options as well as reads the config files.

The optional argument, defaults, is a dictionary of default values for the configuration object. These values are used if a value is not found in the requested section.

__add__(other)

merge items from another ConfigManager. This allows you to add ConfigManager instances with syntax like: config + other. This operation will modify the original instance.

add_section(name)

create a new section in the configuration with the given name. This name is the name used for the section heading in the INI file (i.e. the name used within square brackets ([ ]) to start a section). The return value of this method is a reference to the newly created section.

categories()

return the dictionary of categories

copy()

return a deep copy of the configuration

defaults()

return the dictionary of default values

read(filenames)

read configuration data contained in files specified by filenames. Files that cannot be opened are silently ignored. This is designed so that you can specify a list of potential configuration file locations (e.g. current directory, user’s home directory, system directory), and all existing configuration files in the list will be read. A single filename may also be given.

get(section, option, raw=0, vars={ })

retrieve the value of option from the section section. Setting raw to true prevents any string interpolation from occurring in that value. vars is a dictionary of addition value to use when interpolating values into the option.

Note: You can alsouse the alternative dictionary syntax: config[section].get(option).

getboolean(section, option)

retrieve the specified value and cast it to a boolean

get_category(key)

return the title of the given category

getfloat(section, option)

retrieve the specified value and cast it to a float

getint(section, option)

retrieve the specified value and cast it to and integer

get_opt(section, option)

return the option value with any leading and trailing quotes removed

getopt(args=None, merge=True)

parse the command-line options. If args is not given, the args are parsed from sys.argv[1:]. If merge is set to false, then the options are not merged into the configuration. The return value is a two element tuple. The first value is a list of parsed options in the form (option, value), and the second value is the list of arguments.

get_optlist(section, option, delim=’,’)

return the option value as a list using delim as the delimiter

getraw(section, option)

return the raw (i.e. un-interpolated) value of the option

has_category(key, title)

add a category to group options when printing the command-line help. Command-line options can be grouped into categories to make options easier to find when printing the usage message for a program. Categories consist of two pieces: 1) the name, and 2) the title. The name is the key in the category dictionary and is the name used when specifying which category an option belongs to. The title is the actual text that you see as a section header when printing the usage message.

has_option(section, name)

return a boolean indicating whether or not an option with the given name exists in the given section

has_section(name)

return a boolean indicating whether or not a section with the given name exists

__iadd__(other)

merge items from another ConfigManager. This allows you to add ConfigManager instances with syntax like: config += other.

options(name)

return a list of configured option names within a section. Options are all of the settings of a configuration file within a section (i.e. the lines that start with ‘optionname=’).

__radd__(other)

merge items from another ConfigManager. This allows you to add ConfigManager instances with syntax like: other + config. This operation will modify the original instance.

readfp(fp, filename=None)

like read(), but the argument is a file object. The optional filename argument is used for printing error messages.

remove_option(section, option)

remove the specified option from the given section

remove_section(section)

remove the specified section

__repr__()

return the configuration as an INI formatted string; this also includes options that were set from Python code.

sections()

return a list of all section names in the configuration

set(section, option, value)

set the value of an option

__str__()

return the configuration as an INI formatted string; however, do not include options that were set from Python code.

to_string(source=...)

return the configuration as an INI formatted string. The source option indicates which source of information should be included in the resulting INI file. The possible values are:

Name

Description

COMMANDLINE

set from a command-line option

CONFIGFILE

set from a configuration file

BUILTIN

set from Python code

ENVIRONMENT

set from an environment variable

write(fp)

write the configuration as an INI formatted string to the given file object

usage(categories=[])

print the descriptions of all command-line options. If categories is specified, only the command-line options from those categories is printed.