]> granicus.if.org Git - icinga2/commitdiff
Add utf8, Windows-1252 and ISO 8859 support for icinga-list-objects
authorJean-Marcel Flach <jean-marcel.flach@netways.de>
Fri, 5 Sep 2014 10:45:32 +0000 (12:45 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Fri, 5 Sep 2014 11:07:44 +0000 (13:07 +0200)
fixes #7042

python/icinga2/commands/list_objects.py
python/icinga2/utils/debug.py

index 1978a547c4f4d96bf16c36e7b5df76b7bd252e2e..bf627cade262d528bd3fb9edb1a73efe760a4356 100644 (file)
@@ -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])
index f4072d6a1e35fa96b87c706c8799aa419bb58c9a..804cd83a650abdf314af5d67535fdebd8d5ccc13 100644 (file)
 
 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))