Пример #1
0
def get_task_type(job=None,
                  file_cacher=None,
                  dataset=None,
                  task_type_name=None):
    """Given a job, instantiate the corresponding TaskType class.

    job (Job): the job to perform.
    file_cacher (FileCacher): a file cacher object.
    dataset (Dataset): if we don't want to grade, but just to get
                 information, we can provide only the
                 dataset and not the submission.
    task_type_name (string): again, if we only need the class, we can
                             give only the task type name.

    return (object): an instance of the correct TaskType class.

    """
    # Validate arguments.
    if [x is not None
            for x in [job, dataset, task_type_name]].count(True) != 1:
        raise ValueError("Need exactly one way to get the task type.")
    elif [x is not None for x in [job, file_cacher]].count(True) not in [0, 2]:
        raise ValueError("Need file cacher to perform a job.")

    # Recover information from the arguments.
    task_type_parameters = None
    if job is not None:
        task_type_name = job.task_type
    if dataset is not None:
        task_type_name = dataset.task_type
        try:
            task_type_parameters = json.loads(dataset.task_type_parameters)
        except json.decoder.JSONDecodeError as error:
            logger.error("Cannot decode task type parameters.\n%r." % error)
            raise
        job = Job()
        job.task_type = task_type_name
        job.task_type_parameters = task_type_parameters

    cls = plugin_lookup(task_type_name, "cms.grading.tasktypes", "tasktypes")

    return cls(job, file_cacher)