Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature 1471 fix current field info in output_prefix #1500

Merged
merged 9 commits into from
Mar 9, 2022
36 changes: 35 additions & 1 deletion internal_tests/pytests/config_metplus/test_config_metplus.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,4 +991,38 @@ def test_get_process_list_instances(metplus_config, input_list, expected_list):
conf = metplus_config()
conf.set('config', 'PROCESS_LIST', input_list)
output_list = config_metplus.get_process_list(conf)
assert(output_list == expected_list)
assert(output_list == expected_list)

def test_getraw_sub_and_nosub(metplus_config):
raw_string = '{MODEL}_{CURRENT_FCST_NAME}'
sub_actual = 'FCST_NAME'

config = metplus_config()
config.set('config', 'MODEL', 'FCST')
config.set('config', 'CURRENT_FCST_NAME', 'NAME')
config.set('config', 'OUTPUT_PREFIX', raw_string)
nosub_value = config.getraw('config', 'OUTPUT_PREFIX', sub_vars=False)
assert nosub_value == raw_string

sub_value = config.getraw('config', 'OUTPUT_PREFIX', sub_vars=True)
assert sub_value == sub_actual

def test_getraw_instance_with_unset_var(metplus_config):
"""! Replicates bug where CURRENT_FCST_NAME is substituted with
an empty string when copied from an instance section
"""
pytest.skip()
instance = 'my_section'
config = metplus_config()
config.set('config', 'MODEL', 'FCST')

config.add_section(instance)
config.set('config', 'CURRENT_FCST_NAME', '')
config.set(instance, 'OUTPUT_PREFIX', '{MODEL}_{CURRENT_FCST_NAME}')
new_config = (
config_metplus.replace_config_from_section(config,
instance,
required=False)
)
new_config.set('config', 'CURRENT_FCST_NAME', 'NAME')
assert new_config.getraw('config', 'OUTPUT_PREFIX') == 'FCST_NAME'
18 changes: 7 additions & 11 deletions metplus/util/config_metplus.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,14 +458,14 @@ def replace_config_from_section(config, section, required=True):
for key in all_configs:
new_config.set(section_to_copy,
key,
config.getraw(section_to_copy, key))
config.getraw(section_to_copy, key, sub_vars=False))

# override values in [config] with values from {section}
all_configs = config.keys(section)
for key in all_configs:
new_config.set('config',
key,
config.getraw(section, key))
config.getraw(section, key, sub_vars=False))

return new_config

Expand Down Expand Up @@ -585,7 +585,7 @@ def remove_current_vars(self):
self._conf.remove_option('config', current_var)

# override get methods to perform additional error checking
def getraw(self, sec, opt, default='', count=0):
def getraw(self, sec, opt, default='', count=0, sub_vars=True):
""" parse parameter and replace any existing parameters
referenced with the value (looking in same section, then
config, dir, and os environment)
Expand Down Expand Up @@ -614,6 +614,10 @@ def getraw(self, sec, opt, default='', count=0):
self.check_default(sec, opt, default)
return default

# if not substituting values of other variables return value
if not sub_vars:
return in_template

# get inner-most tags that could potentially be other variables
match_list = re.findall(r'\{([^}{]*)\}', in_template)
for var_name in match_list:
Expand Down Expand Up @@ -1320,14 +1324,6 @@ def check_for_deprecated_met_config(config):
sed_cmds = []
all_good = True

# set CURRENT_* METplus variables in case they are referenced in a
# METplus config variable and not already set
for fcst_or_obs in ['FCST', 'OBS']:
for name_or_level in ['NAME', 'LEVEL']:
current_var = f'CURRENT_{fcst_or_obs}_{name_or_level}'
if not config.has_option('config', current_var):
config.set('config', current_var, '')

# check if *_CONFIG_FILE if set in the METplus config file and check for
# deprecated environment variables in those files
met_config_keys = [key for key in config.keys('config')
Expand Down