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

import pscheduler
import jsonschema
from validate import spec_is_valid, REQUEST_SCHEMA

#load spec JSON
spec = pscheduler.json_load(exit_on_error=True, max_schema=1)
if not isinstance(spec, dict):
    pscheduler.fail("Invalid JSON for this operation")

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

#get properties from schema
schema_props = REQUEST_SCHEMA.get('properties', {})
if not schema_props:
    pscheduler.fail("Error retrieving schema properties")

#build command-line from schema
result = []
for prop in schema_props:
    if prop in spec:
        if prop == "schema":
            continue
        elif prop == 'data-ports':
            result.append("--%s" % prop)
            result.append("%d-%d" %
                          (spec[prop]['lower'], spec[prop]['upper']))
        elif schema_props[prop].get('type', '') == "boolean" or schema_props[prop].get('$ref', '') == '#/pScheduler/Boolean':
            result.append("--%s" % prop)
        else:
            result.append("--%s" % prop)
            result.append("%s" % spec[prop])

pscheduler.succeed_json(result)


