diff --git a/wev/mock_plugin.py b/wev/mock_plugin.py index 949fe5d..a76f6ec 100644 --- a/wev/mock_plugin.py +++ b/wev/mock_plugin.py @@ -1,6 +1,6 @@ from datetime import datetime, timedelta from logging import Logger -from typing import List, Optional, Tuple +from typing import Any, Dict, List, Optional, Tuple from wev.sdk import PluginBase, Resolution, ResolutionSupport from wev.sdk.exceptions import CannotResolveError @@ -21,7 +21,7 @@ class MockPlugin(PluginBase): def __init__( self, - values: dict, + values: Dict[Any, Any], raises_cannot_resolve_error: Optional[bool] = False, return_value: Optional[Tuple[str, ...]] = None, return_expires_at: Optional[bool] = False, @@ -43,3 +43,7 @@ def resolve(self, support: ResolutionSupport) -> Resolution: value=self.return_value, expires_at=self.return_expires_at, ) + + @property + def version(self) -> str: + return "1.2.3" diff --git a/wev/plugins.py b/wev/plugins.py index a38c58f..c2e0d6c 100644 --- a/wev/plugins.py +++ b/wev/plugins.py @@ -25,6 +25,10 @@ def get_plugin(config: PluginConfiguration) -> PluginBase: if len(plugins) > 1: raise MultiplePluginsError(plugin_id=config.id, count=len(plugins)) - get_logger().debug("Instantiating plugin with: %s", config) + logger = get_logger() - return cast(PluginBase, plugins[0].load().Plugin(config)) + logger.debug("Instantiating plugin with: %s", config) + plugin = cast(PluginBase, plugins[0].load().Plugin(config)) + + logger.debug("Instantiated plugin: %s", plugin) + return plugin diff --git a/wev/sdk/exceptions.py b/wev/sdk/exceptions.py index efd567c..6cd93b2 100644 --- a/wev/sdk/exceptions.py +++ b/wev/sdk/exceptions.py @@ -14,8 +14,8 @@ class MissingConfigurationError(Exception): scenarios. """ - def __init__(self, config: dict, key: str, explanation: Optional[str] = None): - message = f"The {key} key is required in {config}" + def __init__(self, key: str, explanation: Optional[str] = None): + message = f"The {key} key is required in this plugin's configuration" message = f"{message}: {explanation}" if explanation else f"{message}." super().__init__(message) diff --git a/wev/sdk/plugin_base.py b/wev/sdk/plugin_base.py index cb62653..dc3c16c 100644 --- a/wev/sdk/plugin_base.py +++ b/wev/sdk/plugin_base.py @@ -1,11 +1,14 @@ from abc import ABC, abstractmethod from logging import Logger -from typing import List +from typing import Any, Dict, List from wev.sdk import Resolution, ResolutionSupport -class PluginBase(ABC, dict): +class PluginBase(ABC, Dict[Any, Any]): + def __str__(self) -> str: + return f"version {self.version}" + @abstractmethod def explain(self, logger: Logger) -> List[str]: """ @@ -23,3 +26,9 @@ def resolve(self, support: ResolutionSupport) -> Resolution: Resolves the environment variable. """ pass + + @property + @abstractmethod + def version(self) -> str: + """ Gets the plugin's version. """ + pass diff --git a/wev/sdk/test_exceptions.py b/wev/sdk/test_exceptions.py index b3031a9..16322d1 100644 --- a/wev/sdk/test_exceptions.py +++ b/wev/sdk/test_exceptions.py @@ -7,16 +7,15 @@ "ex, expect", [ ( - MissingConfigurationError(config={"fooo": "bar"}, key="foo"), - "The foo key is required in {'fooo': 'bar'}.", + MissingConfigurationError(key="foo"), + "The foo key is required in this plugin's configuration.", ), ( MissingConfigurationError( - config={"fooo": "bar"}, key="foo", explanation="bar", ), - "The foo key is required in {'fooo': 'bar'}: bar", + "The foo key is required in this plugin's configuration: bar", ), ], ) diff --git a/wev/test_plugins.py b/wev/test_plugins.py index 9152511..ba15071 100644 --- a/wev/test_plugins.py +++ b/wev/test_plugins.py @@ -7,7 +7,8 @@ def test_get_plugin() -> None: - assert get_plugin(PluginConfiguration({"id": "wev-echo"})) + plugin = get_plugin(PluginConfiguration({"id": "wev-echo"})) + assert plugin.version == "1.0.0" def test_get_plugin__no_match() -> None: diff --git a/wev/wev_echo/plugin.py b/wev/wev_echo/plugin.py index ba068f2..d4b92f8 100644 --- a/wev/wev_echo/plugin.py +++ b/wev/wev_echo/plugin.py @@ -47,7 +47,11 @@ def value(self) -> str: return str(self["value"]) except KeyError as ex: raise MissingConfigurationError( - config=self, explanation="This is the value that will be echoed.", key=str(ex), ) + + @property + def version(self) -> str: + """ Gets the plugin's version. """ + return "1.0.0" diff --git a/wev/wev_echo/test_plugin.py b/wev/wev_echo/test_plugin.py index b676bb3..8c8bbda 100644 --- a/wev/wev_echo/test_plugin.py +++ b/wev/wev_echo/test_plugin.py @@ -22,6 +22,14 @@ def test_resolve__missing_config(resolution_support: ResolutionSupport) -> None: with raises(MissingConfigurationError) as ex: Plugin({"foo": "bar"}).resolve(support=resolution_support) assert str(ex.value) == ( - "The 'value' key is required in {'foo': 'bar'}: " + "The 'value' key is required in this plugin's configuration: " "This is the value that will be echoed." ) + + +def test_version() -> None: + assert Plugin({}).version == "1.0.0" + + +def test_str() -> None: + assert str(Plugin({})) == "version 1.0.0"