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

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

More changes from print to logging.

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                              os.path.join(base_dir, config_dict["main"]["annotations_file"])
84
85    global annotations_config_dict
86
87    if annotations_config_dict == None:
88        annotations_config_dict = makeAnnotationsConfigDict(annotations_config_file)
89
90    return annotations_config_dict
91
92
93def makeLocalAttributesConfigDict(laf):
94    """
95    Parses local attributes config file and returns dictionary.
96    """
97    lad = {}
98    conf = MyCasePreservingConfigParser()
99    conf.read(laf)
100
101    # Load up dict
102    for sect in ("nc_attributes", "na_attributes"):
103        lad[sect] = {}
104        for item in conf.options(sect):
105            value = conf.get(sect, item)
106            lad[sect][item] = value
107
108    return lad
109
110
111def getLocalAttributesConfigDict():
112    "Checks if already made and only makes if required."
113    config_dict = getConfigDict()
114
115    local_attributes_config_file = os.environ.get("NAPPY_LOCAL_ATTRIBUTES", None) or \
116                                   os.path.join(base_dir, config_dict["main"]["local_attributes_file"])
117   
118    global attributes_config_dict
119
120    if attributes_config_dict == None:
121        attributes_config_dict = makeLocalAttributesConfigDict(local_attributes_config_file)
122    return attributes_config_dict
123
124
125
126if __name__=="__main__":
127
128    print getConfigDict()
129    print getAnnotationsConfigDict()
130    print getLocalAttributesConfigDict()
131
Note: See TracBrowser for help on using the repository browser.