Module: check_mk
Branch: master
Commit: dff51534ff0dbe2564619c26311e8f77eb0ad485
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=dff51534ff0dbe…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed Jan 9 12:27:22 2019 +0100
Register user configured icons and actions to the icon plugin registry
CMK-1514
Change-Id: I7c023dfdc7e6e22f841956c76daf5e0cbf94cde4
---
cmk/gui/config.py | 17 ++++++++++++++
cmk/gui/plugins/views/icons/__init__.py | 41 ++++++---------------------------
cmk/gui/plugins/views/icons/utils.py | 38 ++++++++++++++++++++++++++++++
3 files changed, 62 insertions(+), 34 deletions(-)
diff --git a/cmk/gui/config.py b/cmk/gui/config.py
index 54943b9..9d0df8d 100644
--- a/cmk/gui/config.py
+++ b/cmk/gui/config.py
@@ -28,6 +28,7 @@ import sys
import errno
import os
import copy
+from typing import Callable # pylint: disable=unused-import
import cmk.gui.utils as utils
import cmk.gui.i18n
@@ -200,6 +201,21 @@ def load_config():
migrate_old_sample_config_tag_groups(wato_host_tags, wato_aux_tags)
+ execute_post_config_load_hooks()
+
+
+def execute_post_config_load_hooks():
+ for func in _post_config_load_hooks:
+ func()
+
+
+_post_config_load_hooks = []
+
+
+def register_post_config_load_hook(func):
+ # type: (Callable) -> None
+ _post_config_load_hooks.append(func)
+
def _initialize_with_default_config():
vars_before_plugins = all_nonfunction_vars(globals())
@@ -764,6 +780,7 @@ class BuiltinTags(object):
# a) Check whether or not the tag group has been modified. If not, simply remove it from
the user
# config and use the builtin tag group in the future.
# b) Extend the tag group in the user configuration with the tag configuration we need
for 1.5.
+# TODO: Move to wato/watolib and register using register_post_config_load_hook()
def migrate_old_sample_config_tag_groups(host_tags, aux_tags_):
remove_old_sample_config_tag_groups(host_tags, aux_tags_)
extend_user_modified_tag_groups(host_tags)
diff --git a/cmk/gui/plugins/views/icons/__init__.py
b/cmk/gui/plugins/views/icons/__init__.py
index 84d58aa..9f470bf 100644
--- a/cmk/gui/plugins/views/icons/__init__.py
+++ b/cmk/gui/plugins/views/icons/__init__.py
@@ -62,17 +62,6 @@ def get_multisite_icons():
for icon_class in icon_and_action_registry.values():
icons[icon_class.ident()] = icon_class()
- # Now apply the global settings customized options
- for icon_id, cfg in config.builtin_icon_visibility.items():
- icon = icon_and_action_registry.get(icon_id)
- if icon is None:
- continue
-
- if 'toplevel' in cfg:
- icon.override_toplevel(cfg['toplevel'])
- if 'sort_index' in cfg:
- icon.override_sort_index(cfg['sort_index'])
-
return icons
@@ -94,7 +83,7 @@ def get_icons(what, row, toplevel):
# Extract needed custom variables
tags = host_custom_vars.get('TAGS', '').split()
- user_action_ids = custom_vars.get('ACTIONS', '').split(',')
+ user_icon_ids = custom_vars.get('ACTIONS', '').split(',')
# Icons is a list of triple or quintuplets with these elements:
# (toplevel, sort_index, html_code)
@@ -103,22 +92,21 @@ def get_icons(what, row, toplevel):
# is written to HTML
# -> or when an exception occured
# (toplevel, sort_index, icon_name, title, url)
- icons = _process_multisite_icons(what, row, tags, host_custom_vars, toplevel)
- icons += _process_custom_user_icons_and_actions(user_action_ids, toplevel)
+ icons = _process_icons(what, row, tags, host_custom_vars, toplevel, user_icon_ids)
return sorted(icons, key=lambda i: i[0])
-def _process_multisite_icons(what, row, tags, custom_vars, toplevel):
+def _process_icons(what, row, tags, custom_vars, toplevel, user_icon_ids):
icons = []
for icon_id, icon in get_multisite_icons().items():
- if icon.type() != 'icon':
+ if icon.toplevel() != toplevel:
continue
- if icon.toplevel() != toplevel:
+ if icon.type() == "custom_icon" and icon_id not in user_icon_ids:
continue
try:
- for icon in _process_multisite_icon(what, row, tags, custom_vars, icon_id,
icon):
+ for icon in _process_icon(what, row, tags, custom_vars, icon_id, icon):
icons.append(icon)
except Exception:
icons.append((icon.sort_index(),
@@ -126,7 +114,7 @@ def _process_multisite_icons(what, row, tags, custom_vars, toplevel):
return icons
-def _process_multisite_icon(what, row, tags, custom_vars, icon_id, icon):
+def _process_icon(what, row, tags, custom_vars, icon_id, icon):
# In old versions, the icons produced html code directly. The new API
# is that the icon functions need to return:
# a) None - nothing to be rendered
@@ -176,21 +164,6 @@ def _process_multisite_icon(what, row, tags, custom_vars, icon_id,
icon):
yield icon.sort_index(), icon_name, title, url
-def _process_custom_user_icons_and_actions(user_action_ids, toplevel):
- icons = []
- for uid in user_action_ids:
- try:
- icon = config.user_icons_and_actions[uid]
- except KeyError:
- continue # Silently skip not existing icons
-
- if icon.get('toplevel', False) == toplevel:
- sort_index = icon.get('sort_index', 15)
- icons.append((sort_index, icon['icon'], icon.get('title'),
icon.get('url')))
-
- return icons
-
-
# toplevel may be
# True to get only columns for top level icons
# False to get only columns for dropdown menu icons
diff --git a/cmk/gui/plugins/views/icons/utils.py b/cmk/gui/plugins/views/icons/utils.py
index 17f41ee..34b5f6e 100644
--- a/cmk/gui/plugins/views/icons/utils.py
+++ b/cmk/gui/plugins/views/icons/utils.py
@@ -27,6 +27,7 @@
import abc
from typing import Union, Optional, Tuple # pylint: disable=unused-import
+import cmk.gui.config as config
import cmk.utils.plugin_registry
@@ -114,3 +115,40 @@ class IconRegistry(cmk.utils.plugin_registry.ClassRegistry):
icon_and_action_registry = IconRegistry()
+
+
+def update_icons_from_configuration():
+ _update_builtin_icons(config.builtin_icon_visibility)
+ _register_custom_user_icons_and_actions(config.user_icons_and_actions)
+
+
+config.register_post_config_load_hook(update_icons_from_configuration)
+
+
+def _update_builtin_icons(builtin_icon_visibility):
+ # Now apply the global settings customized options
+ for icon_id, cfg in builtin_icon_visibility.items():
+ icon = icon_and_action_registry.get(icon_id)
+ if icon is None:
+ continue
+
+ if 'toplevel' in cfg:
+ icon.override_toplevel(cfg['toplevel'])
+ if 'sort_index' in cfg:
+ icon.override_sort_index(cfg['sort_index'])
+
+
+def _register_custom_user_icons_and_actions(user_icons_and_actions):
+ for icon_id, icon_cfg in user_icons_and_actions.items():
+ icon_class = type(
+ "CustomIcon%s" % icon_id.title(), (Icon,), {
+ "_ident": icon_id,
+ "_icon_spec": icon_cfg,
+ "ident": classmethod(lambda cls: cls._ident),
+ "type": classmethod(lambda cls: "custom_icon"),
+ "sort_index": lambda self:
self._icon_spec.get("sort_index", 15),
+ "toplevel": lambda self:
self._icon_spec.get("toplevel", False),
+ "render": lambda self, *args:
(self._icon_spec["icon"], self._icon_spec.get("title"),
self._icon_spec.get("url")),
+ })
+
+ icon_and_action_registry.register_plugin(icon_class)