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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/nappy/utils/parse_config.py@3387
Revision 3387, 1.4 KB checked in by astephen, 13 years ago (diff)

bug fix

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
14base_dir = os.environ.get("NAPPY_BASE_DIR", ".." + os.path.sep + "..")
15config_file = os.path.join(base_dir, "nappy.ini")
16config_dict = None
17
18class MyCasePreservingConfigParser(ConfigParser.ConfigParser):
19    optionxform = str
20
21def makeConfigDict(cf=config_file):
22    """
23    Parses config file and returns dictionary of sub-dictionaries
24    for each section holding Keyword-Value pairs.
25    """
26    d = {}
27    conf = MyCasePreservingConfigParser()
28    conf.read(cf)
29
30    # Get all defaults
31    d["DEFAULT"] = {}
32    for i in conf.defaults():
33        d["DEFAULT"][i] = conf.get("DEFAULT", i)
34
35    # and other needed
36    for section in conf.sections():
37        d[section] = {}
38        for item in conf.options(section):
39            value = conf.get(section, item)
40            if item.find("&") > -1:
41                item = tuple(item.split("&"))
42            if value.find("&") > -1:
43                value = tuple(value.split("&"))
44            d[section][item] = value
45
46    return d
47
48
49def getConfigDict(cf=config_file):
50    "Checks if already made and only makes if required."
51    global config_dict
52    if config_dict == None:
53        config_dict = makeConfigDict(cf)
54    return config_dict
55
56
57if __name__=="__main__":
58    print getConfigDict()
Note: See TracBrowser for help on using the repository browser.