From 9f9ee575f1b48231cbae2810bb4aa0edf8e4603f Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 9 Mar 2023 01:40:35 +0100 Subject: [PATCH] Added _import_error_ignored_modules. ImportErrors on these entity modules will be ignored when dynamically loading them, since they have optional external dependencies and we shouldn't throw an error if we can't import them. --- platypush/entities/_base.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/platypush/entities/_base.py b/platypush/entities/_base.py index 415ffea15..a0ca8158c 100644 --- a/platypush/entities/_base.py +++ b/platypush/entities/_base.py @@ -4,7 +4,7 @@ import pathlib import types from datetime import datetime from dateutil.tz import tzutc -from typing import Callable, Mapping, Type, Tuple, Any +from typing import Callable, Final, Mapping, Set, Type, Tuple, Any import pkgutil from sqlalchemy import ( @@ -26,6 +26,14 @@ from platypush.message import JSONAble entities_registry: Mapping[Type['Entity'], Mapping] = {} +_import_error_ignored_modules: Final[Set[str]] = {'bluetooth'} +""" +ImportError exceptions will be ignored for these entity submodules when +imported dynamically. An ImportError for these modules means that some optional +requirements are missing, and if those plugins aren't enabled then we shouldn't +fail. +""" + if 'entity' not in Base.metadata: @@ -177,8 +185,15 @@ def _discover_entity_types(): module = types.ModuleType(mod_loader.name) mod_loader.loader.exec_module(module) except Exception as e: - logger.warning(f'Could not import module {modname}') - logger.exception(e) + if ( + isinstance(e, (ImportError, ModuleNotFoundError)) + and modname[len(__package__) + 1 :] in _import_error_ignored_modules + ): + logger.debug(f'Could not import module {modname}') + else: + logger.warning(f'Could not import module {modname}') + logger.exception(e) + continue for _, obj in inspect.getmembers(module):