]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/plugins/__init__.py
6cd03fa95385832b7570535685f0605c2e4c2db8
1 # -*- coding: utf-8 -*-
2 from __future__
import absolute_import
6 from .pluggy
import HookimplMarker
, HookspecMarker
, PluginManager
9 class Interface(object, metaclass
=abc
.ABCMeta
):
17 class DashboardPluginManager(object):
18 def __init__(self
, project_name
):
19 self
.__pm
= PluginManager(project_name
)
20 self
.__add
_spec
= HookspecMarker(project_name
)
21 self
.__add
_abcspec
= lambda *args
, **kwargs
: abc
.abstractmethod(
22 self
.__add
_spec
(*args
, **kwargs
))
23 self
.__add
_hook
= HookimplMarker(project_name
)
25 pm
= property(lambda self
: self
.__pm
)
26 hook
= property(lambda self
: self
.pm
.hook
)
28 add_spec
= property(lambda self
: self
.__add
_spec
)
29 add_abcspec
= property(lambda self
: self
.__add
_abcspec
)
30 add_hook
= property(lambda self
: self
.__add
_hook
)
32 def add_interface(self
, cls
):
33 assert issubclass(cls
, Interface
)
34 self
.pm
.add_hookspecs(cls
)
39 setattr(func
, '__final__', True)
42 def add_plugin(self
, plugin
):
43 """ Provides decorator interface for PluginManager.register():
44 @PLUGIN_MANAGER.add_plugin
47 Additionally it checks whether the Plugin instance has all Interface
48 methods implemented and marked with add_hook decorator.
49 As a con of this approach, plugins cannot call super() from __init__()
51 assert issubclass(plugin
, Interface
)
52 from inspect
import getmembers
, ismethod
53 for interface
in plugin
.__bases
__:
54 for method_name
, _
in getmembers(interface
, predicate
=ismethod
):
55 if hasattr(getattr(interface
, method_name
), '__final__'):
58 if self
.pm
.parse_hookimpl_opts(plugin
, method_name
) is None:
59 raise NotImplementedError(
60 "Plugin '{}' implements interface '{}' but existing"
61 " method '{}' is not declared added as hook".format(
65 self
.pm
.register(plugin())
69 PLUGIN_MANAGER
= DashboardPluginManager("ceph-mgr.dashboard")
71 # Load all interfaces and their hooks
72 from . import interfaces
# noqa pylint: disable=C0413,W0406