Пример #1
0
def test_kwargs_passed(monkeypatch):
    viewer_mock = MagicMock(napari.Viewer)
    monkeypatch.setattr(napari.view_layers, 'Viewer', viewer_mock)
    napari.view_path(
        path='some/path',
        title='my viewer',
        ndisplay=3,
        name='img name',
        scale=(1, 2, 3),
    )
    assert viewer_mock.mock_calls == [
        call(title='my viewer', ndisplay=3),
        call().open(path='some/path', name='img name', scale=(1, 2, 3)),
    ]
Пример #2
0
def test_kwargs_passed(monkeypatch):
    viewer_mock = MagicMock(napari.Viewer)
    monkeypatch.setattr(napari, 'Viewer', viewer_mock)
    napari.view_path(
        path='some/path', title='my viewer', name='img name', scale=(1, 2, 3)
    )
    assert viewer_mock.mock_calls == [
        call(
            title='my viewer', ndisplay=2, order=(), axis_labels=(), show=True,
        ),
        call().open(
            path='some/path',
            stack=False,
            plugin=None,
            layer_type=None,
            name='img name',
            scale=(1, 2, 3),
        ),
    ]
Пример #3
0
def _run():
    """Main program."""
    args, kwargs = parse_sys_argv()

    # parse -v flags and set the appropriate logging level
    levels = [logging.WARNING, logging.INFO, logging.DEBUG]
    level = levels[min(2, args.verbose)]  # prevent index error
    logging.basicConfig(
        level=level,
        format="%(asctime)s %(levelname)s %(message)s",
        datefmt='%H:%M:%S',
    )

    if args.reset:
        SETTINGS.reset()
        sys.exit("Resetting settings to default values.\n")

    if args.plugin:
        # make sure plugin is only used when files are specified
        if not args.paths:
            sys.exit(
                "error: The '--plugin' argument is only valid "
                "when providing a file name"
            )
        # I *think* that Qt is looking in sys.argv for a flag `--plugins`,
        # which emits "WARNING: No such plugin for spec 'builtins'"
        # so remove --plugin from sys.argv to prevent that warningz
        sys.argv.remove('--plugin')

    if any(p.endswith('.py') for p in args.paths):
        # we're running a script
        if len(args.paths) > 1:
            sys.exit(
                'When providing a python script, only a '
                'single positional argument may be provided'
            )

        # run the file
        mod = runpy.run_path(args.paths[0])

        from napari_plugin_engine.markers import HookImplementationMarker

        # if this file had any hook implementations, register and run as plugin
        if any(isinstance(i, HookImplementationMarker) for i in mod.values()):
            _run_plugin_module(mod, os.path.basename(args.paths[0]))

    else:
        if args.with_:
            from .plugins import plugin_manager

            # if a plugin widget has been requested, this will fail immediately
            # if the requested plugin/widget is not available.
            plugin_manager.discover_widgets()
            pname, *wnames = args.with_
            if wnames:
                for wname in wnames:
                    plugin_manager.get_widget(pname, wname)
            else:
                plugin_manager.get_widget(pname)

        from napari._qt.widgets.qt_splash_screen import NapariSplashScreen

        splash = NapariSplashScreen()
        splash.close()  # will close once event loop starts

        # viewer is unused but _must_  be kept around.
        # it will be referenced by the global window only
        # once napari has finished starting
        # but in the meantime if the garbage collector runs;
        # it will collect it and hang napari at start time.
        # in a way that is machine, os, time (and likely weather dependant).
        viewer = view_path(  # noqa: F841
            args.paths,
            stack=args.stack,
            plugin=args.plugin,
            layer_type=args.layer_type,
            **kwargs,
        )

        if args.with_:
            pname, *wnames = args.with_
            if wnames:
                for wname in wnames:
                    viewer.window.add_plugin_dock_widget(pname, wname)
            else:
                viewer.window.add_plugin_dock_widget(pname)

        run(gui_exceptions=True)