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

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

First major re-factor.

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.environ["NAPPY_INI"]
15wps_config_dict = None
16
17
18def makeConfigDict(cf=config_file):
19    """
20    Parses config file and returns dictionary of sub-dictionaries
21    for each section holding Keyword-Value pairs.
22    """
23    d = {}
24    conf = ConfigParser.ConfigParser()
25    conf.read(cf)
26
27    # Get all defaults
28    d["DEFAULT"] = {}
29    for i in conf.defaults():
30        d["DEFAULT"][i] = conf.get("DEFAULT", i)
31
32    # and other needed
33    for section in conf.sections():
34        d[section] = {}
35        for item in d.options(section):
36            d[section][item] = conf.get(section, item)
37
38    return d
39
40
41def getConfigDict(cf=config_file):
42    "Checks if already made and only makes if required."
43    global wps_config_dict
44    if wps_config_dict == None:
45        wps_config_dict = makeConfigDict(cf)
46    return wps_config_dict
47
48
49if __name__=="__main__":
50    print getConfigDict()
Note: See TracBrowser for help on using the repository browser.