]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/plugins/pluggy.py
1 # -*- coding: utf-8 -*-
5 Copyright (c) 2015 holger krekel (rather uses bitbucket/hpk42)
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:
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
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
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).
31 Despite being a widely available Python library, it does not reach all the
32 distros and releases currently targeted for Ceph Nautilus:
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.
44 from typing
import DefaultDict
46 pass # For typing only
49 class HookspecMarker(object):
50 """ Dummy implementation. No spec validation. """
51 def __init__(self
, project_name
):
52 self
.project_name
= project_name
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")
62 class HookimplMarker(object):
63 def __init__(self
, project_name
):
64 self
.project_name
= project_name
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", {})
75 class _HookRelay(object):
77 Provides the PluginManager.hook.<method_name>() syntax and
81 from collections
import defaultdict
82 self
._registry
= defaultdict(list) # type: DefaultDict[str, list]
84 def __getattr__(self
, hook_name
):
85 return lambda *args
, **kwargs
: [
86 hook(*args
, **kwargs
) for hook
in self
._registry
[hook_name
]]
88 def _add_hookimpl(self
, hook_name
, hook_method
):
89 self
._registry
[hook_name
].append(hook_method
)
92 class PluginManager(object):
93 def __init__(self
, project_name
):
94 self
.project_name
= project_name
95 self
.__hook
= _HookRelay()
101 def parse_hookimpl_opts(self
, plugin
, name
):
103 getattr(plugin
, name
),
104 self
.project_name
+ "_impl",
107 def add_hookspecs(self
, module_or_class
):
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
))