1 | """ |
---|
2 | parse_config.py |
---|
3 | =================== |
---|
4 | |
---|
5 | Parses config file for nappy. |
---|
6 | |
---|
7 | """ |
---|
8 | |
---|
9 | # Standard library imports |
---|
10 | import ConfigParser |
---|
11 | import 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__) |
---|
18 | base_dir = os.path.join(_here, '../config') |
---|
19 | |
---|
20 | config_file = os.path.join(base_dir, "nappy.ini") |
---|
21 | config_dict = None |
---|
22 | annotations_config_dict = None |
---|
23 | attributes_config_dict = None |
---|
24 | |
---|
25 | class MyCasePreservingConfigParser(ConfigParser.ConfigParser): |
---|
26 | optionxform = str |
---|
27 | |
---|
28 | def 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 | |
---|
54 | def 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 | |
---|
62 | def 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 | |
---|
78 | def 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 | |
---|
94 | def 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 | |
---|
113 | def 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 | |
---|
129 | if __name__=="__main__": |
---|
130 | |
---|
131 | print getConfigDict() |
---|
132 | print getAnnotationsConfigDict() |
---|
133 | print getLocalAttributesConfigDict() |
---|
134 | |
---|