Skip to content

Instantly share code, notes, and snippets.

@toudi
Created November 18, 2013 13:11
Show Gist options
  • Save toudi/7527501 to your computer and use it in GitHub Desktop.
Save toudi/7527501 to your computer and use it in GitHub Desktop.
ConfigParser that emulates ZendConfigParser (multiple sections inheritance)
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