source: nappy/trunk/nappy/utils/parse_config.py @ 3324

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/nappy/utils/parse_config.py@3324
Revision 3324, 1.3 KB checked in by astephen, 12 years ago (diff)
Line 
1"""
2parse_config.py
3===================
4
5Parses config file for nappy.
6
7"""
8
9# Standard library imports
10import ConfigParser
11import os
12
13# Global variables
14config_file = ".." + os.path.sep + "nappy.ini"
15config_dict = None
16
17class MyCasePreservingConfigParser(ConfigParser.ConfigParser):
18    optionxform = str
19
20def makeConfigDict(cf=config_file):
21    """
22    Parses config file and returns dictionary of sub-dictionaries
23    for each section holding Keyword-Value pairs.
24    """
25    d = {}
26    conf = MyCasePreservingConfigParser()
27    conf.read(cf)
28
29    # Get all defaults
30    d["DEFAULT"] = {}
31    for i in conf.defaults():
32        d["DEFAULT"][i] = conf.get("DEFAULT", i)
33
34    # and other needed
35    for section in conf.sections():
36        d[section] = {}
37        for item in conf.options(section):
38            value = conf.get(section, item)
39            if item.find("&") > -1:
40                item = tuple(item.split("&"))
41            if value.find("&") > -1:
42                value = tuple(value.split("&"))
43            d[section][item] = value
44
45    return d
46
47
48def getConfigDict(cf=config_file):
49    "Checks if already made and only makes if required."
50    global config_dict
51    if config_dict == None:
52        config_dict = makeConfigDict(cf)
53    return config_dict
54
55
56if __name__=="__main__":
57    print getConfigDict()
Note: See TracBrowser for help on using the repository browser.