From d19184c44b379475a73786da9eae6d2e7d9c207d Mon Sep 17 00:00:00 2001 From: Jean-Marcel Flach Date: Fri, 5 Sep 2014 12:45:32 +0200 Subject: [PATCH] Add utf8, Windows-1252 and ISO 8859 support for icinga-list-objects fixes #7042 --- python/icinga2/commands/list_objects.py | 4 +- python/icinga2/utils/debug.py | 52 +++++++++++++++---------- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/python/icinga2/commands/list_objects.py b/python/icinga2/commands/list_objects.py index 1978a547c..bf627cade 100644 --- a/python/icinga2/commands/list_objects.py +++ b/python/icinga2/commands/list_objects.py @@ -15,7 +15,7 @@ # along with this program; if not, write to the Free Software Foundation # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. -import os, sys, getopt +import os, sys, getopt, codecs from icinga2.utils.debug import ObjectsFileReader from icinga2.config import LocalStateDir from signal import signal, SIGPIPE, SIG_DFL @@ -59,7 +59,7 @@ def main(): fp = open(fname) ofr = ObjectsFileReader(fp) for obj in ofr: - print obj.format(use_colors) + print codecs.encode(obj.format(use_colors), 'utf8') def usage(): print "Syntax: %s [--help|-h] [--color] [file]" % (sys.argv[0]) diff --git a/python/icinga2/utils/debug.py b/python/icinga2/utils/debug.py index f4072d6a1..804cd83a6 100644 --- a/python/icinga2/utils/debug.py +++ b/python/icinga2/utils/debug.py @@ -17,12 +17,22 @@ from icinga2.utils import netstring import subprocess +import codecs try: import json except ImportError: import simplejson as json +def trydecode(inc_string): + try: + return codecs.decode(inc_string, 'utf8') + except UnicodeDecodeError: + try: + return inc_string.decode('Windows-1252') + except UnicodeDecodeError: + return inc_string.decode('ISO 8859-1') + class ConsoleColors(object): @staticmethod def _exec(args): @@ -41,24 +51,24 @@ class DebugObject(object): def format(self, use_colors=False): if self._obj["abstract"]: - result = "Template '" + result = u"Template '" else: - result = "Object '" - result += self._obj["properties"]["__name"] + "' of type '" + self._obj["type"] + "':\n" + result = u"Object '" + result += self._obj["properties"]["__name"] + u"' of type '" + self._obj["type"] + u"':\n" result += self.format_properties(use_colors, 2) return result @staticmethod def format_value(value): if isinstance(value, list): - result = "" + result = u"" for avalue in value: - if result != "": - result += ", " + if result != u"": + result += u", " result += DebugObject.format_value(avalue) - return "[%s]" % (result) + return u"[%s]" % result elif isinstance(value, basestring): - return "'%s'" % (str(value)) + return u"'%s'" % value else: return str(value) @@ -71,19 +81,19 @@ class DebugObject(object): color_begin = _colors.GREEN color_end = _colors.RESET else: - color_begin = '' - color_end = '' + color_begin = u'' + color_end = u'' - result = "" + result = u"" for key, value in props.items(): path.append(key) - result += ' ' * indent + "* %s%s%s" % (color_begin, key, color_end) + result += u' ' * indent + u"* %s%s%s" % (color_begin, key, color_end) hints = self.format_hints(use_colors, indent + 2, path) if isinstance(value, dict): - result += "\n" + hints + result += u"\n" + hints result += self.format_properties(use_colors, indent + 2, path) else: - result += " = %s\n" % (DebugObject.format_value(value)) + result += u" = %s\n" % (DebugObject.format_value(value)) result += hints path.pop() return result @@ -94,18 +104,18 @@ class DebugObject(object): for component in path: dhints = dhints["properties"][component] except KeyError: - return "" + return u"" if use_colors: color_begin = _colors.CYAN color_end = _colors.RESET else: - color_begin = '' - color_end = '' + color_begin = u'' + color_end = u'' - result = "" + result = u"" for message in dhints["messages"]: - result += ' ' * indent + "%% %smodified in %s, lines %s:%s-%s:%s%s\n" % (color_begin, + result += u' ' * indent + u"%% %smodified in %s, lines %s:%s-%s:%s%s\n" % (color_begin, message[1], message[2], message[3], message[4], message[5], color_end) return result @@ -118,9 +128,9 @@ class ObjectsFileReader(object): while True: try: - json_data = fr.readskip() + json_data = trydecode((fr.readskip())) except EOFError: break - if json_data == "": + if json_data == u"": break yield DebugObject(json.loads(json_data))