Skip to content

Commit

Permalink
Fix validation of campaign configs
Browse files Browse the repository at this point in the history
This merge switches to first merging user and campaign config
files and only validating after the merge.  This slightly
complicates the logic for getting the campaign (if any) because
defaults have not yet been parsed at that point.  But it does
mean we only validate once (so only one validation success is
printed) and it means that defaults will not override the
campaign config options, as was happening before this merge.
  • Loading branch information
xylar committed Oct 6, 2021
1 parent 91813d3 commit 42434c4
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions zppy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,23 @@ def main():
# Read configuration file and validate it
default_config = os.path.join(templateDir, "default.ini")
user_config = ConfigObj(args.config, configspec=default_config)
_validate_config(user_config)
campaign = user_config["default"]["campaign"]
if "campaign" in user_config["default"]:
campaign = user_config["default"]["campaign"]
else:
campaign = "none"
if campaign != "none":
campaign_file = os.path.join(templateDir, "{}.cfg".format(campaign))
if not os.path.exists(campaign_file):
raise ValueError(
"{} does not appear to be a known campaign".format(campaign)
)
config = ConfigObj(campaign_file, configspec=default_config)
_validate_config(config)
# merge such that user_config takes priority
config.merge(user_config)
campaign_config = ConfigObj(campaign_file, configspec=default_config)
# merge such that config takes priority over campaign_config
campaign_config.merge(user_config)
config = campaign_config
else:
# no need to merge
config = user_config
_validate_config(config)

# Add templateDir to config
config["default"]["templateDir"] = templateDir
Expand Down

0 comments on commit 42434c4

Please sign in to comment.