Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for colorized JSON output #2125

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 25 additions & 3 deletions awscli/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,37 @@ def __call__(self, command_name, response, stream=None):

class JSONFormatter(FullyBufferedFormatter):

def __init__(self, args):
super(JSONFormatter, self).__init__(args)

self.colorize = False
try:
if args.color == 'on':
self.colorize = True
except:
pass

def _format_response(self, command_name, response, stream):
# For operations that have no response body (e.g. s3 put-object)
# the response will be an empty string. We don't want to print
# that out to the user but other "falsey" values like an empty
# dictionary should be printed.
if response != {}:
json.dump(response, stream, indent=4, default=json_encoder,
ensure_ascii=False)
stream.write('\n')
if self.colorize:
from pygments import highlight, lexers, formatters
from StringIO import StringIO
io = StringIO()
json.dump(response, io, indent=4, default=json_encoder,
ensure_ascii=False)
colorful_json = highlight(io.getvalue(), lexers.JsonLexer(), formatters.TerminalFormatter())

stream.write(colorful_json)
stream.write('\n')
else:
json.dump(response, stream, indent=4, default=json_encoder,
ensure_ascii=False)

stream.write('\n')


class TableFormatter(FullyBufferedFormatter):
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ docutils>=0.10
-e git://github.com/boto/jmespath.git@develop#egg=jmespath
nose==1.3.0
colorama>=0.2.5,<=0.3.7
pygments==2.1.3
mock==1.3.0
rsa>=3.1.2,<=3.5.0
wheel==0.24.0
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ universal = 1
requires-dist =
botocore==1.4.48
colorama>=0.2.5,<=0.3.7
pygments==2.1.3
docutils>=0.10
rsa>=3.1.2,<=3.5.0
s3transfer>=0.1.0,<0.2.0
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

requires = ['botocore==1.4.48',
'colorama>=0.2.5,<=0.3.7',
'pygments==2.1.3',
'docutils>=0.10',
'rsa>=3.1.2,<=3.5.0',
's3transfer>=0.1.0,<0.2.0']
Expand Down
1 change: 1 addition & 0 deletions tests/unit/customizations/datapipeline/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ def __init__(self, **kwargs):
self.region = None
self.verify_ssl = None
self.output = None
self.color = None
self.query = None
self.__dict__.update(kwargs)

Expand Down