Ejemplo n.º 1
0
def test_sample_data_triggers_reader_dialog(mock_npe2_pm, tmp_reader,
                                            make_napari_viewer):
    """Sample data pops reader dialog if multiple compatible readers"""
    # make two tmp readers that take tif files
    tmp_reader(mock_npe2_pm, 'tif-reader', filename_patterns=['*.tif'])
    tmp_reader(mock_npe2_pm, 'other-tif-reader', filename_patterns=['*.tif'])

    # make a sample data reader for tif file
    tmp_sample_plugin = DynamicPlugin('sample-plugin', mock_npe2_pm)
    my_sample = SampleDataURI(
        key='tmp-sample',
        display_name='Temp Sample',
        uri='some-path/some-file.tif',
    )
    tmp_sample_plugin.manifest.contributions.sample_data = [my_sample]
    tmp_sample_plugin.register()

    viewer = make_napari_viewer()
    sample_action = viewer.window.file_menu.open_sample_menu.actions()[0]
    with mock.patch(
            'napari._qt.menus.file_menu.handle_gui_reading') as mock_read:
        sample_action.trigger()

    # assert that handle gui reading was called
    mock_read.assert_called_once()
Ejemplo n.º 2
0
    def make_plugin(
        pm,
        name,
        filename_patterns=['*.fake'],
    ):
        reader_plugin = DynamicPlugin(name, plugin_manager=pm)

        @reader_plugin.contribute.reader(filename_patterns=filename_patterns)
        def read_func(pth):
            ...

        reader_plugin.register()
        return reader_plugin
Ejemplo n.º 3
0
    def make_plugin(
        pm, name, filename_patterns=['*.fake'], accepts_directories=False
    ):
        reader_plugin = DynamicPlugin(name, plugin_manager=pm)

        @reader_plugin.contribute.reader(
            filename_patterns=filename_patterns,
            accepts_directories=accepts_directories,
        )
        def read_func(pth):
            ...

        reader_plugin.register()
        return reader_plugin
Ejemplo n.º 4
0
def test_sample_data_triggers_reader_dialog(
    make_napari_viewer, tmp_plugin: DynamicPlugin
):
    """Sample data pops reader dialog if multiple compatible readers"""
    # make two tmp readers that take tif files
    tmp2 = tmp_plugin.spawn(register=True)

    @tmp_plugin.contribute.reader(filename_patterns=['*.tif'])
    def _(path):
        ...

    @tmp2.contribute.reader(filename_patterns=['*.tif'])
    def _(path):
        ...

    # make a sample data reader for tif file
    my_sample = SampleDataURI(
        key='tmp-sample',
        display_name='Temp Sample',
        uri='some-path/some-file.tif',
    )
    tmp_plugin.manifest.contributions.sample_data = [my_sample]

    viewer = make_napari_viewer()
    sample_action = viewer.window.file_menu.open_sample_menu.actions()[0]
    with mock.patch(
        'napari._qt.menus.file_menu.handle_gui_reading'
    ) as mock_read:
        sample_action.trigger()

    # assert that handle gui reading was called
    mock_read.assert_called_once()
Ejemplo n.º 5
0
def npy_reader(tmp_plugin: DynamicPlugin):
    tmp2 = tmp_plugin.spawn(name='npy_reader', register=True)

    @tmp2.contribute.reader(filename_patterns=['*.npy'])
    def _(path):
        ...

    return tmp2
Ejemplo n.º 6
0
def test_get_potential_readers_finds_readers(tmp_plugin: DynamicPlugin):
    tmp2 = tmp_plugin.spawn(register=True)

    @tmp_plugin.contribute.reader(filename_patterns=['*.tif'])
    def read_tif(path):
        ...

    @tmp2.contribute.reader(filename_patterns=['*.*'])
    def read_all(path):
        ...

    readers = get_potential_readers('my_file.tif')
    assert len(readers) == 2
Ejemplo n.º 7
0
def test_get_all_readers(tmp_plugin: DynamicPlugin):
    tmp2 = tmp_plugin.spawn(register=True)

    @tmp_plugin.contribute.reader(filename_patterns=['*.fake'])
    def read_tif(path):
        ...

    @tmp2.contribute.reader(filename_patterns=['.fake2'])
    def read_all(path):
        ...

    npe2_readers, npe1_readers = get_all_readers()
    assert len(npe2_readers) == 2
    assert len(npe1_readers) == 0
Ejemplo n.º 8
0
def test_open_or_get_error_multiple_readers(tmp_plugin: DynamicPlugin):
    """Assert error is returned when multiple plugins are available to read."""
    viewer = ViewerModel()
    tmp2 = tmp_plugin.spawn(register=True)

    @tmp_plugin.contribute.reader(filename_patterns=['*.fake'])
    def _(path):
        ...

    @tmp2.contribute.reader(filename_patterns=['*.fake'])
    def _(path):
        ...

    with pytest.raises(MultipleReaderError,
                       match='Multiple plugins found capable'):
        viewer._open_or_raise_error(['my_file.fake'])
Ejemplo n.º 9
0
def test_prepare_dialog_options_removes_plugin(tmp_plugin: DynamicPlugin):
    tmp2 = tmp_plugin.spawn(register=True)

    @tmp_plugin.contribute.reader(filename_patterns=['*.fake'])
    def _(path):
        ...

    @tmp2.contribute.reader(filename_patterns=['*.fake'])
    def _(path):
        ...

    readers = prepare_remaining_readers(
        ['my-file.fake'],
        tmp_plugin.name,
        RuntimeError('Reader failed'),
    )
    assert tmp2.name in readers
    assert tmp_plugin.name not in readers
Ejemplo n.º 10
0
def test_open_or_get_error_no_prefered_plugin_many_available(
    tmp_plugin: DynamicPlugin, ):
    """Test MultipleReaderError raised if preferred plugin missing."""
    viewer = ViewerModel()
    tmp2 = tmp_plugin.spawn(register=True)

    @tmp_plugin.contribute.reader(filename_patterns=['*.fake'])
    def _(path):
        ...

    @tmp2.contribute.reader(filename_patterns=['*.fake'])
    def _(path):
        ...

    get_settings().plugins.extension2reader = {'*.fake': 'not-a-plugin'}

    with pytest.warns(RuntimeWarning, match="Can't find not-a-plugin plugin"):
        with pytest.raises(MultipleReaderError,
                           match='Multiple plugins found capable'):
            viewer._open_or_raise_error(['my_file.fake'])
Ejemplo n.º 11
0
def builtins(_mock_npe2_pm: PluginManager):
    plugin = DynamicPlugin('napari', plugin_manager=_mock_npe2_pm)
    mf = PluginManifest.from_file(Path(__file__).parent / 'builtins.yaml')
    plugin.manifest = mf
    with plugin:
        yield plugin
Ejemplo n.º 12
0
@pytest.fixture
def builtins(_mock_npe2_pm: PluginManager):
    plugin = DynamicPlugin('napari', plugin_manager=_mock_npe2_pm)
    mf = PluginManifest.from_file(Path(__file__).parent / 'builtins.yaml')
    plugin.manifest = mf
    with plugin:
        yield plugin


@pytest.fixture
def tmp_plugin(_mock_npe2_pm: PluginManager):
    # guarantee that the name is unique, even if tmp_plugin has already been used
    count = itertools.count(0)
    while (name := f'tmp_plugin{next(count)}') in _mock_npe2_pm._manifests:
        continue
    with DynamicPlugin(name, plugin_manager=_mock_npe2_pm) as plugin:
        yield plugin


def _event_check(instance):
    def _prepare_check(name, no_event_):
        def check(instance, no_event=no_event_):
            if name in no_event:
                assert not hasattr(instance.events,
                                   name), f"event {name} defined"
            else:
                assert hasattr(instance.events,
                               name), f"event {name} not defined"

        return check