From a1e4cec94f1015f14dd89a6207a126c35d631c29 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Sun, 11 Feb 2024 13:18:03 -0800 Subject: [PATCH] Log file path to plugin when it fails to load and remove debug statements --- comicapi/comicarchive.py | 54 ++++++++++++++++------ comictaggerlib/ctsettings/plugin_finder.py | 4 +- comictaggerlib/main.py | 1 - 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/comicapi/comicarchive.py b/comicapi/comicarchive.py index 6fd92d9..032e867 100644 --- a/comicapi/comicarchive.py +++ b/comicapi/comicarchive.py @@ -15,6 +15,7 @@ # limitations under the License. from __future__ import annotations +import importlib.util import io import itertools import logging @@ -33,6 +34,7 @@ from comicapi.metadata import Metadata from comictaggerlib.ctversion import version if TYPE_CHECKING: + from importlib.machinery import ModuleSpec from importlib.metadata import EntryPoint logger = logging.getLogger(__name__) @@ -50,7 +52,6 @@ def load_archive_plugins(local_plugins: Sequence[EntryPoint] = tuple()) -> None: builtin: list[type[Archiver]] = [] # A list is used first matching plugin wins for ep in itertools.chain(local_plugins, entry_points(group="comicapi.archiver")): - logger.warning(ep) try: archiver: type[Archiver] = ep.load() if ep.module.startswith("comicapi"): @@ -58,7 +59,14 @@ def load_archive_plugins(local_plugins: Sequence[EntryPoint] = tuple()) -> None: else: archivers.append(archiver) except Exception: - logger.exception("Failed to load archive plugin: %s", ep.name) + try: + spec = importlib.util.find_spec(ep.module) + except ValueError: + spec = None + if spec and spec.has_location: + logger.exception("Failed to load archive plugin: %s from %s", ep.name, spec.origin) + else: + logger.exception("Failed to load archive plugin: %s", ep.name) archivers.extend(builtin) @@ -69,10 +77,13 @@ def load_metadata_plugins(version: str = f"ComicAPI/{version}", local_plugins: S else: from importlib.metadata import entry_points builtin: dict[str, Metadata] = {} - styles: dict[str, Metadata] = {} + styles: dict[str, tuple[Metadata, ModuleSpec | None]] = {} # A dict is used, last plugin wins for ep in itertools.chain(entry_points(group="comicapi.metadata"), local_plugins): - logger.warning(ep) + try: + spec = importlib.util.find_spec(ep.module) + except ValueError: + spec = None try: style: type[Metadata] = ep.load() if style.enabled: @@ -80,19 +91,36 @@ def load_metadata_plugins(version: str = f"ComicAPI/{version}", local_plugins: S builtin[style.short_name] = style(version) else: if style.short_name in styles: - logger.warning( - "Plugin %s is overriding the existing metadata plugin for %s tags", - ep.module, - style.short_name, - ) - styles[style.short_name] = style(version) + if spec and spec.has_location: + logger.warning( + "Plugin %s from %s is overriding the existing metadata plugin for %s tags", + ep.module, + spec.origin, + style.short_name, + ) + else: + logger.warning( + "Plugin %s is overriding the existing metadata plugin for %s tags", + ep.module, + style.short_name, + ) + styles[style.short_name] = (style(version), spec) except Exception: - logger.exception("Failed to load metadata plugin: %s", ep.name) + if spec and spec.has_location: + logger.exception("Failed to load metadata plugin: %s from %s", ep.name, spec.origin) + else: + logger.exception("Failed to load metadata plugin: %s", ep.name) for style_name in set(builtin.keys()).intersection(styles): - logger.warning("Builtin metadata for %s tags are being overridden by a plugin", style_name) + spec = styles[style_name][1] + if spec and spec.has_location: + logger.warning( + "Builtin metadata for %s tags are being overridden by a plugin from %s", style_name, spec.origin + ) + else: + logger.warning("Builtin metadata for %s tags are being overridden by a plugin", style_name) metadata_styles.clear() metadata_styles.update(builtin) - metadata_styles.update(styles) + metadata_styles.update({s[0]: s[1][0] for s in styles.items()}) class ComicArchive: diff --git a/comictaggerlib/ctsettings/plugin_finder.py b/comictaggerlib/ctsettings/plugin_finder.py index 0c93920..f24d60a 100644 --- a/comictaggerlib/ctsettings/plugin_finder.py +++ b/comictaggerlib/ctsettings/plugin_finder.py @@ -12,7 +12,7 @@ import re from collections.abc import Generator from typing import Any, NamedTuple -LOG = logging.getLogger(__name__) +logger = logging.getLogger(__name__) NORMALIZE_PACKAGE_NAME_RE = re.compile(r"[-_.]+") PLUGIN_GROUPS = frozenset(("comictagger.talker", "comicapi.archiver", "comicapi.metadata")) @@ -144,7 +144,7 @@ def _classify_plugins(plugins: list[Plugin]) -> Plugins: elif p.entry_point.group == "comicapi.archiver": archivers.append(p) else: - LOG.warning(NotImplementedError(f"what plugin type? {p}")) + logger.warning(NotImplementedError(f"what plugin type? {p}")) return Plugins( metadata=metadata, diff --git a/comictaggerlib/main.py b/comictaggerlib/main.py index d99365f..ffc7101 100644 --- a/comictaggerlib/main.py +++ b/comictaggerlib/main.py @@ -125,7 +125,6 @@ class App: def load_plugins(self, opts: argparse.Namespace) -> None: local_plugins = plugin_finder.find_plugins(opts.config.user_plugin_dir) self._extend_plugin_paths(local_plugins) - logger.warning(sys.path) comicapi.comicarchive.load_archive_plugins(local_plugins=[p.entry_point for p in local_plugins.archivers]) comicapi.comicarchive.load_metadata_plugins(