#!/usr/bin/env python3

#
# Development Order #8:
#
# This file is called when perfSonar goes to print the result, which
# has been returned from the tool.
#
# To test this file, a result is needed. A sample one has been provided
# in this directory. Use the following syntax:
# cat example-result.json | ./result-format text/plain
# cat example-result.json | ./result-format text/html
#

import jsontemplate
import pscheduler
import sys

from validate import result_is_valid


try:
   format = sys.argv[1]
except IndexError:
   format = 'text/plain'

input = pscheduler.json_load(exit_on_error=True)

valid, message = result_is_valid(input["result"])

if not valid:
    pscheduler.fail(message)

result = input["result"]

if format == 'text/plain':
    # Print results of the test here, in plaintext
    print('MTU: %s\n' % result['mtu'])
   
elif format == 'text/html':
    # Print results of the test here, in html
    print('<table>')
    print('    <tr>')
    print('        <th>MTU</th>')
    print('    </tr>')
    print('    <tr>')
    print('        <td>%s</td>' % result['mtu'])
    print('    </tr>')
    print('</table>')


else:
   pscheduler.fail("Unsupported format '%s'" % format)
