From 78d882401d5277864222e7197e61259033b215a2 Mon Sep 17 00:00:00 2001 From: Tom Keffer Date: Fri, 24 Nov 2023 16:42:12 -0800 Subject: [PATCH 1/2] Refactor initialize() to allow it to be used globally --- src/weectllib/__init__.py | 17 +---------------- src/weectllib/database_actions.py | 25 +++++++++++++------------ src/weectllib/report_actions.py | 2 +- src/weewx/__init__.py | 20 ++++++++++++++++++++ src/weewxd.py | 15 +++------------ 5 files changed, 38 insertions(+), 41 deletions(-) diff --git a/src/weectllib/__init__.py b/src/weectllib/__init__.py index 4896e400..650fcca6 100644 --- a/src/weectllib/__init__.py +++ b/src/weectllib/__init__.py @@ -6,25 +6,10 @@ """The 'weectllib' package.""" import datetime -import importlib import weecfg -import weeutil.logger import weewx -from weeutil.weeutil import to_int, bcolors - - -def initialize(config_dict): - # Set weewx.debug as necessary: - weewx.debug = to_int(config_dict.get('debug', 0)) - - # Customize the logging with user settings. - weeutil.logger.setup('weectl', config_dict) - - # Add the 'user' package to PYTHONPATH - weewx.add_user_path(config_dict) - # Now we can import user.extensions - importlib.import_module('user.extensions') +from weeutil.weeutil import bcolors def parse_dates(date=None, from_date=None, to_date=None, as_datetime=False): diff --git a/src/weectllib/database_actions.py b/src/weectllib/database_actions.py index 45f04af5..258c061e 100644 --- a/src/weectllib/database_actions.py +++ b/src/weectllib/database_actions.py @@ -12,6 +12,7 @@ import time import weecfg import weectllib import weedb +import weewx import weewx.manager from weeutil.weeutil import bcolors, y_or_n, timestamp_to_string @@ -28,7 +29,7 @@ def create_database(config_path, config_path, config_dict = weecfg.read_config(config_path) print(f"The configuration file {bcolors.BOLD}{config_path}{bcolors.ENDC} will be used.") - weectllib.initialize(config_dict) + weewx.initialize(config_dict, 'weectl') # Try a simple open. If it succeeds, that means the database # exists and is initialized. Otherwise, an exception will be raised. @@ -50,7 +51,7 @@ def drop_daily(config_path, """Drop the daily summary from a WeeWX database.""" config_path, config_dict, database_name = weectllib.prepare(config_path, db_binding, dry_run) - weectllib.initialize(config_dict) + weewx.initialize(config_dict, 'weectl') print(f"Proceeding will delete all your daily summaries from database '{database_name}'") ans = y_or_n("Are you sure you want to proceed (y/n)? ") @@ -88,7 +89,7 @@ def rebuild_daily(config_path, """Rebuild the daily summaries.""" config_path, config_dict, database_name = weectllib.prepare(config_path, db_binding, dry_run) - weectllib.initialize(config_dict) + weewx.initialize(config_dict, 'weectl') # Get any dates the user might have specified. from_d, to_d = weectllib.parse_dates(date, from_date, to_date) @@ -159,7 +160,7 @@ def add_column(config_path, column_type: The type ("REAL"|"INTEGER") of the new column. """ config_path, config_dict, database_name = weectllib.prepare(config_path, db_binding, dry_run) - weectllib.initialize(config_dict) + weewx.initialize(config_dict, 'weectl') column_type = column_type or 'REAL' ans = y_or_n( @@ -183,7 +184,7 @@ def rename_column(config_path, db_binding='wx_binding', dry_run=False): config_path, config_dict, database_name = weectllib.prepare(config_path, db_binding, dry_run) - weectllib.initialize(config_dict) + weewx.initialize(config_dict, 'weectl') ans = y_or_n(f"Rename column '{from_name}' to '{to_name}' " f"in database {database_name}? (y/n) ") @@ -205,7 +206,7 @@ def drop_columns(config_path, dry_run=False): """Drop a set of columns from the database""" config_path, config_dict, database_name = weectllib.prepare(config_path, db_binding, dry_run) - weectllib.initialize(config_dict) + weewx.initialize(config_dict, 'weectl') ans = y_or_n(f"Drop column(s) '{', '.join(column_names)}' from the database? (y/n) ") if ans == 'y': @@ -237,7 +238,7 @@ def reconfigure_database(config_path, same name as the old, except with the suffix _new attached to the end.""" config_path, config_dict, database_name = weectllib.prepare(config_path, db_binding, dry_run) - weectllib.initialize(config_dict) + weewx.initialize(config_dict, 'weectl') manager_dict = weewx.manager.get_manager_dict_from_config(config_dict, db_binding) @@ -322,7 +323,7 @@ def transfer_database(config_path, print("This is a dry run. Nothing will actually be done.") config_path, config_dict = weecfg.read_config(config_path) - weectllib.initialize(config_dict) + weewx.initialize(config_dict, 'weectl') print(f"The configuration file {bcolors.BOLD}{config_path}{bcolors.ENDC} will be used.") @@ -439,7 +440,7 @@ def calc_missing(config_path, import weecfg.database config_path, config_dict, database_name = weectllib.prepare(config_path, db_binding, dry_run) - weectllib.initialize(config_dict) + weewx.initialize(config_dict, 'weectl') log.info("Preparing to calculate missing derived observations...") @@ -533,7 +534,7 @@ def check(config_path, db_binding='wx_binding'): """Check the database for any issues.""" config_path, config_dict, database_name = weectllib.prepare(config_path, db_binding, dry_run=False) - weectllib.initialize(config_dict) + weewx.initialize(config_dict, 'weectl') print("Checking daily summary tables version...") with weewx.manager.open_manager_with_config(config_dict, db_binding) as dbm: @@ -565,7 +566,7 @@ def update_database(config_path, """ config_path, config_dict, database_name = weectllib.prepare(config_path, db_binding, dry_run) - weectllib.initialize(config_dict) + weewx.initialize(config_dict, 'weectl') ans = y_or_n("The update process does not affect archive data, " "but does alter the database.\nContinue (y/n)? ") @@ -655,7 +656,7 @@ def reweight_daily(config_path, """Recalculate the weighted sums in the daily summaries.""" config_path, config_dict, database_name = weectllib.prepare(config_path, db_binding, dry_run) - weectllib.initialize(config_dict) + weewx.initialize(config_dict, 'weectl') # Determine the period over which we are rebuilding from any command line date parameters from_d, to_d = weectllib.parse_dates(date, diff --git a/src/weectllib/report_actions.py b/src/weectllib/report_actions.py index 2736be29..d08b3c35 100644 --- a/src/weectllib/report_actions.py +++ b/src/weectllib/report_actions.py @@ -72,7 +72,7 @@ def run_reports(config_path, config_path, config_dict = weecfg.read_config(config_path) print(f"The configuration file {bcolors.BOLD}{config_path}{bcolors.ENDC} will be used.") - weectllib.initialize(config_dict) + weewx.initialize(config_dict, 'weectl') # We want to generate all reports irrespective of any report_timing settings that may exist. # The easiest way to do this is walk the config dict, resetting any report_timing settings diff --git a/src/weewx/__init__.py b/src/weewx/__init__.py index beba22c9..a99a27a2 100644 --- a/src/weewx/__init__.py +++ b/src/weewx/__init__.py @@ -4,10 +4,14 @@ # See the file LICENSE.txt for your full rights. # """Package weewx, containing modules specific to the weewx runtime engine.""" +import importlib import os.path import sys import time +import weeutil.logger +from weeutil.weeutil import to_int + __version__ = "5.0.0b15" # Holds the program launch time in unix epoch seconds: @@ -219,3 +223,19 @@ def extract_roots(config_dict): pass return root_dict + + +def initialize(config_dict, log_label): + """Set debug, set up the logger, and add the user path""" + global debug + + # Set weewx.debug as necessary: + debug = to_int(config_dict.get('debug', 0)) + + # Customize the logging with user settings. + weeutil.logger.setup(log_label, config_dict) + + # Add the 'user' package to PYTHONPATH + add_user_path(config_dict) + # Now we can import user.extensions + importlib.import_module('user.extensions') diff --git a/src/weewxd.py b/src/weewxd.py index 777db2b5..fc5e5e67 100644 --- a/src/weewxd.py +++ b/src/weewxd.py @@ -87,18 +87,9 @@ def main(): weeutil.logger.log_traceback(log.critical, " **** ") sys.exit(weewx.CMD_ERROR) - # Now that we have the configuration dictionary, we can add the path to the user - # directory to PYTHONPATH. - weewx.add_user_path(config_dict) - # Now we can import user extensions - importlib.import_module('user.extensions') - - # Look for the debug flag. If set, ask for extra logging - weewx.debug = int(config_dict.get('debug', 0)) - - # Now that we have the config_dict and debug setting, we can customize the - # logging with user additions - weeutil.logger.setup(namespace.log_label, config_dict) + # Now that we have the configuration dictionary, we can set up logging, debug, and + # other housekeeping chores: + weewx.initialize(config_dict, namespace.log_label) # Log key bits of information. log.info("Initializing weewx version %s", weewx.__version__) From 04458d712cf03ad48929d5641b09e25c9b1b9ac0 Mon Sep 17 00:00:00 2001 From: Tom Keffer Date: Fri, 24 Nov 2023 17:52:14 -0800 Subject: [PATCH 2/2] More robust detection of unbuilt docs. --- src/weecfg/station_config.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/weecfg/station_config.py b/src/weecfg/station_config.py index fab9ae12..3b2d41ff 100644 --- a/src/weecfg/station_config.py +++ b/src/weecfg/station_config.py @@ -538,19 +538,19 @@ def copy_docs(config_dict, docs_root=None, dry_run=False, force=False): if os.path.isdir(docs_dir) and not force: print(f"Directory {docs_dir} already exists.") return None - with weeutil.weeutil.get_resource_path('weewx_data', 'docs') as docs_resources: - # Check whether new docs are actually available. - if os.path.isdir(docs_resources): - # They are available. Proceed. + # If the documentation has not been built, then a FileNotFoundError will be raised. Be + # prepared to catch it. + try: + with weeutil.weeutil.get_resource_path('weewx_data', 'docs') as docs_resources: print(f"Removing {docs_dir}.") if not dry_run: shutil.rmtree(docs_dir, ignore_errors=True) print(f"Copying new docs into {docs_dir}.") if not dry_run: shutil.copytree(docs_resources, docs_dir) - else: - print(f"{bcolors.WARNING}Documentation not available at {docs_resources}. " - f"Try building them using mkdocs.{bcolors.ENDC}", file=sys.stderr) + except FileNotFoundError: + print(f"{bcolors.WARNING}Documentation not available. " + f"Try building them using mkdocs.{bcolors.ENDC}", file=sys.stderr) return docs_dir