Skip to content

Commit

Permalink
Log plugin version (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cariad Eccleston authored Jan 16, 2021
1 parent 7ad15fd commit 9f8e976
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 15 deletions.
8 changes: 6 additions & 2 deletions wev/mock_plugin.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand All @@ -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"
8 changes: 6 additions & 2 deletions wev/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions wev/sdk/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
13 changes: 11 additions & 2 deletions wev/sdk/plugin_base.py
Original file line number Diff line number Diff line change
@@ -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]:
"""
Expand All @@ -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
7 changes: 3 additions & 4 deletions wev/sdk/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
),
],
)
Expand Down
3 changes: 2 additions & 1 deletion wev/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 5 additions & 1 deletion wev/wev_echo/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
10 changes: 9 additions & 1 deletion wev/wev_echo/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 9f8e976

Please sign in to comment.