#!/bin/python3
# -*- coding: utf-8 -*-
#
# This file is part of INGInious. See the LICENSE and the COPYRIGHTS files for
# more information about the licensing of this file.

import json
import argparse
import inginious_container_api
from inginious_container_api import feedback

parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description='Set a pair of custom key/value feedback fields\n')
parser.add_argument('-j', '--json', help="interprets value as json", action='store_true')
parser.add_argument('key', help="key")
parser.add_argument('value', help="value")
args = parser.parse_args()

is_json = args.json
key = args.key
value = args.value

# Doing the real stuff
if is_json:
    try:
        value = json.loads(value)
    except:
        print("Cannot decode json. Invalid value for --json")
        exit(1)
else:
    # try to convert to int/float if possible
    try:
        value = int(value)
    except:
        try:
            value = float(value)
        except:
            pass

inginious_container_api.feedback.set_custom_value(key, value)
