Ejemplo n.º 1
0
def test_should_fail_to_load_task_for_not_existent_location(mocker):
    # GIVEN
    non_existant_location = mocker.MagicMock()
    non_existant_location.exists.return_value = False
    locations = [mocker.MagicMock(), non_existant_location]

    # THEN
    with pytest.raises(EmbedevalError):
        # WHEN
        load_tasks(locations)
Ejemplo n.º 2
0
def tasks_cli_command(additional_tasks_path):
    """Show all available tasks"""
    # load all available tasks
    tasks_paths = [__TASKS_DIR__]
    if additional_tasks_path:
        tasks_paths.append(additional_tasks_path)
    load_tasks(tasks_paths)

    # nicely print all available loaded tasks
    print("The following tasks are available for evaluation:")
    for task_name in sorted(task_registry.tasks):
        print(f"    * {cf.bold(task_name)}")
Ejemplo n.º 3
0
def create_tasks(ctx, param, task_names):
    """Create the given Tasks"""
    tasks_paths = [__TASKS_DIR__]
    if ctx.params["tasks_path"] is not None:
        logger.debug("Using additional path to load tasks: %s",
                     ctx.params["tasks_path"])
        tasks_paths.insert(0, Path(ctx.params["tasks_path"]))

    # load all tasks deployed with embedeval
    load_tasks(tasks_paths)

    # create Tasks with the given Embedding
    try:
        tasks = [task_registry.create_task(n) for n in task_names]
    except EmbedevalError as exc:
        print(f"{cf.bold_firebrick('Error:')} {cf.firebrick(exc)}",
              file=sys.stderr)
        raise click.Abort()
    return tasks
Ejemplo n.º 4
0
def test_should_load_python_module_in_location(mocker):
    # GIVEN
    load_module_mock = mocker.patch("embedeval.taskregistry.load_module")
    module_mock = mocker.MagicMock(name="Python Module")
    location = mocker.MagicMock()
    location.glob.return_value = [module_mock]

    # WHEN
    loaded_locations = load_tasks([location])

    # THEN
    load_module_mock.assert_called_once_with(module_mock)
    assert loaded_locations == [module_mock]
Ejemplo n.º 5
0
def create_task_cli_command(is_debug_mode, target_task_path,
                            additional_tasks_path, based_on, new_task_name):
    """Create a new Task for Word Embedding evaluations

    \b
    embedeval ships with a skeleton Task which is used by default.
    Using the ``--based-on`` option changes that skeleton Task to the given Task.

    \b
    Examples:

    \b
        # create a new Task named "rating-analysis" based on the default skeleton Task
        $ embedeval create-task rating-analysis

    \b
        # create a new Task named "rating-analysis" in the directory "tasks" reletive to the execution
        $ embedeval create-task rating-analysis --target-task-path tasks/

    \b
        # create a new Task named "rating-analysis" based on the built-in "offense-detection" Task
        $ embedeval create-task rating-analysis --based-on offense-detection

    \b
        # create a new Task named "rating-analysis-v2"
        # based on a custom "rating-analysis" Task in the "tasks" directory
        $ embedeval create-task rating-analysis-v2 --based-on rating-analysis --tasks-path tasks/
    """  # noqa
    # generate the Python module name for the new Task
    task_module_name = re.sub(r"[^A-Za-z0-9_]", lambda c: "_", new_task_name)
    target_taskfile_path = target_task_path / (task_module_name + ".py")

    if based_on is None:  # based on default Skeleton Task
        base_task_path = __TASKS_DIR__ / "skeleton.py.in"
        with base_task_path.open("r") as skeleton_file:
            target_task_contents = skeleton_file.read().format(
                task_name=new_task_name,
                task_module_name=task_module_name,
            )
    else:
        # load all available tasks
        tasks_paths = [__TASKS_DIR__]
        if additional_tasks_path:
            tasks_paths.append(additional_tasks_path)
        load_tasks(tasks_paths)

        base_task_cls = next(
            (x for x in task_registry.tasks.values() if x.NAME == based_on),
            None)

        if base_task_cls is None:
            print(f"Error: Task {based_on} is not known to embedeval",
                  file=sys.stderr)
            raise click.Abort()

        base_task_path = Path(inspect.getsourcefile(base_task_cls))
        with base_task_path.open("r") as based_on_file:
            target_task_contents = based_on_file.read()

    with target_taskfile_path.open("w") as target_taskfile:
        target_taskfile.write(target_task_contents)

    print(f"Created the new Task '{new_task_name}' based on {base_task_path}")
    print(
        f"Make sure to edit and finalize the new Task at {target_taskfile_path}"
    )