From 3c66dd9e1e4cdae25be27bbf184fa5cb2aef5738 Mon Sep 17 00:00:00 2001 From: Tom Keffer Date: Sun, 8 Jan 2023 18:03:57 -0800 Subject: [PATCH] Inject user subdirectory in path. Be prepared to catch ModuleNotFound if user does not exist yet --- bin/wee_reports | 5 ++--- bin/weecfg/__init__.py | 10 +++++++--- bin/weecfg/station_config.py | 1 + bin/weewx/__init__.py | 21 ++++++++++++++++++--- bin/weewxd.py | 10 +--------- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/bin/wee_reports b/bin/wee_reports index 8d1a0819..014e6abe 100755 --- a/bin/wee_reports +++ b/bin/wee_reports @@ -8,14 +8,13 @@ import argparse import importlib -import os.path import socket import sys import time import weecfg -import weewxd import weeutil.logger +import weewx import weewx.engine import weewx.manager import weewx.reportengine @@ -93,7 +92,7 @@ def main(): # Now that we have the configuration dictionary, we can add the path to the user # directory to PYTHONPATH. - weewxd.add_user_path(config_dict) + weewx.add_user_path(config_dict) # Now we can import user extensions importlib.import_module('user.extensions') diff --git a/bin/weecfg/__init__.py b/bin/weecfg/__init__.py index f0209f29..45667da7 100644 --- a/bin/weecfg/__init__.py +++ b/bin/weecfg/__init__.py @@ -350,9 +350,13 @@ def get_driver_infos(driver_pkg_name='weewx.drivers'): """ driver_info_dict = {} - # Import the package, so we can find the modules contained within it - print("importing", driver_pkg_name) - driver_pkg = importlib.import_module(driver_pkg_name) + # Import the package, so we can find the modules contained within it. It's possible we are + # trying to import the not-yet-created 'user' subdirectory, which would cause a + # ModuleNotFoundError. Be prepared to catch it. + try: + driver_pkg = importlib.import_module(driver_pkg_name) + except ModuleNotFoundError: + return driver_info_dict driver_path = os.path.dirname(driver_pkg.__file__) # Iterate over all the modules in the package. diff --git a/bin/weecfg/station_config.py b/bin/weecfg/station_config.py index 4a8bf03b..a349f2b4 100644 --- a/bin/weecfg/station_config.py +++ b/bin/weecfg/station_config.py @@ -88,6 +88,7 @@ def config_config(config_dict, driver=None, location=None, user_root=None, no_prompt=False): """Modify a configuration file.""" + weewx.add_user_path(config_dict) config_location(config_dict, location=location, no_prompt=no_prompt) config_altitude(config_dict, altitude=altitude, no_prompt=no_prompt) config_latlon(config_dict, latitude=latitude, longitude=longitude, no_prompt=no_prompt) diff --git a/bin/weewx/__init__.py b/bin/weewx/__init__.py index 687537b8..9b33f28a 100644 --- a/bin/weewx/__init__.py +++ b/bin/weewx/__init__.py @@ -1,13 +1,14 @@ # -# Copyright (c) 2009-2021 Tom Keffer +# Copyright (c) 2009-2023 Tom Keffer # # See the file LICENSE.txt for your full rights. # """Package weewx, containing modules specific to the weewx runtime engine.""" -from __future__ import absolute_import +import os.path +import sys import time -__version__ = "5.0.0a9" +__version__ = "5.0.0a10" # Holds the program launch time in unix epoch seconds: # Useful for calculating 'uptime.' @@ -139,9 +140,23 @@ class Event(object): s = "; ".join("%s: %s" %(k, self.__dict__[k]) for k in self.__dict__ if k!="event_type") return et + s +# ============================================================================= +# Utilities +# ============================================================================= + + def require_weewx_version(module, required_version): """utility to check for version compatibility""" from weeutil.weeutil import version_compare if version_compare(__version__, required_version) < 0: raise UnsupportedFeature("%s requires weewx %s or greater, found %s" % (module, required_version, __version__)) + + +def add_user_path(config_dict): + """add the path to the user directory to PYTHONPATH.""" + user_root = config_dict.get('USER_ROOT', 'bin/user') + lib_dir = os.path.abspath(os.path.join(config_dict['WEEWX_ROOT'], user_root, '..')) + sys.path.append(lib_dir) + + diff --git a/bin/weewxd.py b/bin/weewxd.py index 29dd3554..d6615476 100755 --- a/bin/weewxd.py +++ b/bin/weewxd.py @@ -88,7 +88,7 @@ def main(): # Now that we have the configuration dictionary, we can add the path to the user # directory to PYTHONPATH. - add_user_path(config_dict) + weewx.add_user_path(config_dict) # Now we can import user extensions importlib.import_module('user.extensions') @@ -233,14 +233,6 @@ def main(): raise -def add_user_path(config_dict): - """add the path to the user directory to PYTHONPATH.""" - weewx_root = os.path.dirname(config_dict['config_path']) - user_root = config_dict.get('USER_ROOT', 'bin/user') - lib_dir = os.path.abspath(os.path.join(weewx_root, user_root, '..')) - sys.path.append(lib_dir) - - # ============================================================================== # Signal handlers # ==============================================================================