Module: check_mk
Branch: master
Commit: 9b8c6ed9a35be9b365206792078124f2af8d61aa
URL: http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=9b8c6ed9a35be9…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Fri Feb 16 18:34:38 2018 +0100
Moved make_utf8() helper to cmk.utils
Change-Id: I7442d12b0106647aca5489e48e8b498723fe74db
---
cmk/utils.py | 36 ++++++++++++++++++++++++++++++++++++
cmk_base/utils.py | 8 +-------
web/htdocs/html_mod_python.py | 6 +++---
web/htdocs/lib.py | 8 --------
web/htdocs/mkeventd.py | 3 ++-
web/htdocs/userdb.py | 5 +++--
web/htdocs/watolib.py | 11 ++++++-----
7 files changed, 51 insertions(+), 26 deletions(-)
diff --git a/cmk/utils.py b/cmk/utils.py
new file mode 100644
index 0000000..3afbc1c
--- /dev/null
+++ b/cmk/utils.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8; py-indent-offset: 4 -*-
+# +------------------------------------------------------------------+
+# | ____ _ _ __ __ _ __ |
+# | / ___| |__ ___ ___| | __ | \/ | |/ / |
+# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
+# | | |___| | | | __/ (__| < | | | | . \ |
+# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
+# | |
+# | Copyright Mathias Kettner 2014 mk(a)mathias-kettner.de |
+# +------------------------------------------------------------------+
+#
+# This file is part of Check_MK.
+# The official homepage is at http://mathias-kettner.de/check_mk.
+#
+# check_mk is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation in version 2. check_mk is distributed
+# in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
+# out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE. See the GNU General Public License for more de-
+# tails. You should have received a copy of the GNU General Public
+# License along with GNU Make; see the file COPYING. If not, write
+# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+# Boston, MA 02110-1301 USA.
+
+"""This is an unsorted collection of small unrelated helper functions which are
+usable in all components of Check_MK
+
+Please try to find a better place for the things you want to put here."""
+
+def make_utf8(x):
+ if type(x) == unicode:
+ return x.encode('utf-8')
+ else:
+ return x
diff --git a/cmk_base/utils.py b/cmk_base/utils.py
index 1c27cd7..ac6c052 100644
--- a/cmk_base/utils.py
+++ b/cmk_base/utils.py
@@ -32,6 +32,7 @@ import signal
import time
from cmk.exceptions import MKGeneralException, MKTerminate
+from cmk.utils import make_utf8
# TODO: Try to find a better place for them.
@@ -39,13 +40,6 @@ import sys
import itertools
-def make_utf8(x):
- if type(x) == unicode:
- return x.encode('utf-8')
- else:
- return x
-
-
# Aggegates several monitoring states to the worst state
def worst_service_state(*states):
if 2 in states:
diff --git a/web/htdocs/html_mod_python.py b/web/htdocs/html_mod_python.py
index 1320476..1ea9286 100644
--- a/web/htdocs/html_mod_python.py
+++ b/web/htdocs/html_mod_python.py
@@ -25,13 +25,13 @@
# Boston, MA 02110-1301 USA.
from mod_python import Cookie, util, apache # pylint: disable=import-error
-from lib import make_utf8
from gui_exceptions import MKGeneralException, MKException
from log import logger
import htmllib
import os, time, config, weblib, re
import mobile
import cmk.paths
+import cmk.utils
# Is used to end the HTTP request processing from deeper code levels
class FinalizeRequest(Exception):
@@ -176,9 +176,9 @@ class html_mod_python(htmllib.html):
# httponly tells the browser not to make this cookie available to Javascript.
# But it is only available from Python 2.6+. Be compatible.
try:
- c = Cookie.Cookie(varname, make_utf8(value), path='/', httponly=True)
+ c = Cookie.Cookie(varname, cmk.utils.make_utf8(value), path='/', httponly=True)
except AttributeError:
- c = Cookie.Cookie(varname, make_utf8(value), path='/')
+ c = Cookie.Cookie(varname, cmk.utils.make_utf8(value), path='/')
if self.is_ssl_request():
c.secure = True
diff --git a/web/htdocs/lib.py b/web/htdocs/lib.py
index 7c98ba0..5bdd54f 100644
--- a/web/htdocs/lib.py
+++ b/web/htdocs/lib.py
@@ -32,14 +32,6 @@ import cmk.paths
from log import logger
-
-def make_utf8(x):
- if type(x) == unicode:
- return x.encode('utf-8')
- else:
- return x
-
-
# We should use /dev/random here for cryptographic safety. But
# that involves the great problem that the system might hang
# because of loss of entropy. So we hope /dev/urandom is enough.
diff --git a/web/htdocs/mkeventd.py b/web/htdocs/mkeventd.py
index c9da43a..8061acc 100644
--- a/web/htdocs/mkeventd.py
+++ b/web/htdocs/mkeventd.py
@@ -39,6 +39,7 @@ import cmk.paths
import cmk.ec.settings
import cmk.ec.export
import cmk.store
+import cmk.utils
if cmk.is_managed_edition():
import managed
@@ -174,7 +175,7 @@ def send_event(event):
event["ipaddress"], event["application"], event["text"]),
]
- execute_command("CREATE", map(make_utf8, rfc), site=event["site"])
+ execute_command("CREATE", map(cmk.utils.make_utf8, rfc), site=event["site"])
return ";".join(rfc)
diff --git a/web/htdocs/userdb.py b/web/htdocs/userdb.py
index eccbd06..20bcfa0 100644
--- a/web/htdocs/userdb.py
+++ b/web/htdocs/userdb.py
@@ -33,6 +33,7 @@ from gui_exceptions import MKGeneralException, MKUserError, MKInternalError
from log import logger
import time, os, pprint, shutil, traceback
from valuespec import *
+import cmk.utils
import cmk.paths
import cmk.store as store
@@ -648,7 +649,7 @@ def load_users(lock = False):
def custom_attr_path(userid, key):
- return cmk.paths.var_dir + "/web/" + make_utf8(userid) + "/" + key + ".mk"
+ return cmk.paths.var_dir + "/web/" + cmk.utils.make_utf8(userid) + "/" + key + ".mk"
def load_custom_attr(userid, key, conv_func, default = None):
@@ -868,7 +869,7 @@ def _save_auth_serials(updated_profiles):
# Write out the users serials
serials = ""
for user_id, user in updated_profiles.items():
- serials += '%s:%d\n' % (make_utf8(user_id), user.get('serial', 0))
+ serials += '%s:%d\n' % (cmk.utils.make_utf8(user_id), user.get('serial', 0))
store.save_file('%s/auth.serials' % os.path.dirname(cmk.paths.htpasswd_file), serials)
diff --git a/web/htdocs/watolib.py b/web/htdocs/watolib.py
index 46d677a..fd6c44a 100644
--- a/web/htdocs/watolib.py
+++ b/web/htdocs/watolib.py
@@ -64,6 +64,7 @@ from gui_exceptions import MKGeneralException, MKAuthException, MKUserError
import cmk.paths
import cmk.defines
+import cmk.utils
import cmk.store as store
import cmk.render as render
import cmk.ec.defaults
@@ -193,7 +194,7 @@ def log_entry(linkinfo, action, message, user_id=None):
# at the last possible time: When rendering. But this here is the last
# place where we can distinguish between HTML() encapsulated (already)
# escaped / allowed HTML and strings to be escaped.
- message = make_utf8(html.attrencode(message)).strip()
+ message = cmk.utils.make_utf8(html.attrencode(message)).strip()
# linkinfo is either a Folder, or a Host or a hostname or None
if isinstance(linkinfo, Folder):
@@ -603,7 +604,7 @@ class ConfigDomainOMD(ConfigDomain):
p = subprocess.Popen(["omd", "config", "change"],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
stdin=subprocess.PIPE, close_fds=True)
- stdout = p.communicate(make_utf8("\n".join(config_change_commands)))[0]
+ stdout = p.communicate(cmk.utils.make_utf8("\n".join(config_change_commands)))[0]
self._logger.debug(" Exit code: %d" % p.returncode)
self._logger.debug(" Output: %r" % stdout)
if p.returncode != 0:
@@ -7011,7 +7012,7 @@ def check_mk_local_automation(command, args=None, indata="", stdin_data=None, ti
if command in [ 'restart', 'reload' ]:
call_hook_pre_activate_changes()
- cmd = [ make_utf8(a) for a in cmd ]
+ cmd = [ cmk.utils.make_utf8(a) for a in cmd ]
try:
# This debug output makes problems when doing bulk inventory, because
# it garbles the non-HTML response output
@@ -7442,7 +7443,7 @@ class Rulespecs(object):
if group_name.startswith(main_group_name + "/"):
# TODO: Move this subgroup title calculation to some generic place
sub_group_title = group_name.split("/", 1)[1]
- choices.append((make_utf8(group_name), u" ⌙ %s" % sub_group_title))
+ choices.append((cmk.utils.make_utf8(group_name), u" ⌙ %s" % sub_group_title))
return choices
@@ -7462,7 +7463,7 @@ class Rulespecs(object):
group_names = []
for group_name in self._sorted_groups:
- main_group = make_utf8(group_name.split('/')[0])
+ main_group = cmk.utils.make_utf8(group_name.split('/')[0])
if main_group not in seen:
group_names.append(main_group)
seen.add(main_group)