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

If pygments is installed, produce colorized json output by default. #10

Closed
wants to merge 1 commit into from
Closed
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
35 changes: 32 additions & 3 deletions awscli/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,29 @@ def __call__(self, operation, response, stream=None):


class JSONFormatter(FullyBufferedFormatter):

"""
Formatter to output JSON with ident of 4 spaces
"""
def _format_response(self, operation, response, stream):
json.dump(response, stream, indent=4)


class ColorizedJSONFormatter(JSONFormatter):
"""
Formatter to output colorized JSON using Pygments
"""
def _format_response(self, operation, response, stream):
js = json.dumps(response, indent=4)
if stream.isatty():
import pygments.lexers
lexer = pygments.lexers.get_lexer_by_name('json') # Returns an instance
from pygments.formatters import TerminalFormatter
from pygments import highlight
stream.write(highlight(js, lexer, TerminalFormatter()))
else:
stream.write(js)


class TableFormatter(FullyBufferedFormatter):
"""Pretty print a table from a given response.

Expand Down Expand Up @@ -184,8 +202,19 @@ def _format_response(self, operation, response, stream):

def get_formatter(format_type, args):
if format_type == 'json':
return JSONFormatter(args)
elif format_type == 'text':
#Try to see if we have pygments installed
try:
#Ensure pygments is installed
import pygments.lexers
#Also ensure the JSON lexer is installed.
#Old versions of pygments don't have this installed,
#it's available separately as pygments-json
lexer = pygments.lexers.get_lexer_by_name('json')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the JSON lexer is not installed? Does get_lexer_by_name throw an ImportError?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, indeed you are right. get_lexer_by_name throws a pygments.util.ClassNotFound when the given lexer is not found (see http://pygments.org/docs/api/).

I updated the PR to catch any exception when trying to use pygments, falling back to the straight JSONFormatter if anything exception arise.

return ColorizedJSONFormatter(args)
except Exception,e:
#Default to JSONFormatter if pygments not avail
return JSONFormatter(args)
if format_type == 'text':
return TextFormatter(args)
elif format_type == 'table':
return TableFormatter(args)
Expand Down