From 0d807b565a4739859712cff5722ead5c9c878809 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98ystein=20Olai=20Heggen?= Date: Fri, 30 Aug 2019 09:33:55 +0200 Subject: [PATCH] Change validator for ensemble smoother Use error messages from the validators. --- ert_gui/main.py | 31 +++++++++++++++++++------------ tests/global/test_main.py | 4 ++-- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/ert_gui/main.py b/ert_gui/main.py index 9ea382c3c5b..55fe410f84d 100644 --- a/ert_gui/main.py +++ b/ert_gui/main.py @@ -1,13 +1,19 @@ #!/usr/bin/env python import os import sys +import re from argparse import ArgumentParser, ArgumentTypeError from ert_gui import run_cli from ert_gui import ERT -from ert_gui.ide.keywords.definitions import RangeStringArgument, ProperNameFormatArgument, NumberListStringArgument +from ert_gui.ide.keywords.definitions import RangeStringArgument, ProperNameArgument, ProperNameFormatArgument, NumberListStringArgument from ert_gui.simulation.models.multiple_data_assimilation import MultipleDataAssimilation +def strip_error_message_and_raise_exception(validated): + error = validated.message() + error = re.sub(r'\<[^>]*\>', " ", error) + raise ArgumentTypeError(error) + def valid_file(fname): if not os.path.isfile(fname): raise ArgumentTypeError("File was not found: {}".format(fname)) @@ -18,8 +24,7 @@ def valid_realizations(user_input): validator = RangeStringArgument() validated = validator.validate(user_input) if validated.failed(): - raise ArgumentTypeError( - "Defined realizations is not of correct format: {}".format(user_input)) + strip_error_message_and_raise_exception(validated) return user_input @@ -27,9 +32,7 @@ def valid_weights(user_input): validator = NumberListStringArgument() validated = validator.validate(user_input) if validated.failed(): - raise ArgumentTypeError( - "Defined weights is not of correct format: {}".format(user_input)) - + strip_error_message_and_raise_exception(validated) return user_input @@ -37,20 +40,24 @@ def valid_name_format(user_input): validator = ProperNameFormatArgument() validated = validator.validate(user_input) if validated.failed(): - raise ArgumentTypeError( - "Defined name is not of correct format: {}".format(user_input)) + strip_error_message_and_raise_exception(validated) return user_input +def valid_name(user_input): + validator = ProperNameArgument() + validated = validator.validate(user_input) + if validated.failed(): + strip_error_message_and_raise_exception(validated) + return user_input -def valid_name_format_not_default(user_input): +def valid_name_not_default(user_input): if user_input == 'default': msg = "Target file system and source file system can not be the same. "\ "They were both: ." raise ArgumentTypeError(msg) - valid_name_format(user_input) + valid_name(user_input) return user_input - def range_limited_int(user_input): try: i = int(user_input) @@ -109,7 +116,7 @@ def ert_parser(parser, args): ensemble_smoother_parser = subparsers.add_parser('ensemble_smoother', help="run simulations in cli while performing one update on the " "parameters by using the ensemble smoother algorithm") - ensemble_smoother_parser.add_argument('--target-case', type=valid_name_format_not_default, required=True, + ensemble_smoother_parser.add_argument('--target-case', type=valid_name_not_default, required=True, help="This is the name of the case where the results for the " "updated parameters will be stored") ensemble_smoother_parser.add_argument('--verbose', action='store_true', diff --git a/tests/global/test_main.py b/tests/global/test_main.py index d6d30793de8..d02a210d029 100644 --- a/tests/global/test_main.py +++ b/tests/global/test_main.py @@ -43,11 +43,11 @@ def test_argparse_exec_ensemble_experiment_faulty_realizations(self): def test_argparse_exec_ensemble_smoother_valid_case(self): parser = ArgumentParser(prog="test_main") parsed = ert_parser(parser, [ - 'ensemble_smoother', "--target-case", "some_case%d", 'test-data/local/poly_example/poly.ert']) + 'ensemble_smoother', "--target-case", "some_case", 'test-data/local/poly_example/poly.ert']) self.assertEquals(parsed.mode, "ensemble_smoother") self.assertEquals( parsed.config, "test-data/local/poly_example/poly.ert") - self.assertEquals(parsed.target_case, "some_case%d") + self.assertEquals(parsed.target_case, "some_case") self.assertEquals(parsed.func.__name__, "run_cli") self.assertFalse(parsed.verbose)