#!/usr/bin/python
#
# Convert a test specification to command-line options

import pscheduler
from validate import spec_is_valid, REQUEST_SCHEMA

#load spec JSON
spec = pscheduler.json_load(exit_on_error=True, max_schema=2)
if type(spec) != dict:
    pscheduler.fail("Invalid JSON for this operation")

#validate spec
valid, message = spec_is_valid(spec)
if not valid:
    pscheduler.fail(message)


# With a valid spec in hand, we can simply spit out the values one at
# a time.

result = []

# PYTHON3: This would be spec.items()
for key, value in spec.iteritems():

    # Things that get special handling

    if key == "schema":
        continue

    option = "--%s" % key

    if isinstance(value, bool):
        if value:
            result.append(option)
            continue
        continue

    result.append(option)

    if key == "data-ports":
        result.append("%d-%d" % (value['lower'], value['upper']))
        continue

    result.append(str(value))

pscheduler.succeed_json(result)
