From 495b8df0ac90e4c6653cbc1733489e8039a4de6c Mon Sep 17 00:00:00 2001 From: dgw Date: Thu, 23 Jun 2022 13:43:25 -0500 Subject: [PATCH 1/3] plugins.handlers: override get_version() for EntryPointPlugin The `_module` of an entry point plugin is unlikely to have its own `__version__`. What's important is the version of the package from which that entry point was loaded. --- sopel/plugins/handlers.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/sopel/plugins/handlers.py b/sopel/plugins/handlers.py index dfd5d61c61..b91977b5e2 100644 --- a/sopel/plugins/handlers.py +++ b/sopel/plugins/handlers.py @@ -51,6 +51,9 @@ import os from typing import Optional +# TODO: refactor along with usage in sopel.__init__ in py3.8+ world +import importlib_metadata + from sopel import __version__ as release, loader from . import exceptions @@ -582,6 +585,19 @@ def __init__(self, entry_point): def load(self): self._module = self.entry_point.load() + def get_version(self) -> Optional[str]: + """Retrieve the plugin's version. + + :return: the plugin's version string + :rtype: Optional[str] + """ + version: Optional[str] = None + + if hasattr(self._module, "__package__"): + version = importlib_metadata.version(self._module.__package__) + + return version + def get_meta_description(self): """Retrieve a meta description for the plugin. From f380fb9986daf367f22c0e1623bdda79df0a209a Mon Sep 17 00:00:00 2001 From: dgw Date: Mon, 27 Jun 2022 17:42:03 -0500 Subject: [PATCH 2/3] fix error case (empty __package__ attribute) --- sopel/plugins/handlers.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sopel/plugins/handlers.py b/sopel/plugins/handlers.py index b91977b5e2..cfa774d2b2 100644 --- a/sopel/plugins/handlers.py +++ b/sopel/plugins/handlers.py @@ -594,7 +594,11 @@ def get_version(self) -> Optional[str]: version: Optional[str] = None if hasattr(self._module, "__package__"): - version = importlib_metadata.version(self._module.__package__) + try: + version = importlib_metadata.version(self._module.__package__) + except ValueError: + # package name is probably empty-string; just give up + pass return version From 89f4aea9aeeab026f95c8774d72bfd7eb7bf3dfa Mon Sep 17 00:00:00 2001 From: dgw Date: Sun, 3 Jul 2022 11:56:16 -0700 Subject: [PATCH 3/3] plugins.handlers: entrypoint starts with module version if available The other way to do this would be what was there before this change, plus an added `if version is None:` that then calls on `super()`. I'm just not convinced that that would be better, and it's more LoC. Plus, in theory, an entrypoint's module could have its own separate version number that differs from the package's version, and this order of operations allows showing the more specific one if it's set. --- sopel/plugins/handlers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sopel/plugins/handlers.py b/sopel/plugins/handlers.py index cfa774d2b2..c22de8ccb7 100644 --- a/sopel/plugins/handlers.py +++ b/sopel/plugins/handlers.py @@ -591,9 +591,9 @@ def get_version(self) -> Optional[str]: :return: the plugin's version string :rtype: Optional[str] """ - version: Optional[str] = None + version: Optional[str] = super().get_version() - if hasattr(self._module, "__package__"): + if version is None and hasattr(self._module, "__package__"): try: version = importlib_metadata.version(self._module.__package__) except ValueError: