Created
November 18, 2013 13:11
-
-
Save toudi/7527501 to your computer and use it in GitHub Desktop.
ConfigParser that emulates ZendConfigParser (multiple sections inheritance)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ConfigParser import SafeConfigParser, NoOptionError, NoSectionError | |
class ZendConfigParser(SafeConfigParser): | |
def read(self, filenames): | |
self._parents = {} | |
self._s = {} | |
SafeConfigParser.read(self, filenames) | |
for section in self.sections(): | |
if ':' in section: | |
s = section.split(':') | |
self._parents[s[0]] = s[1] | |
self._s[section] = s[1] | |
def get(self, section, option, raw=False, vars=None): | |
try: | |
return SafeConfigParser.get(self, section, option, raw, vars) | |
except NoSectionError as e: | |
if section in self._parents: | |
return self.get(':'.join((section, self._parents[section])), option, raw, vars) | |
else: | |
raise e | |
except NoOptionError: | |
if section in self._s: | |
return self.get(self._s[section], option, raw, vars) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment