Skip to content

Commit

Permalink
Add ?format=prettyjson output option, and make sure JSON output has a…
Browse files Browse the repository at this point in the history
… newline.
  • Loading branch information
PeterScott committed Sep 20, 2011
1 parent c691843 commit 42da72c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/greplin/scales/flaskhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def statsHandler(serverName, path=''):
query = request.args.get('query', None)
if outputFormat == 'json':
formats.jsonFormat(output, statDict, query)
elif outputFormat == 'prettyjson':
formats.jsonFormat(output, statDict, query, pretty=True)
else:
formats.htmlHeader(output, '/' + path, serverName, query)
formats.htmlFormat(output, tuple(parts), statDict, query)
Expand Down
6 changes: 4 additions & 2 deletions src/greplin/scales/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ def _htmlRenderDict(pathParts, statDict, output):
output.write('</table>')


def jsonFormat(output, statDict = None, query = None):
def jsonFormat(output, statDict = None, query = None, pretty = False):
"""Formats as JSON, writing to the given object."""
statDict = statDict or scales.getStats()
if query:
statDict = runQuery(statDict, query)
json.dump(statDict, output, cls=scales.StatContainerEncoder)
indent = (pretty and 2) or None
json.dump(statDict, output, cls=scales.StatContainerEncoder, indent=indent)
output.write('\n')
2 changes: 2 additions & 0 deletions src/greplin/scales/tornadohandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def get(self, path): # pylint: disable=W0221
query = self.get_argument('query', default=None)
if outputFormat == 'json':
formats.jsonFormat(self, statDict, query)
elif outputFormat == 'prettyjson':
formats.jsonFormat(self, statDict, query, pretty=True)
else:
formats.htmlHeader(self, '/' + path, self.servername, query)
formats.htmlFormat(self, tuple(parts), statDict, query)
Expand Down
4 changes: 3 additions & 1 deletion src/greplin/scales/twistedweb.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def render_GET(self, request):
if 'format' in request.args and request.args['format'][0] == 'json':
request.headers['content-type'] = 'text/javascript; charset=UTF-8'
formats.jsonFormat(request, statDict, query)

elif 'format' in request.args and request.args['format'][0] == 'prettyjson':
request.headers['content-type'] = 'text/javascript; charset=UTF-8'
formats.jsonFormat(request, statDict, query, pretty=True)
else:
formats.htmlHeader(request, '/' + '/'.join(parts), self.serverName, query)
formats.htmlFormat(request, tuple(parts), statDict, query)
Expand Down

0 comments on commit 42da72c

Please sign in to comment.