Module config
[show private | hide private]
[frames | no frames]

Module config

This is a configuration module for Python.

This module should work under Python versions >= 2.2, and cannot be used with earlier versions since it uses new-style classes.

Development and testing has only been carried out (so far) on Python 2.3.4 and Python 2.4.2. See the test module (test_config.py) included in the distribution (follow the download link).

A simple example - with the example configuration file:
   messages:
   [
     {
       stream : `sys.stderr`
       message: 'Welcome'
       name: 'Harry'
     }
     {
       stream : `sys.stdout`
       message: 'Welkom'
       name: 'Ruud'
     }
     {
       stream : $messages[0].stream
       message: 'Bienvenue'
       name: Yves
     }
   ]
a program to read the configuration would be:
   from config import Config

   f = file('simple.cfg')
   cfg = Config(f)
   for m in cfg.messages:
       s = '%s, %s' % (m.message, m.name)
       try:
           print >> m.stream, s
       except IOError, e:
           print e
which, when run, would yield the console output:
   Welcome, Harry
   Welkom, Ruud
   Bienvenue, Yves
See this tutorial for more information.

Version: 0.3.7

Author: Vinay Sajip

Copyright: Copyright (C) 2004-2007 Vinay Sajip. All Rights Reserved.

Classes
Config This class represents a configuration, and is the only one which clients need to interface to, under normal circumstances.
ConfigInputStream An input stream which can read either ANSI files with default encoding or Unicode files with BOMs.
ConfigList This class implements an ordered list of configurations and allows you to try getting the configuration from each entry in turn, returning the first successfully obtained value.
ConfigMerger This class is used for merging two configurations.
ConfigOutputStream An output stream which can write either ANSI files with default encoding or Unicode files with BOMs.
ConfigReader This internal class implements a parser for configurations.
Container This internal class is the base class for mappings and sequences.
Expression This internal class implements a value which is obtained by evaluating an expression.
Mapping This internal class implements key-value mappings in configurations.
Reference This internal class implements a value which is a reference to another value.
Sequence This internal class implements a value which is a sequence of other values.

Exceptions
ConfigError This is the base class of exceptions raised by this module.
ConfigFormatError This is the base class of exceptions raised due to syntax errors in configurations.
ConfigResolutionError This is the base class of exceptions raised due to semantic errors in configurations.

Function Summary
str defaultMergeResolve(map1, map2, key)
A default resolver for merge conflicts.
A read-only stream (file-like object) defaultStreamOpener(name)
This function returns a read-only stream, given its name.
bool isWord(s)
See if a passed-in value is an identifier.
str makePath(prefix, suffix)
Make a path from a prefix and suffix.
  overwriteMergeResolve(map1, map2, key)
An overwriting resolver for merge conflicts.

Variable Summary
str __author__ = 'Vinay Sajip <vinay_sajip@red-dove.com>'
str __date__ = '05 October 2007'
str __status__ = 'alpha'
str __version__ = '0.3.7'
str AT = '@'
str BACKTICK = '`'
str COLON = ':'
str COMMA = ','
str DOLLAR = '$'
str DOT = '.'
str EOF = ''
str FALSE = 'False'
str LBRACK = '['
str LBRACK2 = 'a['
str LCURLY = '{'
str LPAREN = '('
str LPAREN2 = '(('
str MINUS = '-'
str MOD = '%'
str NONE = 'None'
str NUMBER = '9'
str PLUS = '+'
str RBRACK = ']'
str RCURLY = '}'
str RPAREN = ')'
str SLASH = '/'
str STAR = '*'
NoneType streamOpener: The default stream opener.
str STRING = '"'
str TRUE = 'True'
str WORD = 'a'
str WORDCHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopq...

Function Details

defaultMergeResolve(map1, map2, key)

A default resolver for merge conflicts. Returns a string indicating what action to take to resolve the conflict.
Parameters:
map1 - The map being merged into.
           (type=Mapping.)
map2 - The map being used as the merge operand.
           (type=Mapping.)
key - The key in map2 (which also exists in map1).
           (type=str)
Returns:
One of "merge", "append", "mismatch" or "overwrite" indicating what action should be taken. This should be appropriate to the objects being merged - e.g. there is no point returning "merge" if the two objects are instances of Sequence.
           (type=str)

defaultStreamOpener(name)

This function returns a read-only stream, given its name. The name passed in should correspond to an existing stream, otherwise an exception will be raised.

This is the default value of streamOpener; assign your own callable to streamOpener to return streams based on names. For example, you could use urllib2.urlopen().
Parameters:
name - The name of a stream, most commonly a file name.
           (type=str)
Returns:
A stream with the specified name.
           (type=A read-only stream (file-like object))

isWord(s)

See if a passed-in value is an identifier. If the value passed in is not a string, False is returned. An identifier consists of alphanumerics or underscore characters.

Examples:
   isWord('a word') ->False
   isWord('award') -> True
   isWord(9) -> False
   isWord('a_b_c_') ->True
Parameters:
s - The name to be tested
           (type=any)
Returns:
True if a word, else False
           (type=bool)

Note: isWord('9abc') will return True - not exactly correct, but adequate for the way it's used here.

makePath(prefix, suffix)

Make a path from a prefix and suffix.

Examples:
   makePath('', 'suffix') -> 'suffix'
   makePath('prefix', 'suffix') -> 'prefix.suffix'
   makePath('prefix', '[1]') -> 'prefix[1]'
Parameters:
prefix - The prefix to use. If it evaluates as false, the suffix is returned.
           (type=str)
suffix - The suffix to use. It is either an identifier or an index in brackets.
           (type=str)
Returns:
The path concatenation of prefix and suffix, with a dot if the suffix is not a bracketed index.
           (type=str)

overwriteMergeResolve(map1, map2, key)

An overwriting resolver for merge conflicts. Calls defaultMergeResolve, but where a "mismatch" is detected, returns "overwrite" instead.
Parameters:
map1 - The map being merged into.
           (type=Mapping.)
map2 - The map being used as the merge operand.
           (type=Mapping.)
key - The key in map2 (which also exists in map1).
           (type=str)

Variable Details

__author__

Type:
str
Value:
'Vinay Sajip <vinay_sajip@red-dove.com>'                               

__date__

Type:
str
Value:
'05 October 2007'                                                      

__status__

Type:
str
Value:
'alpha'                                                                

__version__

Type:
str
Value:
'0.3.7'                                                                

AT

Type:
str
Value:
'@'                                                                    

BACKTICK

Type:
str
Value:
'`'                                                                    

COLON

Type:
str
Value:
':'                                                                    

COMMA

Type:
str
Value:
','                                                                    

DOLLAR

Type:
str
Value:
'$'                                                                    

DOT

Type:
str
Value:
'.'                                                                    

EOF

Type:
str
Value:
''                                                                     

FALSE

Type:
str
Value:
'False'                                                                

LBRACK

Type:
str
Value:
'['                                                                    

LBRACK2

Type:
str
Value:
'a['                                                                   

LCURLY

Type:
str
Value:
'{'                                                                    

LPAREN

Type:
str
Value:
'('                                                                    

LPAREN2

Type:
str
Value:
'(('                                                                   

MINUS

Type:
str
Value:
'-'                                                                    

MOD

Type:
str
Value:
'%'                                                                    

NONE

Type:
str
Value:
'None'                                                                 

NUMBER

Type:
str
Value:
'9'                                                                    

PLUS

Type:
str
Value:
'+'                                                                    

RBRACK

Type:
str
Value:
']'                                                                    

RCURLY

Type:
str
Value:
'}'                                                                    

RPAREN

Type:
str
Value:
')'                                                                    

SLASH

Type:
str
Value:
'/'                                                                    

STAR

Type:
str
Value:
'*'                                                                    

streamOpener

The default stream opener. This is a factory function which takes a string (e.g. filename) and returns a stream suitable for reading. If unable to open the stream, an IOError exception should be thrown.

The default value of this variable is defaultStreamOpener. For an example of how it's used, see test_config.py (search for streamOpener).
Type:
NoneType
Value:
None                                                                  

STRING

Type:
str
Value:
'"'                                                                    

TRUE

Type:
str
Value:
'True'                                                                 

WORD

Type:
str
Value:
'a'                                                                    

WORDCHARS

Type:
str
Value:
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_'