Inject user subdirectory in path.

Be prepared to catch ModuleNotFound if user does not exist yet
This commit is contained in:
Tom Keffer
2023-01-08 18:03:57 -08:00
parent 0a8bf30e75
commit 3c66dd9e1e
5 changed files with 29 additions and 18 deletions

View File

@@ -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')

View File

@@ -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.

View File

@@ -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)

View File

@@ -1,13 +1,14 @@
#
# Copyright (c) 2009-2021 Tom Keffer <tkeffer@gmail.com>
# Copyright (c) 2009-2023 Tom Keffer <tkeffer@gmail.com>
#
# 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)

View File

@@ -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
# ==============================================================================