Fix an error introduced in 88a923c87e

This commit is contained in:
Lucas Verney 2017-10-19 15:30:03 -04:00
parent 4633dd5179
commit f1a5535bca
2 changed files with 19 additions and 10 deletions

View File

@ -142,7 +142,11 @@ def main():
sys.exit(0) sys.exit(0)
else: else:
# Load config # Load config
config = flatisfy.config.load_config(args) if args.cmd == "build-data":
# Data not yet built, do not use it in config checks
config = flatisfy.config.load_config(args, check_with_data=False)
else:
config = flatisfy.config.load_config(args, check_with_data=True)
if config is None: if config is None:
LOGGER.error("Invalid configuration. Exiting. " LOGGER.error("Invalid configuration. Exiting. "
"Run init-config before if this is the first time " "Run init-config before if this is the first time "

View File

@ -78,11 +78,13 @@ DEFAULT_CONFIG = {
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
def validate_config(config): def validate_config(config, check_with_data):
""" """
Check that the config passed as argument is a valid configuration. Check that the config passed as argument is a valid configuration.
:param config: A config dictionary to fetch. :param config: A config dictionary to fetch.
:param check_with_data: Whether we should use the available OpenData to
check the config values.
:return: ``True`` if the configuration is valid, ``False`` otherwise. :return: ``True`` if the configuration is valid, ``False`` otherwise.
""" """
def _check_constraints_bounds(bounds): def _check_constraints_bounds(bounds):
@ -142,12 +144,13 @@ def validate_config(config):
assert "postal_codes" in constraint assert "postal_codes" in constraint
assert constraint["postal_codes"] assert constraint["postal_codes"]
opendata_postal_codes = [ if check_with_data:
x.postal_code opendata_postal_codes = [
for x in data.load_data(PostalCode, constraint, config) x.postal_code
] for x in data.load_data(PostalCode, constraint, config)
for postal_code in constraint["postal_codes"]: ]
assert postal_code in opendata_postal_codes # noqa: E501 for postal_code in constraint["postal_codes"]:
assert postal_code in opendata_postal_codes # noqa: E501
assert "area" in constraint assert "area" in constraint
_check_constraints_bounds(constraint["area"]) _check_constraints_bounds(constraint["area"])
@ -177,11 +180,13 @@ def validate_config(config):
return traceback.extract_tb(exc_traceback)[-1][-1] return traceback.extract_tb(exc_traceback)[-1][-1]
def load_config(args=None): def load_config(args=None, check_with_data=True):
""" """
Load the configuration from file. Load the configuration from file.
:param args: An argparse args structure. :param args: An argparse args structure.
:param check_with_data: Whether we should use the available OpenData to
check the config values. Defaults to ``True``.
:return: The loaded config dict. :return: The loaded config dict.
""" """
LOGGER.info("Initializing configuration...") LOGGER.info("Initializing configuration...")
@ -269,7 +274,7 @@ def load_config(args=None):
if config_data["website_url"][-1] != '/': if config_data["website_url"][-1] != '/':
config_data["website_url"] += '/' config_data["website_url"] += '/'
config_validation = validate_config(config_data) config_validation = validate_config(config_data, check_with_data)
if config_validation is True: if config_validation is True:
LOGGER.info("Config has been fully initialized.") LOGGER.info("Config has been fully initialized.")
return config_data return config_data