#!/usr/bin/env python3
#
# Format a spec
#

import pscheduler

from validate import spec_is_valid
from validate import MAX_SCHEMA

TEMPLATE='''
{%- set schema = 1 if schema is undefined else schema -%}
{% if _mime_type == 'text/plain' %}

{%- if schema == 1 -%}
Host ............... {{ unspec(host) }}
Host Node .......... {{ unspec(hostnode) }}
{%- elif schema == 2 -%}
Hosts:
  {%- for hostname in host %}
    {{ hostname }}
  {%- endfor -%}
{%- else -%}
{{ error("Unsupported schema %d" | format(schema)) }}
{%- endif %}
Duration ........... {{ unspec(duration) }}
Starting Comment ... {{ unspec(startingcomment) }}
Parting Comment .... {{ unspec(partingcomment) }}

{% elif _mime_type == 'text/html' %}

<table>
{%- if schema == 1 -%}
<tr><td>Host</td><td>{{ unspec(host) }}</td></tr>
<tr><td>Host Node</td><td>{{ unspec(hostnode) }}</td></tr>
{%- elif schema == 2 -%}
<tr><td>Hosts</td><td><ul>
  {%- for hostname in host %}
    <li>{{ hostname }}</li>
  {%- endfor -%}</ul></td>
{%- else -%}
{{ error("Unsupported schema %d" | format(schema)) }}
{%- endif %}
<tr><td>Duration</td><td>{{ unspec(duration) }}</td></tr>
<tr><td>Starting Comment</td><td>{{ unspec(startingcomment) }}</td></tr>
<tr><td>Parting Comment</td><td>{{ unspec(partingcomment) }}</td></tr>
</table>

{% else %}

{{ error('Unsupported MIME type "' + _mime_type + '"') }}

{% endif %}
'''

pscheduler.spec_format_method(TEMPLATE, max_schema=MAX_SCHEMA, validator=spec_is_valid)
