Table of Contents
Provides a simpler interface for the Python built-in ConfigParser module [1].
The ConfigParser module in python is a very powerful module, however if you just wish to store and retrieve values, and maybe provide some default values, the ConfigParser tends to be rather heavy weight.
This module allows you to create, read and write to an INI file in a convenient easy to use way.
As an example, consider you want to save "ore" features, that maybe a user has set within your script. You want to have these values available from run-to-run of your script, but you also wish to provide a nice to read format for the user to edit. In this case the INI file structure is ideal.
from grail.misc import iniconfigparser
ini = iniconfigparser.IniConfigParser()
ini.read("myini.ini")
ini.set("ore", "start-cu", ".23")
ini.set("ort", "start-au", ".301")
Later you can retrieve those values from the file via,
start_value = ini.get("ore", "start-cu", default="0.0")
If the "start-cu" option is not within the "ore" section, the default value will be returned.
Note
Its important to know that IniConfigParser isa ConfigParser object. In other words the IniConfigParser inherits form the ConfigParser. Its just a specialized version that has its get() and set() method logic overridden. The remaining logic for a ConfigParser is still available.
A specialized ConfigParser object. Basically provides some additional methods to the ConfigParser methods to help make working with an extremely simple INI file much easier.
This object has its set() or get() method overridden to allow a INI file to flesh itself out when you insert attributes that do not exist.
For example, when using the get() method you can provide a default value that is used if we can not locate the existing option/section. As another example, consider that the set() method will generate a section if the section you specify did not exist.
Finally, note that the ConfigParser object provides the ability to specify default values. To simply this object, default values where left out. If you wish to have that behavior use the ConfigParser object.
Retrieves the value for the option with in the given section.
Allows you to optionally provide a default value, which is inserted into the INI file on write. You can read an INI file like this,
[section] option = value
Leading whitespace is removed from the value.
Note
If the value is not defined, then the default value will be assigned to the option. When you write the contents to a file via write(), this will write the default value to the file.
Sets a value for a given section's option.
You can read the INI file and the parameters for this method as,
[section] option = value
Note
If the section is not already in the INI file it will be created.
All other methods defined for this object are the same as the ConfigParser object [1].
[1] | (1, 2) Python Library. "ConfigParser -- Configuration file parser". 30 May 2003. 3 June 2004. <http://www.python.org/doc/2.2.3/lib/module-ConfigParser.html> |