]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/plugins/pluggy.py
import 15.2.0 Octopus source
[ceph.git] / ceph / src / pybind / mgr / dashboard / plugins / pluggy.py
1 # -*- coding: utf-8 -*-
2 """
3 The MIT License (MIT)
4
5 Copyright (c) 2015 holger krekel (rather uses bitbucket/hpk42)
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 """\
25 """
26 CAVEAT:
27 This is a minimal implementation of python-pluggy (based on 0.8.0 interface:
28 https://github.com/pytest-dev/pluggy/releases/tag/0.8.0).
29
30
31 Despite being a widely available Python library, it does not reach all the
32 distros and releases currently targeted for Ceph Nautilus:
33 - CentOS/RHEL 7.5 [ ]
34 - CentOS/RHEL 8 [ ]
35 - Debian 8.0 [ ]
36 - Debian 9.0 [ ]
37 - Ubuntu 14.05 [ ]
38 - Ubuntu 16.04 [X]
39
40 TODO: Once this becomes available in the above distros, this file should be
41 REMOVED, and the fully featured python-pluggy should be used instead.
42 """
43 try:
44 from typing import DefaultDict
45 except ImportError:
46 pass # For typing only
47
48
49 class HookspecMarker(object):
50 """ Dummy implementation. No spec validation. """
51 def __init__(self, project_name):
52 self.project_name = project_name
53
54 def __call__(self, function, *args, **kwargs):
55 """ No options supported. """
56 if any(args) or any(kwargs):
57 raise NotImplementedError(
58 "This is a minimal implementation of pluggy")
59 return function
60
61
62 class HookimplMarker(object):
63 def __init__(self, project_name):
64 self.project_name = project_name
65
66 def __call__(self, function, *args, **kwargs):
67 """ No options supported."""
68 if any(args) or any(kwargs):
69 raise NotImplementedError(
70 "This is a minimal implementation of pluggy")
71 setattr(function, self.project_name + "_impl", {})
72 return function
73
74
75 class _HookRelay(object):
76 """
77 Provides the PluginManager.hook.<method_name>() syntax and
78 functionality.
79 """
80 def __init__(self):
81 from collections import defaultdict
82 self._registry = defaultdict(list) # type: DefaultDict[str, list]
83
84 def __getattr__(self, hook_name):
85 return lambda *args, **kwargs: [
86 hook(*args, **kwargs) for hook in self._registry[hook_name]]
87
88 def _add_hookimpl(self, hook_name, hook_method):
89 self._registry[hook_name].append(hook_method)
90
91
92 class PluginManager(object):
93 def __init__(self, project_name):
94 self.project_name = project_name
95 self.__hook = _HookRelay()
96
97 @property
98 def hook(self):
99 return self.__hook
100
101 def parse_hookimpl_opts(self, plugin, name):
102 return getattr(
103 getattr(plugin, name),
104 self.project_name + "_impl",
105 None)
106
107 def add_hookspecs(self, module_or_class):
108 """ Dummy method"""
109
110 def register(self, plugin, name=None): # pylint: disable=unused-argument
111 for attr in dir(plugin):
112 if self.parse_hookimpl_opts(plugin, attr) is not None:
113 # pylint: disable=protected-access
114 self.hook._add_hookimpl(attr, getattr(plugin, attr))