mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-24 22:24:44 +02:00
Add utf8, Windows-1252 and ISO 8859 support for icinga-list-objects
fixes #7042
This commit is contained in:
parent
5bb3971f0b
commit
d19184c44b
@ -15,7 +15,7 @@
|
|||||||
# along with this program; if not, write to the Free Software Foundation
|
# along with this program; if not, write to the Free Software Foundation
|
||||||
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
# 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.utils.debug import ObjectsFileReader
|
||||||
from icinga2.config import LocalStateDir
|
from icinga2.config import LocalStateDir
|
||||||
from signal import signal, SIGPIPE, SIG_DFL
|
from signal import signal, SIGPIPE, SIG_DFL
|
||||||
@ -59,7 +59,7 @@ def main():
|
|||||||
fp = open(fname)
|
fp = open(fname)
|
||||||
ofr = ObjectsFileReader(fp)
|
ofr = ObjectsFileReader(fp)
|
||||||
for obj in ofr:
|
for obj in ofr:
|
||||||
print obj.format(use_colors)
|
print codecs.encode(obj.format(use_colors), 'utf8')
|
||||||
|
|
||||||
def usage():
|
def usage():
|
||||||
print "Syntax: %s [--help|-h] [--color] [file]" % (sys.argv[0])
|
print "Syntax: %s [--help|-h] [--color] [file]" % (sys.argv[0])
|
||||||
|
@ -17,12 +17,22 @@
|
|||||||
|
|
||||||
from icinga2.utils import netstring
|
from icinga2.utils import netstring
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import codecs
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import json
|
import json
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import simplejson as json
|
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):
|
class ConsoleColors(object):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _exec(args):
|
def _exec(args):
|
||||||
@ -41,24 +51,24 @@ class DebugObject(object):
|
|||||||
|
|
||||||
def format(self, use_colors=False):
|
def format(self, use_colors=False):
|
||||||
if self._obj["abstract"]:
|
if self._obj["abstract"]:
|
||||||
result = "Template '"
|
result = u"Template '"
|
||||||
else:
|
else:
|
||||||
result = "Object '"
|
result = u"Object '"
|
||||||
result += self._obj["properties"]["__name"] + "' of type '" + self._obj["type"] + "':\n"
|
result += self._obj["properties"]["__name"] + u"' of type '" + self._obj["type"] + u"':\n"
|
||||||
result += self.format_properties(use_colors, 2)
|
result += self.format_properties(use_colors, 2)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def format_value(value):
|
def format_value(value):
|
||||||
if isinstance(value, list):
|
if isinstance(value, list):
|
||||||
result = ""
|
result = u""
|
||||||
for avalue in value:
|
for avalue in value:
|
||||||
if result != "":
|
if result != u"":
|
||||||
result += ", "
|
result += u", "
|
||||||
result += DebugObject.format_value(avalue)
|
result += DebugObject.format_value(avalue)
|
||||||
return "[%s]" % (result)
|
return u"[%s]" % result
|
||||||
elif isinstance(value, basestring):
|
elif isinstance(value, basestring):
|
||||||
return "'%s'" % (str(value))
|
return u"'%s'" % value
|
||||||
else:
|
else:
|
||||||
return str(value)
|
return str(value)
|
||||||
|
|
||||||
@ -71,19 +81,19 @@ class DebugObject(object):
|
|||||||
color_begin = _colors.GREEN
|
color_begin = _colors.GREEN
|
||||||
color_end = _colors.RESET
|
color_end = _colors.RESET
|
||||||
else:
|
else:
|
||||||
color_begin = ''
|
color_begin = u''
|
||||||
color_end = ''
|
color_end = u''
|
||||||
|
|
||||||
result = ""
|
result = u""
|
||||||
for key, value in props.items():
|
for key, value in props.items():
|
||||||
path.append(key)
|
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)
|
hints = self.format_hints(use_colors, indent + 2, path)
|
||||||
if isinstance(value, dict):
|
if isinstance(value, dict):
|
||||||
result += "\n" + hints
|
result += u"\n" + hints
|
||||||
result += self.format_properties(use_colors, indent + 2, path)
|
result += self.format_properties(use_colors, indent + 2, path)
|
||||||
else:
|
else:
|
||||||
result += " = %s\n" % (DebugObject.format_value(value))
|
result += u" = %s\n" % (DebugObject.format_value(value))
|
||||||
result += hints
|
result += hints
|
||||||
path.pop()
|
path.pop()
|
||||||
return result
|
return result
|
||||||
@ -94,18 +104,18 @@ class DebugObject(object):
|
|||||||
for component in path:
|
for component in path:
|
||||||
dhints = dhints["properties"][component]
|
dhints = dhints["properties"][component]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return ""
|
return u""
|
||||||
|
|
||||||
if use_colors:
|
if use_colors:
|
||||||
color_begin = _colors.CYAN
|
color_begin = _colors.CYAN
|
||||||
color_end = _colors.RESET
|
color_end = _colors.RESET
|
||||||
else:
|
else:
|
||||||
color_begin = ''
|
color_begin = u''
|
||||||
color_end = ''
|
color_end = u''
|
||||||
|
|
||||||
result = ""
|
result = u""
|
||||||
for message in dhints["messages"]:
|
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)
|
message[1], message[2], message[3], message[4], message[5], color_end)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@ -118,9 +128,9 @@ class ObjectsFileReader(object):
|
|||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
json_data = fr.readskip()
|
json_data = trydecode((fr.readskip()))
|
||||||
except EOFError:
|
except EOFError:
|
||||||
break
|
break
|
||||||
if json_data == "":
|
if json_data == u"":
|
||||||
break
|
break
|
||||||
yield DebugObject(json.loads(json_data))
|
yield DebugObject(json.loads(json_data))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user