2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
10 # | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
11 # +------------------------------------------------------------------+
13 # This file is part of Check_MK.
14 # The official homepage is at http://mathias-kettner.de/check_mk.
16 # check_mk is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation in version 2. check_mk is distributed
19 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
20 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
21 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
22 # tails. You should have received a copy of the GNU General Public
23 # License along with GNU Make; see the file COPYING. If not, write
24 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 # Boston, MA 02110-1301 USA.
29 # TODO: Refactor all plugins to one way of telling the registry it's name.
30 # for example let all use a static/class method .name().
31 # We could standardize this by making all plugin classes inherit
32 # from a plugin base class instead of "object".
34 # TODO: Decide which base class to implement
35 # (https://docs.python.org/2/library/collections.html) and cleanup
38 class ClassRegistry(object):
39 """The management object for all available plugins of a component.
41 The snapins are loaded by importing cmk.gui.plugins.[component]. These plugins
42 contain subclasses of the cmk.gui.plugins.PluginBase (e.g. SidebarSnpain) class.
44 Entries are registered with this registry using register(), typically via decoration.
47 __metaclass__
= abc
.ABCMeta
50 super(ClassRegistry
, self
).__init
__()
53 # TODO: Make staticmethod (But abc.abstractstaticmethod not available. How to make this possible?)
55 def plugin_base_class(self
):
56 raise NotImplementedError()
59 def plugin_name(self
, plugin_class
):
60 raise NotImplementedError()
62 def registration_hook(self
, plugin_class
):
65 def register(self
, plugin_class
):
66 """Register a class with the registry, can be used as a decorator"""
67 if not issubclass(plugin_class
, self
.plugin_base_class()):
68 raise TypeError('%s is not a subclass of %s' % (plugin_class
.__name
__,
69 self
.plugin_base_class().__name
__))
70 self
.registration_hook(plugin_class
)
71 self
._entries
[self
.plugin_name(plugin_class
)] = plugin_class
74 def __contains__(self
, text
):
75 return text
in self
._entries
77 def __delitem__(self
, key
):
78 del self
._entries
[key
]
80 def __getitem__(self
, key
):
81 return self
._entries
[key
]
84 return len(self
._entries
)
87 return self
._entries
.values()
90 return self
._entries
.items()
93 return self
._entries
.keys()
95 def get(self
, key
, deflt
=None):
96 return self
._entries
.get(key
, deflt
)