#!/usr/bin/env python3

#
# Development Order #8:
#
# This file is called when perfSonar goes to print the result, which
# has been returned from the tool.
#
# To test this file, a result is needed. A sample one has been provided
# in this directory. Use the following syntax:
# cat example-result.json | ./result-format text/plain
# cat example-result.json | ./result-format text/html
#

import pscheduler

from validate import result_is_valid
from validate import MAX_SCHEMA

TEMPLATE = '''

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

{% for ssid in result.ssid_list %}
SSID {{ ssid.ssid }}:

  Address ..... {{ unspec(ssid.address) }}
  Bitrates .... {% for rate in ssid.bitrates %}{{ siformat(rate) | replace(' ', '') }} {% endfor %}
  Encrypted ... {{ unspec(ssid.encrypted) }}
  Signal ...... {{ unspec(ssid.signal) }}
  Frequency ... {{ unspec(ssid.frequency) }}
  Channel ..... {{ unspec(ssid.channel) }}
  Quality ..... {{ unspec(ssid.quality) }}
  Mode ........ {{ unspec(ssid.mode) }}
{% endfor %}

Elapsed Time ... {{ result.time }}

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

<table>
<tr>
  <th>SSID</th>
  <th>Address</th>
  <th>Bitrates</th>
  <th>Encrypted</th>
  <th>Signal</th>
  <th>Frequency</th>
  <th>Channel</th>
  <th>Quality</th>
  <th>Mode</th>
</tr>
{% for ssid in result.ssid_list %}
<tr>
  <td>{{ ssid.ssid }}</td>
  <td>{{ unspec(ssid.address) }}</td>
  <td>{% for rate in ssid.bitrates %}{{ siformat(rate) | replace(' ', '') }} {% endfor %}</td>
  <td>{{ unspec(ssid.encrypted) }}</td>
  <td>{{ unspec(ssid.signal) }}</td>
  <td>{{ unspec(ssid.frequency) }}</td>
  <td>{{ unspec(ssid.channel) }}</td>
  <td>{{ unspec(ssid.quality) }}</td>
  <td>{{ unspec(ssid.mode) }}</td>
</tr>
{% endfor %}
<tr><td colspan="9">Elapsed Time: {{ result.time }}</td></tr>
</table>

{% else %}

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

{% endif %}
'''

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