The config module

The config module contains modules pertaining to parsing configuration options from a configuration file, as well as from the command line arguments.

This module provides the ability to specify individual configuration options that should be loaded from either a configuration file, or from the command line.

The Option class

class pyamp.config.Option(name, defaultValue=None, typeFn=None, commandLine=False)

The Option class encapsulates a single configuration option. It contains the name of the option, a default value for the option, as well as a function to convert the option into a specific type.

  • name – The name of the option
  • defaultValue – The default value for this option
  • typeFn – The function to convert the value to a specific type
  • commandLine – True to be a command line option
convert(value)

Convert the given value into the expected type for this Option.

  • value – The value to convert
getDefaultValue()

Get the default value for this Option.

getName()

Get the name of this Option.

isCommandLine()

Determine if this Option is configurable through the command line.

The ClOption class

class pyamp.config.ClOption(name, abbr=None, defaultValue=None, action=None, optionType=None, helpText=None)

The ClOption class encapsulates an Option that is configurable through the command line as well as through a configuration file.

  • name – The name of the option
  • abbr – The abbreviated name
  • defaultValue – The default value for this option
  • action – The action used for this option
  • optionType – The type of the option
  • helpText – The help text to display
addOption(parser)

Add the command line option to the command line parser.

  • parser – The command line parser

The ConfigFile class

class pyamp.config.ConfigFile(configurationFile=None, options=None, variables=None, logger=None)

The ConfigFile class is responsible for parsing a configuration file with the given dictionary of sections and options, and variables.

The ConfigFile class takes a dictionary of options which map the configuration section names to the list of configuration Option objects associated with that section. It also takes a dictionary of variables mapping the name of the variable to the value for the variable. The variable names can then be used in a configuration file by adding a $ before the variable name. This value will then be replaced with the value assigned in the variables dictionary.

This class implements the Loggable interface. This class uses the ConfigParser module to parse the configuration file, and thus parses configuration files of the same format.

  • configurationFile – The configuration file to load
  • options – The dictionary of supported options
  • variables – The dictionary of supported variables
  • logger – The logger
get(section, option, defaultValue=None)

Get the value of the given configuration option from the given section.

  • section – The section name
  • option – The option
  • defaultValue – The default value if the option does not exist
getSettings()

Get the settings dictionary.

parse(filename)

Parse a configuration file.

  • filename – The configuration file to parse

The OptionsParser class

class pyamp.config.OptionsParser(*args, **kwargs)

The OptionsParser class is responsible for parsing command line and configuration file options and providing the ability to get the value of a given option.

This class implements the pyamp.patterns.Borg design pattern and also implements the pyamp.logging.Loggable interface.

  • args – The arguments
  • kwargs – The keyword arguments
ClOptions

This property contains the list of options that are only configurable via the command line.

Options

This property contains a dictionary mapping section names to the list of options contained within that section. Options must be a subclass of the Option class. These are the sections and options that will be loaded from the configuration file and the command line arguments.

Note

Any ClOption objects that are given will be configurable via the configuration file and from the command line.

Variables

This property contains a dictionary mapping variable names to their respective values. These variables can be used within the configuration file and will be replaced by the assigned values.

Variables can be used in the configuration file by appending a dollar sign before the name of the variable. The variable will then replaced with its value prior to parsing the file.

Example:

# $HOME will be replaced with the user's home directory before parsing
[Section]
Var = $HOME
classmethod get(section, optionName, defaultValue=None)

Get the value of an option from the given section.

  • section – The name of the section
  • optionName – The name of the option
  • defaultValue – The default value if the option does not exist
classmethod getCl(optionName, defaultValue=None)

Get the value of a command line option.

  • optionName – The name of the command line option
  • defaultValue – The default value if the option does not exist
init(logger=None)

Override the pyamp.patterns.Borg init function.

parse(argv, filename)

Parse the command line options, and the configuration file.

  • argv – The command line options
  • filename – The path to the configuration file
setLogger(logger, color=0)

Set the logger for the OptionsParser.

The conversions module

Contains various type conversion functions for converting configuration options into specific types.

pyamp.config.conversions.logLevel(value)[source]

Get the log level type from the string representation of the log level (DEBUG, INFO, WARN, or ERROR).

  • value – The string value for the desired log level
pyamp.config.conversions.string(value)[source]

Convert the given value into a string type – being sure to remove any quotations that may be around the string.

  • value – The value to convert into a string

Table Of Contents

This Page