Skip to content

Commit

Permalink
Adding support to output json instead of python dict
Browse files Browse the repository at this point in the history
It's easier to parse json output than python dict.
  • Loading branch information
valleedelisle committed Nov 28, 2019
1 parent 1a96e3c commit 0f913e8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ omit =
exclude_lines =
def console_script()
except ImportError
except json.decoder.JSONDecodeError
if isinstance
1 change: 1 addition & 0 deletions haproxy/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,4 @@ def handle_bad_http_request(self):

if self.raw_http_request != '<BADREQ>':
print('Could not process HTTP request {0}'.format(self.raw_http_request),)

27 changes: 22 additions & 5 deletions haproxy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from haproxy.logfile import Log

import argparse
import datetime
import json
import os
import re

Expand Down Expand Up @@ -76,6 +78,8 @@ def create_parser():
'--list-filters', action='store_true', help='Lists all filters available.'
)

parser.add_argument('--json', action='store_true', help='Output results in json.')

return parser


Expand All @@ -89,6 +93,7 @@ def parse_arguments(args):
'log': None,
'list_commands': None,
'list_filters': None,
'json': None,
}

if args.list_commands:
Expand Down Expand Up @@ -122,6 +127,9 @@ def parse_arguments(args):
_validate_arg_logfile(args.log)
data['log'] = args.log

if args.json is not None:
data['json'] = args.json

return data


Expand Down Expand Up @@ -185,6 +193,11 @@ def _validate_arg_logfile(filename):
raise ValueError('filename {0} does not exist'.format(filepath))


def json_dumps_converter(o):
if isinstance(o, datetime.datetime):
return o.__str__()


def print_commands():
"""Prints all commands available from Log with their
description.
Expand Down Expand Up @@ -219,7 +232,7 @@ def show_help(data):
# make sure that if no arguments are passed the help is shown
show = True
for key in data:
if data[key] is not None and key != 'log':
if data[key] is not None and key not in ('log', 'json'):
show = False
break

Expand Down Expand Up @@ -266,13 +279,17 @@ def main(args):

# run all commands
for command in args['commands']:
string = 'command: {0}'.format(command)
print(string)
print('=' * len(string))
if args['json'] is False:
string = 'command: {0}'.format(command)
print(string)
print('=' * len(string))

cmd = getattr(log_file, 'cmd_{0}'.format(command))
result = cmd()
print(result)
if args['json'] is True:
print(json.dumps(result, default=json_dumps_converter))
else:
print(result)

return log_file # return the log_file object so that tests can inspect it

Expand Down
21 changes: 21 additions & 0 deletions haproxy/tests/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from haproxy.main import VALID_FILTERS
from tempfile import NamedTemporaryFile

import json
import os
import sys
import unittest
Expand Down Expand Up @@ -47,6 +48,13 @@ def tearDown(self):
if filename.endswith('.pickle'):
os.remove('{0}/{1}'.format(path, filename))

def validate_json(self, jsons):
try:
json.loads(jsons)
except json.decoder.JSONDecodeError:
return False
return True

def test_arg_parser_start_invalid(self):
"""Check that if a 'start' argument is not valid an exception is
raised.
Expand Down Expand Up @@ -224,6 +232,19 @@ def test_arg_parser_list_filters_output(self):
for filter_name in VALID_FILTERS:
self.assertIn(filter_name[7:], output_text)

def test_arg_parser_json(self):
"""Test that list filters argument outputs what's expected."""
arguments = ['-l', 'haproxy/tests/files/small.log', '--json', '-c', 'counter']
data = parse_arguments(self.parser.parse_args(arguments))
test_output = NamedTemporaryFile(mode='w', delete=False)

with RedirectStdout(stdout=test_output):
main(data)

with open(test_output.name, 'r') as output_file:
output_text = output_file.read()
self.assertTrue(self.validate_json(output_text))

def test_arg_parser_filters(self):
"""Check that the filter logic on haproxy.main.main works as expected.
"""
Expand Down
1 change: 1 addition & 0 deletions haproxy/tests/test_log_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def test_from_main(self):
'filters': None,
'list_commands': False,
'list_filters': False,
'json': False,
}
logfile = main(data)

Expand Down

0 comments on commit 0f913e8

Please sign in to comment.