"""Finds Source subclasses on disk. `desk.sources.build` instantiates them.""" from __future__ import annotations import importlib.util import inspect import logging import pkgutil import sys import hashlib import threading from functools import lru_cache from pathlib import Path from desk.sources.base import Source log = logging.getLogger(__name__) _load_lock = threading.RLock() def _classes_in_module(module) -> list[type[Source]]: found = [] for _, obj in inspect.getmembers(module, inspect.isclass): if not issubclass(obj, Source) or obj is Source: continue if inspect.isabstract(obj): continue # A class written to be subclassed is not a plugin. `RssSource` is # the fetching a publisher's plugin inherits; it has no feeds, no # archive and nothing to switch on. if obj.__dict__.get("shared", False): continue if not getattr(obj, "name", ""): log.warning("%s.%s has no `name`, skipping", module.__name__, obj.__name__) continue # Only take classes defined in this module, not imported into it. if obj.__module__ != module.__name__: continue found.append(obj) return found def _load_file(path: Path) -> list[type[Source]]: path = path.resolve() try: stamp = path.stat() # A concurrent status request must not execute the same plugin twice. with _load_lock: return _load_version(path, stamp.st_mtime_ns, stamp.st_size) except OSError: log.exception("cannot read plugin %s", path) return [] @lru_cache(maxsize=128) def _load_version(path: Path, modified: int, size: int) -> list[type[Source]]: """Reuse unchanged plugins while allowing edits to take effect on next use.""" identity = hashlib.sha256(str(path).encode()).hexdigest()[:16] mod_name = f"newsdesk_plugin_{path.stem}_{identity}" spec = importlib.util.spec_from_file_location(mod_name, path) if spec is None or spec.loader is None: return [] module = importlib.util.module_from_spec(spec) sys.modules[mod_name] = module try: spec.loader.exec_module(module) except Exception: sys.modules.pop(mod_name, None) log.exception("failed to load plugin %s", path) return [] return _classes_in_module(module) def available(plugin_paths: list[str] | None = None) -> dict[str, type[Source]]: """Map of name -> Source class. Later definitions win, so a user plugin named `rss` overrides the built-in one, with a warning naming both classes.""" classes: dict[str, type[Source]] = {} import desk.sources as builtin_pkg for info in pkgutil.iter_modules(builtin_pkg.__path__): if info.name == "base": continue module = importlib.import_module(f"desk.sources.{info.name}") for cls in _classes_in_module(module): classes[cls.name] = cls for raw in plugin_paths or []: directory = Path(raw).expanduser() if not directory.is_dir(): log.warning("plugin_path %s is not a directory", directory) continue for path in sorted(directory.glob("*.py")): if path.name.startswith("_"): continue for cls in _load_file(path): if (previous := classes.get(cls.name)) and previous is not cls: log.warning("plugin name %s: %s.%s overrides %s.%s", cls.name, cls.__module__, cls.__name__, previous.__module__, previous.__name__) classes[cls.name] = cls return classes