Package grizzled :: Module config :: Class Configuration
[hide private]
[frames] | no frames]

Class Configuration

source code

ConfigParser.RawConfigParser --+        
                               |        
       ConfigParser.ConfigParser --+    
                                   |    
       ConfigParser.SafeConfigParser --+
                                       |
                                      Configuration

Configuration file parser. See the module documentation for details.
Instance Methods [hide private]
 
__init__(self, defaults=None, permit_includes=True, use_ordered_sections=False, strict_substitution=False)
Construct a new Configuration object.
source code
dict
defaults(self)
Returns the instance-wide defaults.
source code
 
add_section(self, section)
Add a section named section to the instance.
source code
bool
has_section(self, section)
Determine whether a section exists in the configuration.
source code
list
options(self, section)
Get a list of options available in the specified section.
source code
bool
has_option(self, section, option)
Determine whether a section has a specific option.
source code
list
read(self, filenames)
Attempt to read and parse a list of filenames or URLs, returning a list of filenames or URLs which were successfully parsed.
source code
 
readfp(self, fp, filename=None)
Read and parse configuration data from a file or file-like object.
source code
str
get(self, section, option, optional=False)
Get an option from a section.
source code
int
getint(self, section, option, optional=False)
Convenience method that coerces the result of a call to get() to an int.
source code
float
getfloat(self, section, option, optional=False)
Convenience method that coerces the result of a call to get() to a float.
source code
bool
getboolean(self, section, option, optional=False)
Convenience method that coerces the result of a call to get() to a boolean.
source code
bool
getlist(self, section, option, sep=None, optional=False)
Convenience method that coerces the result of a call to get() to a list.
source code
str
get_one_of(self, section, options, optional=False, default=None, value_type=<type 'str'>)
Retrieve at most one of a list or set of options from a section.
source code
list
items(self, section)
Get all items in a section.
source code
 
set(self, section, option, value)
If the given section exists, set the given option to the specified value; otherwise raise NoSectionError.
source code
 
write(self, fileobj)
Write a representation of the configuration to the specified file-like object.
source code
 
remove_section(self, section)
Remove a section from the instance.
source code
 
optionxform(self, option_name)
Transforms the option name in option_name as found in an input file or as passed in by client code to the form that should be used in the internal structures.
source code
 
__get_optional(self, func, section, option) source code
 
__preprocess(self, fp, name) source code
 
__normalizeVariableReferences(self, sourceConfig)
Convert all section-local variable references (i.e., those that don't specify a section) to fully-qualified references.
source code
 
__substituteVariables(self, sourceConfig) source code

Inherited from ConfigParser.SafeConfigParser (private): _interpolate, _interpolate_some

Inherited from ConfigParser.ConfigParser (private): _interpolation_replace

Inherited from ConfigParser.RawConfigParser: remove_option

Inherited from ConfigParser.RawConfigParser (private): _get, _read

Class Variables [hide private]

Inherited from ConfigParser.SafeConfigParser (private): _interpvar_re

Inherited from ConfigParser.ConfigParser (private): _KEYCRE

Inherited from ConfigParser.RawConfigParser: OPTCRE, OPTCRE_NV, SECTCRE

Inherited from ConfigParser.RawConfigParser (private): _boolean_states

Properties [hide private]
  sections
Get the list of available sections, not including DEFAULT.
Method Details [hide private]

__init__(self, defaults=None, permit_includes=True, use_ordered_sections=False, strict_substitution=False)
(Constructor)

source code 
Construct a new Configuration object.
Parameters:
  • defaults (dict) - dictionary of default values
  • permit_includes (bool) - whether or not to permit includes
  • use_ordered_sections (bool) - whether or not to use an ordered dictionary for the section names. If True, then a call to sections() will return the sections in the order they were encountered in the file. If False, the order is based on the hash keys for the sections' names.
  • strict_substitution (bool) - If True, then throw an exception if attempting to substitute a non-existent variable. Otherwise, simple substitute an empty value.
Overrides: ConfigParser.RawConfigParser.__init__

defaults(self)

source code 
Returns the instance-wide defaults.
Returns: dict
the instance-wide defaults, or None if there aren't any
Overrides: ConfigParser.RawConfigParser.defaults

add_section(self, section)

source code 
Add a section named section to the instance. If a section by the given name already exists, DuplicateSectionError is raised.
Parameters:
  • section (str) - name of section to add
Raises:
  • DuplicateSectionError - section already exists
Overrides: ConfigParser.RawConfigParser.add_section

has_section(self, section)

source code 
Determine whether a section exists in the configuration. Ignores the DEFAULT section.
Parameters:
  • section (str) - name of section
Returns: bool
True if the section exists in the configuration, False if not.
Overrides: ConfigParser.RawConfigParser.has_section

options(self, section)

source code 
Get a list of options available in the specified section.
Parameters:
  • section (str) - name of section
Returns: list
list of available options. May be empty.
Raises:
Overrides: ConfigParser.RawConfigParser.options

has_option(self, section, option)

source code 
Determine whether a section has a specific option.
Parameters:
  • section (str) - name of section
  • option (str) - name of option to check
Returns: bool
True if the section exists in the configuration and has the specified option, False if not.
Overrides: ConfigParser.RawConfigParser.has_option

read(self, filenames)

source code 

Attempt to read and parse a list of filenames or URLs, returning a list of filenames or URLs which were successfully parsed. If filenames is a string or Unicode string, it is treated as a single filename or URL. If a file or URL named in filenames cannot be opened, that file will be ignored. This is designed so that you can specify a list of potential configuration file locations (for example, the current directory, the user's home directory, and some system-wide directory), and all existing configuration files in the list will be read. If none of the named files exist, the Configuration instance will contain an empty dataset. An application which requires initial values to be loaded from a file should load the required file or files using readfp() before calling read() for any optional files:

import Configuration
import os

config = Configuration.Configuration()
config.readfp(open('defaults.cfg'))
config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')])
Parameters:
  • filenames (list or string) - list of file names or URLs, or the string for a single filename or URL
Returns: list
list of successfully parsed filenames or URLs
Overrides: ConfigParser.RawConfigParser.read

readfp(self, fp, filename=None)

source code 
Read and parse configuration data from a file or file-like object. (Only the readline() moethod is used.)
Parameters:
  • fp (file) - File-like object with a readline() method
  • filename (str) - Name associated with fp, for error messages. If omitted or None, then fp.name is used. If fp has no name attribute, then "<???"> is used.
Overrides: ConfigParser.RawConfigParser.readfp

get(self, section, option, optional=False)

source code 
Get an option from a section.
Parameters:
  • section (str) - name of section
  • option (str) - name of option to check
  • optional (bool) - True to return None if the option doesn't exist. False to throw an exception if the option doesn't exist.
Returns: str
the option value
Raises:
Overrides: ConfigParser.RawConfigParser.get

getint(self, section, option, optional=False)

source code 
Convenience method that coerces the result of a call to get() to an int.
Parameters:
  • section (str) - name of section
  • option (str) - name of option to check
  • optional (bool) - True to return None if the option doesn't exist. False to throw an exception if the option doesn't exist.
Returns: int
the option value
Raises:
Overrides: ConfigParser.RawConfigParser.getint

getfloat(self, section, option, optional=False)

source code 
Convenience method that coerces the result of a call to get() to a float.
Parameters:
  • section (str) - name of section
  • option (str) - name of option to check
  • optional (bool) - True to return None if the option doesn't exist. False to throw an exception if the option doesn't exist.
Returns: float
the option value
Raises:
Overrides: ConfigParser.RawConfigParser.getfloat

getboolean(self, section, option, optional=False)

source code 

Convenience method that coerces the result of a call to get() to a boolean. Accepted boolean values are "1", "yes", "true", and "on", which cause this method to return True, and "0", "no", "false", and "off", which cause it to return False. These string values are checked in a case-insensitive manner. Any other value will cause it to raise ValueError.

:Parameters:a
section : str
name of section
option : str
name of option to check
optional : bool
True to return None if the option doesn't exist. False to throw an exception if the option doesn't exist.
Returns: bool
the option value (True or False)
Raises:
Overrides: ConfigParser.RawConfigParser.getboolean

getlist(self, section, option, sep=None, optional=False)

source code 
Convenience method that coerces the result of a call to get() to a list. The value is split using the separator(s) specified by the sep argument. A sep value of None uses white space. The result is a list of string values.
Parameters:
  • section (str) - name of section
  • option (str) - name of option to check
  • sep (str) - list element separator to use. Defaults to white space.
  • optional (bool) - True to return None if the option doesn't exist. False to throw an exception if the option doesn't exist.
Returns: bool
the option value (True or False)
Raises:

get_one_of(self, section, options, optional=False, default=None, value_type=<type 'str'>)

source code 

Retrieve at most one of a list or set of options from a section. This method is useful if there are multiple possible names for a single option. For example, suppose you permit either a user_name or a login_name option, but not both, in a section called credentials. You can use the following code to retrieve the option value:

from grizzled.config import Configuration

config = Configuration()
config.read('/path/to/config')
user = config.get_one_of('credentials', ['user_name', 'login_name'])

If both options exist, get_one_of() will a NoOptionError. If neither option exists, get_one_of() will throw a NoOptionError if optional is False and there's no default value; otherwise, it will return the default value.

Parameters:
  • section (str) - name of section
  • options (list or set) - list or set of allowable option names
  • optional (bool) - True to return None if the option doesn't exist. False to throw an exception if the option doesn't exist.
  • default (str) - The default value, if the option does not exist.
  • value_type (type) - The type to which to coerce the value. The value is coerced by casting.
Returns: str
the option value, or None if nonexistent and optional is True
Raises:

items(self, section)

source code 
Get all items in a section.
Parameters:
  • section (str) - name of section
Returns: list
a list of (name, value) tuples for each option in in section
Raises:
Overrides: ConfigParser.RawConfigParser.items

set(self, section, option, value)

source code 
If the given section exists, set the given option to the specified value; otherwise raise NoSectionError.
Parameters:
  • section (str) - name of section
  • option (str) - name of option to check
  • value (str) - the value to set
Raises:
Overrides: ConfigParser.RawConfigParser.set

write(self, fileobj)

source code 

Write a representation of the configuration to the specified file-like object. This output can be parsed by a future read() call.

NOTE: Includes and variable references are not reconstructed. That is, the configuration data is written in expanded form.

Parameters:
  • fileobj (file) - file-like object to which to write the configuration
Overrides: ConfigParser.RawConfigParser.write

remove_section(self, section)

source code 
Remove a section from the instance. If a section by the given name does not exist, NoSectionError is raised.
Parameters:
  • section (str) - name of section to remove
Raises:
Overrides: ConfigParser.RawConfigParser.remove_section

optionxform(self, option_name)

source code 
Transforms the option name in option_name as found in an input file or as passed in by client code to the form that should be used in the internal structures. The default implementation returns a lower-case version of option_name; subclasses may override this or client code can set an attribute of this name on instances to affect this behavior. Setting this to str(), for example, would make option names case sensitive.
Overrides: ConfigParser.RawConfigParser.optionxform

__normalizeVariableReferences(self, sourceConfig)

source code 
Convert all section-local variable references (i.e., those that don't specify a section) to fully-qualified references. Necessary for recursive references to work.

Property Details [hide private]

sections

Get the list of available sections, not including DEFAULT. It's not really useful to call this method before calling read() or readfp().

Returns a list of sections.

Get Method:
unreachable.sections(self) - Get the list of available sections, not including DEFAULT.