Add utf8, Windows-1252 and ISO 8859 support for icinga-list-objects

fixes #7042
This commit is contained in:
Jean-Marcel Flach 2014-09-05 12:45:32 +02:00 committed by Gunnar Beutner
parent 5bb3971f0b
commit d19184c44b
2 changed files with 33 additions and 23 deletions

View 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])

View File

@ -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))