Module: check_mk
Branch: master
Commit: 3cd19edb1e8501d9ecbedd5aec27c1ec20156ef9
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=3cd19edb1e8501…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Thu Dec 13 12:19:54 2018 +0100
Fixed order of method string-related calls: First decode, then strip.
Doing it the other way round is wrong in general and only works by accident
for UTF-8.
Fold the decoding step directly into the file handling in a few places, see
Python's "Unicode HOWTO".
Change-Id: Id6dba07fdd70ac579a7dee6442da9bdcdd47d334
---
cmk/gui/plugins/views/perfometer.py | 2 +-
cmk/gui/wato/pages/audit_log.py | 19 +++---
cmk/gui/watolib.py | 2 +-
cmk/man_pages.py | 129 +++++++++++++++++++-----------------
cmk_base/notify.py | 2 +-
5 files changed, 80 insertions(+), 74 deletions(-)
diff --git a/cmk/gui/plugins/views/perfometer.py b/cmk/gui/plugins/views/perfometer.py
index 29d1f82..dbdc1ac 100644
--- a/cmk/gui/plugins/views/perfometer.py
+++ b/cmk/gui/plugins/views/perfometer.py
@@ -58,7 +58,7 @@ class Perfometer(object):
self._parse_perf_data()
def _parse_perf_data(self):
- perf_data_string =
self._row["service_perf_data"].strip().decode("utf-8")
+ perf_data_string =
self._row["service_perf_data"].decode("utf-8").strip()
if not perf_data_string:
return
diff --git a/cmk/gui/wato/pages/audit_log.py b/cmk/gui/wato/pages/audit_log.py
index 46778f6..b6e1df8 100644
--- a/cmk/gui/wato/pages/audit_log.py
+++ b/cmk/gui/wato/pages/audit_log.py
@@ -25,6 +25,7 @@
# Boston, MA 02110-1301 USA.
"""Handling of the audit logfiles"""
+import codecs
import os
import re
import time
@@ -393,18 +394,18 @@ class ModeAuditLog(WatoMode):
return []
entries = []
- for line in file(self.log_path):
- line = line.rstrip().decode("utf-8")
- splitted = line.split(None, 4)
+ with codecs.open(self.log_path, encoding="utf-8") as fp:
+ for line in fp:
+ splitted = line.rstrip().split(None, 4)
- if len(splitted) == 5 and splitted[0].isdigit():
- splitted[0] = int(splitted[0])
+ if len(splitted) == 5 and splitted[0].isdigit():
+ splitted[0] = int(splitted[0])
- user, action, text = splitted[2:]
- if self._filter_entry(user, action, text):
- continue
+ user, action, text = splitted[2:]
+ if self._filter_entry(user, action, text):
+ continue
- entries.append(splitted)
+ entries.append(splitted)
entries.reverse()
diff --git a/cmk/gui/watolib.py b/cmk/gui/watolib.py
index 659895b..7ea6af7 100644
--- a/cmk/gui/watolib.py
+++ b/cmk/gui/watolib.py
@@ -7512,7 +7512,7 @@ def load_user_scripts_from(adir):
try:
lines = file(path)
lines.next()
- line = lines.next().strip().decode("utf-8")
+ line = lines.next().decode("utf-8").strip()
if line.startswith("#") and "encoding:" in line:
line = lines.next().strip()
if line.startswith("#"):
diff --git a/cmk/man_pages.py b/cmk/man_pages.py
index a6cd908..931d9c5 100644
--- a/cmk/man_pages.py
+++ b/cmk/man_pages.py
@@ -30,6 +30,7 @@ used as base for the list of supported checks and catalogs of checks.
These man pages are in a Check_MK specific format an not real
Linux/Unix man pages"""
+import codecs
import os
import re
import sys
@@ -267,9 +268,10 @@ def print_man_page_table():
def _get_title_from_man_page(path):
- for line in file(path):
- if line.startswith("title:"):
- return line.split(":", 1)[1].strip().decode("utf-8")
+ with codecs.open(path, encoding="utf-8") as fp:
+ for line in fp:
+ if line.startswith("title:"):
+ return line.split(":", 1)[1].strip()
raise MKGeneralException(_("Invalid man page: Failed to get the title"))
@@ -407,16 +409,17 @@ def _run_dialog(args):
def _create_fallback_man_page(name, path, error_message):
- return {
- "name": name,
- "path": path,
- "description": file(path).read().strip().decode("utf-8"),
- "title": _("%s: Cannot parse man page: %s") % (name,
error_message),
- "agents": "",
- "license": "unknown",
- "distribution": "unknown",
- "catalog": ["generic"],
- }
+ with codecs.open(path, encoding="utf-8") as fp:
+ return {
+ "name": name,
+ "path": path,
+ "description": fp.read().strip(),
+ "title": _("%s: Cannot parse man page: %s") % (name,
error_message),
+ "agents": "",
+ "license": "unknown",
+ "distribution": "unknown",
+ "catalog": ["generic"],
+ }
def _parse_man_page_header(name, path):
@@ -426,24 +429,25 @@ def _parse_man_page_header(name, path):
}
key = None
lineno = 0
- for line in file(path):
- line = line.rstrip().decode("utf-8")
- lineno += 1
- try:
- if not line:
- parsed[key] += "\n\n"
- elif line[0] == ' ':
- parsed[key] += "\n" + line.lstrip()
- elif line[0] == '[':
- break # End of header
- else:
- key, rest = line.split(":", 1)
- parsed[key] = rest.lstrip()
- except Exception:
- if cmk.debug.enabled():
- raise
- sys.stderr.write("ERROR: Invalid line %d in man page %s\n%s" %
(lineno, path, line))
- break
+ with codecs.open(path, encoding="utf-8") as fp:
+ for line in fp:
+ line = line.rstrip()
+ lineno += 1
+ try:
+ if not line:
+ parsed[key] += "\n\n"
+ elif line[0] == ' ':
+ parsed[key] += "\n" + line.lstrip()
+ elif line[0] == '[':
+ break # End of header
+ else:
+ key, rest = line.split(":", 1)
+ parsed[key] = rest.lstrip()
+ except Exception:
+ if cmk.debug.enabled():
+ raise
+ sys.stderr.write("ERROR: Invalid line %d in man page %s\n%s" %
(lineno, path, line))
+ break
# verify mandatory keys. FIXME: This list may be incomplete
for key in [
@@ -475,40 +479,41 @@ def load_man_page(name):
man_page['header'] = current_section
empty_line_count = 0
- for lineno, line in enumerate(file(path)):
- try:
- line = line.decode("utf-8")
- if line.startswith(' ') and line.strip() != "": #
continuation line
- empty_line_count = 0
- if current_variable:
- name, curval = current_section[-1]
- if curval.strip() == "":
- current_section[-1] = (name, line.rstrip()[1:])
+ with codecs.open(path, encoding="utf-8") as fp:
+ for lineno, line in enumerate(fp):
+ try:
+ if line.startswith(' ') and line.strip() != "": #
continuation line
+ empty_line_count = 0
+ if current_variable:
+ name, curval = current_section[-1]
+ if curval.strip() == "":
+ current_section[-1] = (name, line.rstrip()[1:])
+ else:
+ current_section[-1] = (name, curval + "\n" +
line.rstrip()[1:])
else:
- current_section[-1] = (name, curval + "\n" +
line.rstrip()[1:])
- else:
- raise Exception
- continue
-
- line = line.strip()
- if line == "":
- empty_line_count += 1
- if empty_line_count == 1 and current_variable:
- name, curval = current_section[-1]
- current_section[-1] = (name, curval + "\n<br>\n")
- continue
- empty_line_count = 0
+ raise Exception
+ continue
+
+ line = line.strip()
+ if line == "":
+ empty_line_count += 1
+ if empty_line_count == 1 and current_variable:
+ name, curval = current_section[-1]
+ current_section[-1] = (name, curval +
"\n<br>\n")
+ continue
+ empty_line_count = 0
- if line[0] == '[' and line[-1] == ']':
- section_header = line[1:-1]
- current_section, current_variable = [], None
- man_page[section_header] = current_section
- else:
- current_variable, restofline = line.split(':', 1)
- current_section.append((current_variable, restofline.lstrip()))
+ if line[0] == '[' and line[-1] == ']':
+ section_header = line[1:-1]
+ current_section, current_variable = [], None
+ man_page[section_header] = current_section
+ else:
+ current_variable, restofline = line.split(':', 1)
+ current_section.append((current_variable, restofline.lstrip()))
- except Exception as e:
- raise MKGeneralException("Syntax error in %s line %d (%s).\n" %
(path, lineno + 1, e))
+ except Exception as e:
+ raise MKGeneralException(
+ "Syntax error in %s line %d (%s).\n" % (path, lineno + 1,
e))
header = {}
for key, value in man_page['header']:
diff --git a/cmk_base/notify.py b/cmk_base/notify.py
index 4247fcb..0293e9d 100755
--- a/cmk_base/notify.py
+++ b/cmk_base/notify.py
@@ -1346,7 +1346,7 @@ def call_notification_script(plugin, plugin_context):
# one - potentially huge - memory buffer
line = p.stdout.readline()
if line != '':
- plugin_log("Output: %s" %
line.rstrip().decode('utf-8'))
+ plugin_log("Output: %s" %
line.decode('utf-8').rstrip())
if _log_to_stdout:
console.output(line)
else: