Package rats :: Module flatconfig :: Class ConfigParser
[hide private]
[frames] | no frames]

Class ConfigParser

source code

object --+
         |
        ConfigParser

Parses flat key-value pairs in a text file.

Removes quotes from quoted strings.

A list of tuples makes possible the use of many times the same key. That is lost if you convert the result to a dict.

This ConfigParser is different from the one from the ConfigParser module, since it does not use the concept of sections. Also, there can be many options with the same key name. This class is partly insprired from ConfigParser.

Instance Methods [hide private]
 
__init__(self)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
read(self, file_name)
Attempts to read a file and parse its configuration entries.
source code
 
options(self)
Returns a list of options available.
source code
 
has_option(self, option)
Checks for the existence of a given option.
source code
 
get(self, option, cast=<type 'str'>)
Get an option value as a string.
source code
 
get_list(self, option, cast=<type 'str'>)
Returns a list of values for a given key name.
source code
 
cast(self, value, cast)
Casts a value to a given type.
source code
 
items(self)
Return a list of tuples with (name, value) for each option.
source code
 
_read(self, file_name) source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables [hide private]
  STRIPPED = '"\' '
  ASSIGNATION_OPERATORS = '=:'
  COMMENTERS = '#'
  _boolean_states = {'0': False, '1': True, 'false': False, 'no'...
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self)
(Constructor)

source code 

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Overrides: object.__init__
(inherited documentation)

read(self, file_name)

source code 

Attempts to read a file and parse its configuration entries. It might throw a ParsingError.

get(self, option, cast=<type 'str'>)

source code 

Get an option value as a string.

The cast argument allows you to cast the type of the value to int, float or bool. Just pass it a type.

Might raise a NoSuchOptionError if the option is not found. Might raise a ValueError if the value is not of the given type.

If a key is defined many times, behavious is undefined. It it likely to return the last item with that key.

Note that the accepted values for the option 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.

cast(self, value, cast)

source code 

Casts a value to a given type. Type can be int, float, str or bool.


Class Variable Details [hide private]

_boolean_states

Value:
{'0': False,
 '1': True,
 'false': False,
 'no': False,
 'off': False,
 'on': True,
 'true': True,
 'yes': True}