#!/usr/bin/env python3

#
# Development Order #9:
#
# This will format a test spec into something that is human readable.
#
# To test this file, a spec is needed. You can generate one with cli-to-spec
# after you've written it. Use the following syntax:
# cat example-spec.json | ./spec-format text/plain
# cat example-spec.json | ./spec-format text/html
# 
#

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}
Host Node ................... {.section host_ndoe}{host_node}{.or}Not Specified{.end}
Timeout ................... {.section timeout}{timeout}{.or}Not Specified{.end}
Interface ................... {.section interface}{interface}{.or}Not Specified{.end}
"""

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

   template = """
<table>
<tr><td>Host</td><td>{.section host}{host}{.or}Not Specified{.end}</td></tr>
<tr><td>Host Node</td><td>{.section host_node}{host_node}{.or}Not Specified{.end}</td></tr>
<tr><td>Timeout</td><td>{.section timeout}{timeout}{.or}Not Specified{.end}</td></tr>
<tr><td>Interface</td><td>{.section interface}{interface}{.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())
