Module: check_mk
Branch: master
Commit: 888aee983d9c20928ee13cfa796f7df369d11da5
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=888aee983d9c20…
Author: Tom Baerwinkel <tb(a)mathias-kettner.de>
Date: Thu Apr 12 18:07:11 2018 +0200
Remove common base class CustomAttrMode
Change-Id: I866d8fb926a3b69114f72829ecb3d6829952fd2c
---
web/htdocs/wato.py | 115 ++++++++++++++++++++++++-----------------------------
1 file changed, 52 insertions(+), 63 deletions(-)
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index 806df06..248acb5 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -15522,6 +15522,28 @@ class ModePatternEditor(WatoMode):
# '----------------------------------------------------------------------'
+def declare_custom_host_attrs():
+ # First remove all previously registered custom host attributes
+ for attr_name in watolib.all_host_attribute_names():
+ if attr_name not in builtin_host_attribute_names and not\
+ attr_name.startswith("tag_"):
+ watolib.undeclare_host_attribute(attr_name)
+
+ # now declare the custom attributes
+ for attr in config.wato_host_attrs:
+ vs = globals()[attr['type']](title = attr['title'], help =
attr['help'])
+
+ if attr['add_custom_macro']:
+ a = watolib.NagiosValueSpecAttribute(attr["name"], "_" +
attr["name"], vs)
+ else:
+ a = ValueSpecAttribute(attr["name"], vs)
+
+ declare_host_attribute(a,
+ show_in_table = attr['show_in_table'],
+ topic = attr['topic'],
+ )
+
+
def update_user_custom_attrs():
userdb.declare_custom_user_attrs()
userdb.rewrite_users()
@@ -15546,44 +15568,24 @@ def load_custom_attrs_from_mk_file(lock):
return attrs
-class CustomAttrMode(WatoMode):
- def __init__(self):
- super(CustomAttrMode, self).__init__()
-
-
- @classmethod
- def custom_attr_types(cls):
- return [
- ('TextAscii', _('Simple Text')),
- ]
-
-
- def _load_attributes(self, lock=False):
- # TODO: Inappropriate Intimacy: A custom host attribute shouldn't know
- # custom user attributes (and vice versa). The only reason for this
- # is that custom_attrs.mk contains both types.
- self._all_attrs = load_custom_attrs_from_mk_file(lock)
-
-
- def _update_config(self):
- raise NotImplementedError()
-
-
- def _save_attributes(self):
- # TODO: cleanup that the method knows the subclasses 'user' and
'host' implicitely
- output = watolib.wato_fileheader()
- for what in [ "user", "host" ]:
- if what in self._all_attrs and len(self._all_attrs[what]) > 0:
- output += "if type(wato_%s_attrs) != list:\n wato_%s_attrs =
[]\n" % (what, what)
- output += "wato_%s_attrs += %s\n\n" % (what,
pprint.pformat(self._all_attrs[what]))
+def save_custom_attrs_to_mk_file(attrs):
+ output = watolib.wato_fileheader()
+ for what in [ "user", "host" ]:
+ if what in attrs and len(attrs[what]) > 0:
+ output += "if type(wato_%s_attrs) != list:\n wato_%s_attrs =
[]\n" % (what, what)
+ output += "wato_%s_attrs += %s\n\n" % (what,
pprint.pformat(attrs[what]))
- make_nagios_directory(multisite_dir)
- store.save_file(multisite_dir + "custom_attrs.mk", output)
+ make_nagios_directory(multisite_dir)
+ store.save_file(multisite_dir + "custom_attrs.mk", output)
+def custom_attr_types():
+ return [
+ ('TextAscii', _('Simple Text')),
+ ]
-class ModeEditCustomAttr(CustomAttrMode):
+class ModeEditCustomAttr(WatoMode):
def __init__(self, what):
# TODO: move _what to the subclasses.
# Note: _what is used in _from_vars but called in the base class WatoMode.
@@ -15597,11 +15599,15 @@ class ModeEditCustomAttr(CustomAttrMode):
return self._all_attrs[self._what]
+ def _update_config(self):
+ raise NotImplementedError()
+
+
def _from_vars(self):
self._name = html.var("edit") # missing -> new custom attr
self._new = self._name == None
- self._load_attributes(lock=html.is_transaction())
+ self._all_attrs = load_custom_attrs_from_mk_file(lock=html.is_transaction())
if not self._new:
self._attr = [ a for a in self._attrs if a['name'] == self._name ]
@@ -15648,7 +15654,7 @@ class ModeEditCustomAttr(CustomAttrMode):
raise MKUserError("name", _("Sorry, there is already an
attribute with that name."))
ty = html.var('type', '').strip()
- if ty not in [ t[0] for t in self.custom_attr_types() ]:
+ if ty not in [ t[0] for t in custom_attr_types() ]:
raise MKUserError('type', _('The choosen attribute type is
invalid.'))
self._attr = {
@@ -15671,7 +15677,7 @@ class ModeEditCustomAttr(CustomAttrMode):
if self._what == "user":
self._attr['user_editable'] = user_editable
- self._save_attributes()
+ save_custom_attrs_to_mk_file(self._all_attrs)
self._update_config()
return self._what + "_attrs"
@@ -15719,9 +15725,9 @@ class ModeEditCustomAttr(CustomAttrMode):
forms.section(_('Data type'))
html.help(_('The type of information to be stored in this attribute.'))
if self._new:
- html.dropdown('type', self.custom_attr_types(),
deflt=self._attr.get('type'))
+ html.dropdown('type', custom_attr_types(),
deflt=self._attr.get('type'))
else:
- html.write(dict(self.custom_attr_types())[self._attr.get('type')])
+ html.write(dict(custom_attr_types())[self._attr.get('type')])
if self._what == "user":
forms.section(_('Editable by Users'))
@@ -15799,12 +15805,12 @@ class ModeEditCustomHostAttr(ModeEditCustomAttr):
-class ModeCustomAttrs(CustomAttrMode):
+class ModeCustomAttrs(WatoMode):
def __init__(self, what):
# TODO: move _what to the subclasses.
super(ModeCustomAttrs, self).__init__()
self._what = what
- self._load_attributes(lock=html.is_transaction())
+ self._all_attrs = load_custom_attrs_from_mk_file(lock=html.is_transaction())
@property
@@ -15812,6 +15818,10 @@ class ModeCustomAttrs(CustomAttrMode):
return self._all_attrs[self._what]
+ def _update_config(self):
+ raise NotImplementedError()
+
+
def action(self):
if html.var('_delete'):
delname = html.var("_delete")
@@ -15833,7 +15843,7 @@ class ModeCustomAttrs(CustomAttrMode):
for index, attr in enumerate(self._attrs):
if attr['name'] == delname:
self._attrs.pop(index)
- self._save_attributes()
+ save_custom_attrs_to_mk_file(self._all_attrs)
self._update_config()
add_change("edit-%sattrs" % self._what, _("Deleted
attribute %s") % (delname))
elif c == False:
@@ -15858,7 +15868,7 @@ class ModeCustomAttrs(CustomAttrMode):
table.cell(_("Name"), custom_attr['name'])
table.cell(_("Title"), custom_attr['title'])
- table.cell(_("Type"),
dict(ModeEditCustomAttr.custom_attr_types())[custom_attr['type']])
+ table.cell(_("Type"),
dict(custom_attr_types())[custom_attr['type']])
table.end()
@@ -15906,27 +15916,6 @@ class ModeCustomHostAttrs(ModeCustomAttrs):
-def declare_custom_host_attrs():
- # First remove all previously registered custom host attributes
- for attr_name in watolib.all_host_attribute_names():
- if attr_name not in builtin_host_attribute_names and not\
- attr_name.startswith("tag_"):
- watolib.undeclare_host_attribute(attr_name)
-
- # now declare the custom attributes
- for attr in config.wato_host_attrs:
- vs = globals()[attr['type']](title = attr['title'], help =
attr['help'])
-
- if attr['add_custom_macro']:
- a = watolib.NagiosValueSpecAttribute(attr["name"], "_" +
attr["name"], vs)
- else:
- a = ValueSpecAttribute(attr["name"], vs)
-
- declare_host_attribute(a,
- show_in_table = attr['show_in_table'],
- topic = attr['topic'],
- )
-
#.
# .--Check Plugins-------------------------------------------------------.
# | ____ _ _ ____ _ _ |