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

import pscheduler

from validate import result_is_valid
from validate import MAX_SCHEMA

TEMPLATE = '''
{% if _mime_type == 'text/plain' %}

Response Time ... {{ unspec(result.time) }}
{% if result.status is defined -%}
Status Code ..... {{ unspec(result.status) }}
{%- endif -%}
{% if spec.parse is defined -%}
String Found .... {{ result.found }}
{%- endif -%}
{% if result.headers is defined %}
Headers:   (May be missing hyphens)
{#- TODO: The hyphens get deleted from the keys. #}
{%- for header in result.headers %}
  {{ header }}: {{ result.headers[header] }}
{%- endfor %}
{%- endif %}
{%- if result.content is defined %}
Content:
{{ result.content }}
{%- endif -%}

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

<table>
<tr><td>Response Time</td><td>{{ unspec(result.time) }}</td></tr>
<tr><td>Status Code</td><td>{{ unspec(result.status) }}</td></tr>
{% if spec.parse is defined -%}
<tr><td>String Found</td><td>{{ result.found }}</td></tr>
{%- endif -%}
{% if result.headers is defined %}
<tr><td>Headers</td><td>(May be missing hyphens)<ul>
{#- TODO: The hyphens get deleted from the keys. #}
{%- for header in result.headers %}
  <li>{{ header }}: {{ result.headers[header] }}</li>
{%- endfor %}
</ul></td></tr>
{%- endif %}
{%- if result.content is defined %}
<tr><td>Content</td><td>{{ result.content | e }}</td></tr>
{%- endif %}
</table>

{% else %}

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

{% endif %}
'''

pscheduler.result_format_method(TEMPLATE, max_schema=MAX_SCHEMA, validator=result_is_valid)
