From 80b28562eddb22c1add9ac950e689ee7bc586d93 Mon Sep 17 00:00:00 2001 From: Vipin Balachandran Date: Fri, 18 Nov 2016 15:25:45 +0530 Subject: Pretty print json output Adding kwarg indent in json.dumps() to pretty print the trace output in json format. Change-Id: Icea772bd18e366ad576de7dbc2afc11b2fb376b9 --- osprofiler/cmd/commands.py | 3 ++- osprofiler/tests/cmd/test_shell.py | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/osprofiler/cmd/commands.py b/osprofiler/cmd/commands.py index 99be83e..876493b 100644 --- a/osprofiler/cmd/commands.py +++ b/osprofiler/cmd/commands.py @@ -76,7 +76,8 @@ class TraceCommands(BaseCommand): return obj if args.use_json: - output = json.dumps(trace, default=datetime_json_serialize) + output = json.dumps(trace, default=datetime_json_serialize, + indent=2) elif args.use_html: with open(os.path.join(os.path.dirname(__file__), "template.html")) as html_template: diff --git a/osprofiler/tests/cmd/test_shell.py b/osprofiler/tests/cmd/test_shell.py index fa79c27..fc573ed 100644 --- a/osprofiler/tests/cmd/test_shell.py +++ b/osprofiler/tests/cmd/test_shell.py @@ -174,7 +174,7 @@ class ShellTestCase(test.TestCase): mock_get.return_value = notifications self.run_command("trace show fake_id --json") - self.assertEqual("%s\n" % json.dumps(notifications), + self.assertEqual("%s\n" % json.dumps(notifications, indent=2), sys.stdout.getvalue()) @mock.patch("sys.stdout", six.StringIO()) @@ -222,4 +222,5 @@ class ShellTestCase(test.TestCase): self.run_command("trace show fake_id --json --out='/file'") output = mock_open.return_value.__enter__.return_value - output.write.assert_called_once_with(json.dumps(notifications)) + output.write.assert_called_once_with(json.dumps(notifications, + indent=2)) -- cgit v1.2.1