1 |
import re,ConfigParser
|
2 |
|
3 |
class BetterConfigParser(ConfigParser.SafeConfigParser):
|
4 |
|
5 |
def get(self, section, option):
|
6 |
result = ConfigParser.SafeConfigParser.get(self, section, option, raw=True)
|
7 |
result = self.__replaceSectionwideTemplates(result)
|
8 |
return result
|
9 |
|
10 |
def optionxform(self, optionstr):
|
11 |
'''
|
12 |
enable case sensitive options in .ini files
|
13 |
'''
|
14 |
return optionstr
|
15 |
|
16 |
def __replaceSectionwideTemplates(self, data):
|
17 |
'''
|
18 |
replace <section|option> with get(section,option) recursivly
|
19 |
'''
|
20 |
result = data
|
21 |
findExpression = re.compile("((.*)\<!(.*)\|(.*)\!>(.*))*")
|
22 |
groups = findExpression.search(data).groups()
|
23 |
if not groups == (None, None, None, None, None): # expression not matched
|
24 |
result = self.__replaceSectionwideTemplates(groups[1])
|
25 |
result += self.get(groups[2], groups[3])
|
26 |
result += self.__replaceSectionwideTemplates(groups[4])
|
27 |
return result
|