diff options
| author | leonoverweel <l.overweel@gmail.com> | 2017-01-24 12:05:15 +0100 |
|---|---|---|
| committer | leonoverweel <l.overweel@gmail.com> | 2017-01-24 12:05:15 +0100 |
| commit | 86a50a4f6df9ece982743a3b7ca510846d248909 (patch) | |
| tree | 79edc0478908b7fee9e5dca2088e562c7a62038b /opendc/util/parameter_checker.py | |
Initial commit
Diffstat (limited to 'opendc/util/parameter_checker.py')
| -rw-r--r-- | opendc/util/parameter_checker.py | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/opendc/util/parameter_checker.py b/opendc/util/parameter_checker.py new file mode 100644 index 00000000..32cd6777 --- /dev/null +++ b/opendc/util/parameter_checker.py @@ -0,0 +1,90 @@ +from opendc.util import database, exceptions + +def _missing_parameter(params_required, params_actual, parent=''): + """Recursively search for the first missing parameter.""" + + for param_name in params_required: + + if not param_name in params_actual: + return '{}.{}'.format(parent, param_name) + + param_required = params_required.get(param_name) + param_actual = params_actual.get(param_name) + + if isinstance(param_required, dict): + + param_missing = _missing_parameter( + param_required, + param_actual, + param_name + ) + + if param_missing is not None: + return '{}.{}'.format(parent, param_missing) + + return None + +def _incorrect_parameter(params_required, params_actual, parent=''): + """Recursively make sure each parameter is of the correct type.""" + + for param_name in params_required: + + param_required = params_required.get(param_name) + param_actual = params_actual.get(param_name) + + if isinstance(param_required, dict): + + param_incorrect = _incorrect_parameter( + param_required, + param_actual, + param_name + ) + + if param_incorrect is not None: + return '{}.{}'.format(parent, param_incorrect) + + else: + + if param_required == 'datetime': + try: + database.string_to_datetime(param_actual) + except: + return '{}.{}'.format(parent, param_name) + + if param_required == 'int' and not isinstance(param_actual, int): + return '{}.{}'.format(parent, param_name) + + if param_required == 'string' and not isinstance(param_actual, basestring): + return '{}.{}'.format(parent, param_name) + + if param_required.startswith('list') and not isinstance(param_actual, list): + return '{}.{}'.format(parent, param_name) + +def _format_parameter(parameter): + """Format the output of a parameter check.""" + + parts = parameter.split('.') + inner = ['["{}"]'.format(x) for x in parts[2:]] + return parts[1] + ''.join(inner) + +def check(request, **kwargs): + """Return True if all required parameters are there.""" + + for location, params_required in kwargs.iteritems(): + + params_actual = getattr(request, 'params_{}'.format(location)) + + missing_parameter = _missing_parameter(params_required, params_actual) + if missing_parameter is not None: + raise exceptions.MissingParameterError( + _format_parameter(missing_parameter), + location + ) + + incorrect_parameter = _incorrect_parameter(params_required, params_actual) + if incorrect_parameter is not None: + raise exceptions.IncorrectParameterError( + _format_parameter(incorrect_parameter), + location + ) + |
