From f294826709fe69222a164acdb8e14c965deafe91 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Wed, 23 Nov 2011 17:33:28 +0100 Subject: Fix formatted output to stdout (or file). --- json_diff.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'json_diff.py') diff --git a/json_diff.py b/json_diff.py index 261acdd..d7d8aeb 100755 --- a/json_diff.py +++ b/json_diff.py @@ -32,6 +32,8 @@ from optparse import OptionParser __author__ = "Matěj Cepl" __version__ = "0.9.2" +import locale + logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s', level=logging.INFO) STYLE_MAP = { @@ -43,8 +45,7 @@ INTERNAL_KEYS = set(STYLE_MAP.keys()) LEVEL_INDENT = u" " -out_str_template = u""" - +out_str_template = u""" %s @@ -121,7 +122,7 @@ class HTMLFormatter(object): return ("".join(out_str)).strip() - def __str__(self): + def __unicode__(self): return self._generate_page(self.diff) class BadJSONError(ValueError): @@ -318,9 +319,9 @@ if __name__ == "__main__": diff = Comparator(open(args[0]), open(args[1]), options.include, options.exclude) if options.HTMLoutput: diff_res = diff.compare_dicts() - # logging.debug("diff_res:\n%s", json.dumps(diff_res, indent=True)) - print(HTMLFormatter(diff_res)) + # we want to hardcode UTF-8 here, because that's what's in element + # of the generated HTML + print(unicode(HTMLFormatter(diff_res)).encode("utf-8")) else: outs = json.dumps(diff.compare_dicts(), indent=4, ensure_ascii=False) - outs = "\n".join([line for line in outs.split("\n")]) - print(outs.encode("utf-8")) + print(outs.encode(locale.getpreferredencoding())) \ No newline at end of file -- cgit