Skip to content

Commit ce67efe

Browse files
committed
implement PluginDisabled exception to handle disabled plugins
1 parent 44686b7 commit ce67efe

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

localstack/plugin/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
from .core import Plugin, PluginFinder, PluginLifecycleListener, PluginSpec, PluginType
1+
from .core import (
2+
Plugin,
3+
PluginDisabled,
4+
PluginException,
5+
PluginFinder,
6+
PluginLifecycleListener,
7+
PluginSpec,
8+
PluginType,
9+
)
210
from .manager import PluginManager, PluginSpecResolver
311

412
name = "plugin"
@@ -11,4 +19,6 @@
1119
"PluginFinder",
1220
"PluginManager",
1321
"PluginSpecResolver",
22+
"PluginException",
23+
"PluginDisabled",
1424
]

localstack/plugin/core.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@
33
from typing import Any, Callable, Dict, List, Tuple, Type, Union
44

55

6+
class PluginException(Exception):
7+
def __init__(self, message, namespace: str = None, name: str = None) -> None:
8+
super().__init__(message)
9+
self.namespace = namespace
10+
self.name = name
11+
12+
13+
class PluginDisabled(PluginException):
14+
def __init__(self, namespace: str, name: str):
15+
super(PluginDisabled, self).__init__("plugin %s:%s is disabled" % (namespace, name))
16+
self.namespace = namespace
17+
self.name = name
18+
19+
620
class Plugin(abc.ABC):
721
"""A generic LocalStack plugin.
822

localstack/plugin/manager.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22
import threading
33
from typing import Any, Callable, Dict, Generic, Iterable, List, Tuple, TypeVar, Union
44

5-
from .core import Plugin, PluginFinder, PluginLifecycleListener, PluginSpec, PluginSpecResolver
5+
from .core import (
6+
Plugin,
7+
PluginDisabled,
8+
PluginException,
9+
PluginFinder,
10+
PluginLifecycleListener,
11+
PluginSpec,
12+
PluginSpecResolver,
13+
)
614

715
LOG = logging.getLogger(__name__)
816

@@ -166,8 +174,9 @@ def load(self, name: str) -> P:
166174
raise container.load_error
167175

168176
if not container.is_loaded:
169-
# plugin should_load returned False
170-
raise ValueError("plugin %s:%s is deactivated" % (self.namespace, name))
177+
raise PluginException(
178+
"plugin did not load correctly", namespace=self.namespace, name=name
179+
)
171180

172181
return container.plugin
173182

@@ -186,6 +195,8 @@ def load_all(self, propagate_exceptions=False) -> List[P]:
186195
try:
187196
plugin = self.load(name)
188197
plugins.append(plugin)
198+
except PluginDisabled as e:
199+
LOG.debug("%s", e)
189200
except Exception as e:
190201
if propagate_exceptions:
191202
raise
@@ -251,8 +262,7 @@ def _load_plugin(self, container: PluginContainer):
251262
plugin = container.plugin
252263

253264
if not plugin.should_load():
254-
LOG.debug("not loading deactivated plugin %s", plugin_spec)
255-
return
265+
raise PluginDisabled(namespace=self.namespace, name=container.plugin_spec.name)
256266

257267
args = self.load_args
258268
kwargs = self.load_kwargs

tests/unit/plugin/test_manager.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from localstack.plugin import Plugin, PluginFinder, PluginManager, PluginSpec
5+
from localstack.plugin import Plugin, PluginDisabled, PluginFinder, PluginManager, PluginSpec
66

77

88
class DummyPlugin(Plugin):
@@ -167,4 +167,13 @@ def test_load_all_propagate_exception(self):
167167

168168
ex.match("controlled load fail")
169169

170+
def test_load_disabled_plugin(self, dummy_plugin_finder):
171+
manager = PluginManager("test.plugins.dummy", finder=dummy_plugin_finder)
172+
173+
with pytest.raises(PluginDisabled) as ex:
174+
manager.load("shouldnotload")
175+
176+
assert ex.value.namespace == "test.plugins.dummy"
177+
assert ex.value.name == "shouldnotload"
178+
170179
# TODO: test lifecycle listeners

0 commit comments

Comments
 (0)