Implement section overriding in baseparser

This commit is contained in:
Pradyun S. Gedam 2017-05-15 19:11:36 +05:30
parent b58419afc6
commit ac2e6e5610
1 changed files with 21 additions and 8 deletions

View File

@ -145,6 +145,26 @@ class ConfigOptionParser(CustomOptionParser):
print("An error occurred during configuration: %s" % exc)
sys.exit(3)
def _get_ordered_configuration_items(self):
# Configuration gives keys in an unordered manner. Order them.
override_order = ["global", self.name, ":env:"]
# Pool the options into different groups
section_items = {name: [] for name in override_order}
for key, val in self.config.items():
# ignore empty values
if not val:
continue
section, key = key.split(".", 1)
if section in override_order:
section_items[section].append((key, val))
# Yield each group in their override order
for section in override_order:
for key, val in section_items[section]:
yield key, val
def _update_defaults(self, defaults):
"""Updates the given defaults with values from the config files and
the environ. Does a little special handling for certain types of
@ -157,14 +177,7 @@ class ConfigOptionParser(CustomOptionParser):
self.values = optparse.Values(self.defaults)
late_eval = set()
# Then set the options with those values
for key, val in self.config.items():
# ignore empty values
if not val:
continue
section, key = key.split(".", 1)
if section not in {self.name, "global", ":env:"}:
continue
for key, val in self._get_ordered_configuration_items():
# '--' because configuration supports only long names
option = self.get_option('--' + key)