From f9404720c8db9be29dc22cd11a42e58deb201117 Mon Sep 17 00:00:00 2001 From: George McCabe <23407799+georgemccabe@users.noreply.github.com> Date: Wed, 31 May 2023 15:34:29 -0600 Subject: [PATCH] Bugfix #2189 develop - spaces in complex thresholds (#2191) --- .../config_metplus/test_config_metplus.py | 3 +- .../string_manip/test_util_string_manip.py | 2 ++ .../grid_stat/test_grid_stat_wrapper.py | 6 ++-- metplus/util/config_metplus.py | 16 +++++----- metplus/util/string_manip.py | 32 ++++++++++--------- 5 files changed, 32 insertions(+), 27 deletions(-) diff --git a/internal/tests/pytests/util/config_metplus/test_config_metplus.py b/internal/tests/pytests/util/config_metplus/test_config_metplus.py index 5b374fe011..8dda66dea1 100644 --- a/internal/tests/pytests/util/config_metplus/test_config_metplus.py +++ b/internal/tests/pytests/util/config_metplus/test_config_metplus.py @@ -907,8 +907,7 @@ def test_getraw_instance_with_unset_var(metplus_config): ] ) @pytest.mark.util -def test_format_var_items_options_semicolon(config_value, - expected_result): +def test_format_var_items_options_semicolon(config_value, expected_result): time_info = {} field_configs = {'name': 'FNAME', diff --git a/internal/tests/pytests/util/string_manip/test_util_string_manip.py b/internal/tests/pytests/util/string_manip/test_util_string_manip.py index 22453a53fe..d53f19b469 100644 --- a/internal/tests/pytests/util/string_manip/test_util_string_manip.py +++ b/internal/tests/pytests/util/string_manip/test_util_string_manip.py @@ -128,6 +128,8 @@ def test_threshold(key, value): ("goSFP90", None), ("NA", [('NA', '')]), (",>=,==,!=,<,<=,gt,ge,eq,ne,lt,le and then a number Optionally can have multiple comparison/number pairs separated with && or ||. - Args: - @param thresh_string: String to examine, i.e. <=3.4 - Returns: - None if string does not match any valid comparison operators or does - not contain a number afterwards - regex match object with comparison operator in group 1 and - number in group 2 if valid + + @param thresh_string: String to examine, i.e. <=3.4 + @returns None if string does not match any valid comparison operators + or does not contain a number afterwards. Regex match object with + comparison operator in group 1 and number in group 2 if valid """ comparison_number_list = [] # split thresh string by || or && thresh_split = re.split(r'\|\||&&', thresh_string) # check each threshold for validity - for thresh in thresh_split: + for thresh in [item.strip() for item in thresh_split]: found_match = False for comp in list(VALID_COMPARISONS)+list(VALID_COMPARISONS.values()): # if valid, add to list of tuples @@ -359,14 +357,14 @@ def get_threshold_via_regex(thresh_string): return comparison_number_list -def validate_thresholds(thresh_list): +def validate_thresholds(thresh_list, logger=None): """ Checks list of thresholds to ensure all of them have the correct format Should be a comparison operator with number pair combined with || or && i.e. gt4 or >3&&<5 or gt3||lt1 - Args: - @param thresh_list list of strings to check - Returns: - True if all items in the list are valid format, False if not + + @param thresh_list list of strings to check + @param logger (optional) logging object to output error + @returns True if all items in the list are valid format, False if not """ valid = True for thresh in thresh_list: @@ -375,8 +373,12 @@ def validate_thresholds(thresh_list): valid = False if valid is False: - print("ERROR: Threshold values must use >,>=,==,!=,<,<=,gt,ge,eq,ne,lt, or le with a number, " - "optionally combined with && or ||") + err_str = ("Threshold values must use >,>=,==,!=,<,<=,gt,ge,eq,ne,lt, " + "or le with a number, optionally combined with && or ||") + if logger: + logger.error(err_str) + else: + print(f'ERROR: {err_str}') return False return True