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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/nappy/utils/parse_config.py@5191
Revision 5191, 3.5 KB checked in by spascoe, 11 years ago (diff)

Removed fallback of looking for NAPPY_BASE_DIR to find nappy.ini because it is inconsistent with the egg layout. Now only NAPPY_ANNOTATIONS and NAPPY_LOCAL_ATTRIBUTES are the only configuration hooks.

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
14# Global variables
15
16# Defaults are always loaded from <nappy-egg>/nappy/config/nappy.ini
17_here = os.path.dirname(__file__)
18base_dir = os.path.join(_here, '../config')
19
20config_file = os.path.join(base_dir, "nappy.ini")
21config_dict = None
22annotations_config_dict = None
23attributes_config_dict = None
24
25class MyCasePreservingConfigParser(ConfigParser.ConfigParser):
26    optionxform = str
27
28def makeConfigDict(cf=config_file):
29    """
30    Parses config file and returns dictionary of sub-dictionaries
31    for each section holding Keyword-Value pairs.
32    """
33    d = {}
34    conf = MyCasePreservingConfigParser()
35    conf.read(cf)
36
37    # get all sections and content
38    for section in conf.sections():
39        d[section] = {}
40        for item in conf.options(section):
41            value = conf.get(section, item)
42            if value.find("__space__") > -1:
43                value = value.replace("__space__", " ")
44
45            if item.find("&") > -1:
46                item = tuple(item.split("&"))
47            if value.find("&") > -1:
48                value = tuple(value.split("&"))
49            d[section][item] = value
50
51    return d
52
53
54def getConfigDict(cf=config_file):
55    "Checks if already made and only makes if required."
56    global config_dict
57    if config_dict == None:
58        config_dict = makeConfigDict(cf)
59    return config_dict
60
61
62def makeAnnotationsConfigDict(af):
63    """
64    Parses annotations config file and returns dictionary of annotations.
65    """
66    ad = {}
67    conf = MyCasePreservingConfigParser()
68    conf.read(af)
69
70    # Load up dict
71    for item in conf.options("annotations"):
72        value = conf.get("annotations", item)
73        ad[item] = value
74
75    return ad
76
77
78def getAnnotationsConfigDict():
79    "Checks if already made and only makes if required."
80    config_dict = getConfigDict()
81
82    annotations_config_file = os.environ.get("NAPPY_ANNOTATIONS", None) or \
83                                  config_dict["main"]["annotations_file"]
84
85    af = os.path.join(base_dir, annotations_config_file)
86    global annotations_config_dict
87
88    if annotations_config_dict == None:
89        annotations_config_dict = makeAnnotationsConfigDict(af)
90
91    return annotations_config_dict
92
93
94def makeLocalAttributesConfigDict(laf):
95    """
96    Parses local attributes config file and returns dictionary.
97    """
98    lad = {}
99    conf = MyCasePreservingConfigParser()
100    conf.read(laf)
101
102    # Load up dict
103    for sect in ("nc_attributes", "na_attributes"):
104        lad[sect] = {}
105        for item in conf.options(sect):
106            value = conf.get(sect, item)
107            print value
108            lad[sect][item] = value
109
110    return lad
111
112
113def getLocalAttributesConfigDict():
114    "Checks if already made and only makes if required."
115    config_dict = getConfigDict()
116
117    local_attributes_config_file = os.environ.get("NAPPY_LOCAL_ATTRIBUTES", None) or \
118                                  config_dict["main"]["local_attributes_file"]
119   
120    laf = os.path.join(base_dir, local_attributes_config_file)
121    global attributes_config_dict
122
123    if attributes_config_dict == None:
124        attributes_config_dict = makeLocalAttributesConfigDict(laf)
125    return attributes_config_dict
126
127
128
129if __name__=="__main__":
130
131    print getConfigDict()
132    print getAnnotationsConfigDict()
133    print getLocalAttributesConfigDict()
134
Note: See TracBrowser for help on using the repository browser.