From efffb7474d695fc09f94e8aabcf54f160146b9a8 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Mon, 3 Oct 2016 20:34:33 +0800 Subject: Add custom configuration checkers. These additional checker functions validate the required configs whether provided by the user config file, as well as against necessary contexts. --- fg21sim/configs/__init__.py | 1 + fg21sim/configs/validate.py | 94 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 fg21sim/configs/validate.py (limited to 'fg21sim') diff --git a/fg21sim/configs/__init__.py b/fg21sim/configs/__init__.py index b1fe70f..bbfbbd7 100644 --- a/fg21sim/configs/__init__.py +++ b/fg21sim/configs/__init__.py @@ -2,5 +2,6 @@ # MIT license from .manager import ConfigManager +from .validate import validate_configs configs = ConfigManager() diff --git a/fg21sim/configs/validate.py b/fg21sim/configs/validate.py new file mode 100644 index 0000000..b9accdf --- /dev/null +++ b/fg21sim/configs/validate.py @@ -0,0 +1,94 @@ +# Copyright (c) 2016 Weitian LI +# MIT license + +""" +Custom validations for the configurations. +""" + +from ..errors import ConfigError + + +def _check_missing(configs, keys): + """Check whether the mandatory config is provided by the user.""" + if isinstance(keys, str): + keys = [keys, ] + for key in keys: + if not configs.getn(key): + raise ConfigError('config "%s" missing' % key) + return True + + +def check_common(configs): + """Check the "[common]" section of the configurations.""" + _check_missing(configs, "common/data_dir") + return True + + +def check_frequency(configs): + """Check the "[frequency]" section of the configurations.""" + if configs.getn("frequency/type") == "custom": + _check_missing(configs, "frequency/frequencies") + elif configs.getn("frequency/type") == "calc": + _check_missing(configs, ["frequency/start", + "frequency/stop", + "frequency/step"]) + return True + + +def check_output(configs): + """Check the "[output]" section of the configurations.""" + if configs.getn("output/combine"): + _check_missing(configs, "output/output_dir") + return True + + +def check_galactic_synchrotron(configs): + """Check the "[galactic][synchrotron]" section of the configurations.""" + _check_missing(configs, ["galactic/synchrotron/template", + "galactic/synchrotron/template_freq", + "galactic/synchrotron/template_unit", + "galactic/synchrotron/indexmap"]) + if configs.getn("galactic/synchrotron/save"): + _check_missing(configs, "galactic/synchrotron/output_dir") + return True + + +# Available checkers to validate the configurations +_CHECKERS = [ + check_common, + check_frequency, + check_output, + check_galactic_synchrotron, +] + + +def validate_configs(configs, checkers=_CHECKERS): + """Validate the configurations through the supplied checkers. + + These checker usually validate on the global scale, and validate + some specific configs against their contexts. + + Parameters + ---------- + configs : `ConfigManager` object + An `ConfigManager` object contains both default and user + configurations. + checkers : list of functions + List of checker functions through which the configurations + will be validated. + + Returns + ------- + bool + True if the configurations pass all checker functions, otherwise, + the `ConfigError` will be raised with corresponding message. + + Raises + ------ + ConfigError + If any configuration failed the check, a `ConfigError` with + details will be raised. + """ + for checker in checkers: + checker(configs) + return True -- cgit v1.2.2