Test of upgrade takes unadulterated config file

This commit is contained in:
Tom Keffer
2024-01-03 15:10:54 -08:00
parent 14f3f1c952
commit b75d05cbff
3 changed files with 35 additions and 15 deletions

View File

@@ -144,10 +144,15 @@ class ConfigTest(unittest.TestCase):
"""Test an upgrade against a typical user's configuration file"""
# Start with a typical V2.0 user file:
config_dict = configobj.ConfigObj('weewx20_user.conf', encoding='utf-8')
config_dict = configobj.ConfigObj('weewx20_user.conf',
interpolation=False,
encoding='utf-8',
file_error=True)
# The current config file becomes the template:
template = configobj.ConfigObj(current_config_dict_path, encoding='utf-8')
template = configobj.ConfigObj(current_config_dict_path,
interpolation=False,
encoding='utf-8',
file_error=True)
# First update, then merge:
weecfg.update_config.update_and_merge(config_dict, template)

View File

@@ -964,8 +964,11 @@ def update_to_v50(config_dict):
- If the config file uses '/' for WEEWX_ROOT, set it to '/etc/weewx'
"""
if 'WEEWX_ROOT_CONFIG' in config_dict and config_dict['WEEWX_ROOT_CONFIG'] == '/':
config_dict['WEEWX_ROOT_CONFIG'] = '/etc/weewx'
if 'WEEWX_ROOT' in config_dict:
if config_dict['WEEWX_ROOT'] == '/':
config_dict['WEEWX_ROOT'] = '/etc/weewx'
# Some older versions of weewx.conf use a trailing slash on WEEWX_ROOT. Standardize.
config_dict['WEEWX_ROOT'] = os.path.normpath(config_dict['WEEWX_ROOT'])
config_dict['version'] = '5.0.0'

View File

@@ -771,17 +771,29 @@ def station_upgrade(config_dict, dist_config_path=None, examples_root=None,
print("Nothing done.")
return
# Unless we've been given a path to the new configuration file, retrieve it from
# package resources.
if dist_config_path:
dist_config_dict = configobj.ConfigObj(dist_config_path, encoding='utf-8', file_error=True)
else:
# Retrieve the new configuration file from package resources:
with importlib_resources.open_text('weewx_data', 'weewx.conf', encoding='utf-8') as fd:
dist_config_dict = configobj.ConfigObj(fd, encoding='utf-8', file_error=True)
if 'config' in what:
weecfg.update_config.update_and_merge(config_dict, dist_config_dict)
# Update the config file. Note that the upgrade algorithms take an unadulterated version
# of the dictionaries, so reread them using ConfigObj:
stn_config_dict = configobj.ConfigObj(config_path,
encoding='utf-8',
interpolation=False,
file_error=True)
# Unless we've been given a path to the new configuration file, retrieve it from
# package resources.
if dist_config_path:
dist_config_dict = configobj.ConfigObj(dist_config_path,
encoding='utf-8',
interpolation=False,
file_error=True)
else:
# Retrieve the new configuration file from package resources:
with importlib_resources.open_text('weewx_data', 'weewx.conf', encoding='utf-8') as fd:
dist_config_dict = configobj.ConfigObj(fd,
encoding='utf-8',
interpolation=False,
file_error=True)
weecfg.update_config.update_and_merge(stn_config_dict, dist_config_dict)
print(f"Finished upgrading configuration file {config_path}")
print(f"Saving configuration file {config_path}")
# Save the updated config file with backup