From 7243315bf404d6a304769ed3facec657d76db4b4 Mon Sep 17 00:00:00 2001 From: Tom Keffer Date: Thu, 21 Dec 2023 14:36:32 -0800 Subject: [PATCH] Central dispatch for weectl station --- src/weecfg/station_config.py | 25 +++++++------------------ src/weectllib/station_cmd.py | 15 +++++++++------ 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/weecfg/station_config.py b/src/weecfg/station_config.py index 727d4c40..26c6aab7 100644 --- a/src/weecfg/station_config.py +++ b/src/weecfg/station_config.py @@ -31,7 +31,7 @@ import weeutil.weeutil import weewx from weeutil.weeutil import to_float, to_bool, bcolors -log = logging.getLogger(__name__) +log = logging.getLogger('weectl-station') def station_create(config_path, *args, @@ -96,24 +96,17 @@ def station_create(config_path, *args, return dist_config_dict -def station_reconfigure(config_path, no_backup=False, dry_run=False, *args, **kwargs): +def station_reconfigure(config_dict, no_backup=False, dry_run=False, *args, **kwargs): """Reconfigure an existing station""" - if dry_run: - print("This is a dry run. Nothing will actually be done.") + config_config(config_dict['config_path'], config_dict, dry_run=dry_run, *args, **kwargs) - config_path, config_dict = weecfg.read_config(config_path) - - print(f"Reconfiguring configuration file {bcolors.BOLD}{config_path}{bcolors.ENDC}") - - config_config(config_path, config_dict, dry_run=dry_run, *args, **kwargs) - - print(f"Saving configuration file {config_path}") + print(f"Saving configuration file {config_dict['config_path']}") if dry_run: print("This was a dry run. Nothing was actually done.") else: # Save the results, possibly with a backup. - backup_path = weecfg.save(config_dict, config_path, not no_backup) + backup_path = weecfg.save(config_dict, config_dict['config_path'], not no_backup) if backup_path: print(f"Saved old configuration file as {backup_path}") @@ -673,18 +666,14 @@ def copy_util(config_path, config_dict, dry_run=False, force=False): return util_dir -def station_upgrade(config_path, dist_config_path=None, examples_root=None, +def station_upgrade(config_dict, dist_config_path=None, examples_root=None, skin_root=None, what=None, no_prompt=False, no_backup=False, dry_run=False): """Upgrade the user data for the configuration file found at config_path""" if what is None: what = ('config', 'examples', 'util') - if dry_run: - print("This is a dry run. Nothing will actually be done.") - - # Retrieve the old configuration file as a ConfigObj: - config_path, config_dict = weecfg.read_config(config_path) + config_path = config_dict['config_path'] abbrev = {'config': 'configuration file', 'util': 'utility files'} diff --git a/src/weectllib/station_cmd.py b/src/weectllib/station_cmd.py index 5ef4e298..0f033b73 100644 --- a/src/weectllib/station_cmd.py +++ b/src/weectllib/station_cmd.py @@ -8,6 +8,7 @@ import os.path import sys import weecfg.station_config +import weectllib import weewx from weeutil.weeutil import bcolors @@ -135,7 +136,8 @@ def add_subparser(subparsers): action='store_true', help='Print what would happen, but do not actually ' 'do it.') - station_reconfigure_parser.set_defaults(func=reconfigure_station) + station_reconfigure_parser.set_defaults(func=weectllib.dispatch) + station_reconfigure_parser.set_defaults(action_func=reconfigure_station) # ---------- Action 'upgrade' ---------- station_upgrade_parser = \ @@ -180,7 +182,8 @@ def add_subparser(subparsers): action='store_true', help='Print what would happen, but do not actually ' 'do it.') - station_upgrade_parser.set_defaults(func=upgrade_station) + station_upgrade_parser.set_defaults(func=weectllib.dispatch) + station_upgrade_parser.set_defaults(action_func=upgrade_station) # ============================================================================== @@ -221,10 +224,10 @@ def create_station(namespace): print(f"For example: {bcolors.BOLD}'sudo {path}'{bcolors.ENDC}") -def reconfigure_station(namespace): +def reconfigure_station(config_dict, namespace): """Map namespace to a call to station_reconfigure()""" try: - weecfg.station_config.station_reconfigure(config_path=namespace.config, + weecfg.station_config.station_reconfigure(config_dict=config_dict, driver=namespace.driver, location=namespace.location, altitude=namespace.altitude, @@ -244,8 +247,8 @@ def reconfigure_station(namespace): sys.exit(e) -def upgrade_station(namespace): - weecfg.station_config.station_upgrade(config_path=namespace.config, +def upgrade_station(config_dict, namespace): + weecfg.station_config.station_upgrade(config_dict=config_dict, dist_config_path=namespace.dist_config, examples_root=namespace.examples_root, skin_root=namespace.skin_root,