source: nappy/branches/nappy-eggable/nappy/utils/parse_config.py @ 5071

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/branches/nappy-eggable/nappy/utils/parse_config.py@5071
Revision 5071, 3.6 KB checked in by spascoe, 11 years ago (diff)

Put default ini files in the package hierarchy. NAPPY_BASE_DIR is still
used if set.

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