#!/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 argparse
import inginious_container_api.feedback

def check_dict_entry(entry):
    splitted = entry.split("=")
    if len(splitted) != 2:
        raise argparse.ArgumentTypeError("Invalid entry " + entry)
    return splitted[0], splitted[1]

parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, 
                                 description='Set the feedback message of the task or specified problem, from a template.')
parser.add_argument('template', help="Name of the template, without the ending .tpl. The command will first try to parse the file "
                                          "template_name.LANG.tpl, then template_name.tpl, where LANG is the language of the student.")
parser.add_argument('options', help="In the form of option_name=option_value. Options will then be available inside the template as value "
                                             "option_name.", nargs="*", type=check_dict_entry)
parser.add_argument('-a', '--append', help="append to current feedback", action='store_true')
parser.add_argument('-i', '--id', help="problem id", default=None)
args = parser.parse_args()

template_name = args.template
template_options = dict(args.options)
append = args.append
problem = args.id

inginious_container_api.feedback.set_feedback_from_tpl(template_name, template_options, problem, append)
