#!/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 = """
Source ........................ {.section source}{source}{.or}Not Specified{.end}
Source Node ................... {.section source_node}{source_node}{.or}Not Specified{.end}
Destination ................... {.section dest}{dest}{.or}Not Specified{.end}
Port .......................... {.section port}{port}{.or}Not Specified{.end}
"""

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

   template = """
<table>
<tr><td>Source</td><td>{.section source}{source}{.or}Not Specified{.end}</td></tr>
<tr><td>Source Node</td><td>{.section source_node}{source_node}{.or}Not Specified{.end}</td></tr>
<tr><td>Destination</td><td>{.section destination}{destination}{.or}Not Specified{.end}</td></tr>
<tr><td>Port</td><td>{.section port}{port}{.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())
