#!/usr/bin/env python3

#
# Development Order #9:
#
# This will format a test spec into something that is human readable.
#

import jsontemplate
import pscheduler
import sys

from validate import spec_is_valid


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


json = pscheduler.json_load(exit_on_error=True)

valid, message = spec_is_valid(json)

if not valid:
    pscheduler.fail(message)

# Format a test spec into plain text
if format == 'text/plain':

    template = """
Host        ....... {.section host}{host}{.or}Not Specified{.end}
Source      ....... {.section source}{source}{.or}Not Specified{.end}
Destination ....... {.section dest}{dest}{.or}Not Specified{.end}
Duration    ....... {.section duration}{duration}{.or}Not Specified{.end}
Timeout     ....... {.section timeout}{timeout}{.or}Not Specified{.end}
"""

# Format with html
elif format == 'text/html':

# TODO: This is incomplete.
   template = """
<table>
<tr><td>Timeout</td><td>{.section timeout}{timeout}{.or}Not Specified{.end}</td></tr>
<tr><td>Duration</td><td>{.section duration}{duration}{.or}Not Specified{.end}</td></tr>
</table>
"""

else:

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


# TODO: Should probably handle exceptions in a nicer way.
print(jsontemplate.expand(template, json).strip())
