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

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

I'm branching to experiment with making nappy a distutils package that
can be eggified.

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