Example #1
0
def parseArgs(argv):
    """Creates an argument parser which accepts options for off-screen
    rendering. Uses the :mod:`fsleyes.parseargs` module to peform the
    actual parsing.

    :returns: An ``argparse.Namespace`` object containing the parsed
              arguments.
    """

    mainParser = parseargs.ArgumentParser(
        add_help=False, formatter_class=parseargs.FSLeyesHelpFormatter)

    mainParser.add_argument('-of', '--outfile', help='Output image file name'),
    mainParser.add_argument('-c',
                            '--crop',
                            type=int,
                            metavar='BORDER',
                            help='Auto-crop image, leaving a '
                            'border on each side')
    mainParser.add_argument('-sz',
                            '--size',
                            type=int,
                            nargs=2,
                            metavar=('W', 'H'),
                            help='Size in pixels (width, height)',
                            default=(800, 600))

    name = 'render'
    prolog = 'FSLeyes render version {}\n'.format(version.__version__)
    optStr = '-of outfile'
    description = textwrap.dedent("""\
        FSLeyes screenshot generator.

        Use the '--scene' option to choose between orthographic
        ('ortho'), lightbox ('lightbox'), or 3D ('3d') views.
        """)

    namespace = parseargs.parseArgs(
        mainParser,
        argv,
        name,
        prolog=prolog,
        desc=description,
        usageProlog=optStr,
        argOpts=['-of', '--outfile', '-sz', '--size', '-c', '--crop'],
        shortHelpExtra=['--outfile', '--size', '--crop'])

    if namespace.outfile is None:
        log.error('outfile is required')
        mainParser.print_usage()
        sys.exit(1)

    namespace.outfile = op.abspath(namespace.outfile)

    if namespace.scene not in ('ortho', 'lightbox', '3d'):
        log.info('Unknown scene specified  ("{}") - defaulting '
                 'to ortho'.format(namespace.scene))
        namespace.scene = 'ortho'

    return namespace
Example #2
0
def applyCommandLineArgs(overlayList,
                         displayCtx,
                         argv,
                         panel=None,
                         applyOverlayArgs=True,
                         **kwargs):
    """Applies the command line arguments stored in ``argv`` to the
    :class:`.CanvasPanel` ``panel``. If ``panel is None``, it is assumed
    that ``argv`` only contains overlay arguments.

    :arg overlayList:      The :class:`.OverlayList`.

    :arg displayCtx:       The :class:`.DisplayContext`. If a ``panel`` is
                           provided, this should be the ``DisplayContext``
                           associated with that panel.

    :arg argv:             List of command line arguments to apply.
    :arg panel:            Optional :class:`.CanvasPanel` to apply the
                           arguments to.

    :arg applyOverlayArgs: If ``False``, overlay arguments are not applied.

    All other keyword arguments are passed to the
    :func:`.parseargs.applyOverlayArgs`  function.
    """

    # We patch sys.stdout/stderr
    # while parseargs.parseArgs is
    # called so we can capture its
    # output.
    stdout = six.StringIO()
    stderr = six.StringIO()

    if argv[0] == 'fsleyes':
        argv = argv[1:]

    parser = argparse.ArgumentParser(add_help=False)

    try:
        real_stdout = sys.stdout
        real_stderr = sys.stderr
        sys.stdout = stdout
        sys.stderr = stderr
        namespace = parseargs.parseArgs(parser, argv, 'fsleyes')

    except SystemExit as e:
        raise ApplyCLIExit(e.code, stdout.getvalue(), stderr.getvalue())

    finally:
        sys.stdout = real_stdout
        sys.stderr = real_stderr

    if applyOverlayArgs:
        parseargs.applyOverlayArgs(namespace, overlayList, displayCtx,
                                   **kwargs)

    if panel is not None:
        sceneOpts = panel.sceneOpts
        parseargs.applySceneArgs(namespace, overlayList, displayCtx, sceneOpts)
Example #3
0
def parseArgs(argv):
    """Parses the given ``fsleyes`` command line arguments. See the
    :mod:`.parseargs` module for details on the ``fsleyes`` command
    line interface.

    :arg argv: command line arguments for ``fsleyes``.
    """

    import fsleyes.parseargs as parseargs
    import fsleyes.layouts as layouts
    import fsleyes.version as version

    parser = parseargs.ArgumentParser(
        add_help=False, formatter_class=parseargs.FSLeyesHelpFormatter)

    serveraction = ft.partial(cliserver.CLIServerAction, allArgs=argv)

    parser.add_argument('-r',
                        '--runscript',
                        metavar='SCRIPTFILE',
                        help='Run custom FSLeyes script')
    parser.add_argument('-cs',
                        '--cliserver',
                        action=serveraction,
                        help='Pass all command-line arguments '
                        'to a single FSLeyes instance')

    # We include the list of available
    # layouts in the help description
    allLayouts  = list(layouts.BUILT_IN_LAYOUTS.keys()) + \
                  list(layouts.getAllLayouts())
    name = 'fsleyes'
    prolog = 'FSLeyes version {}\n'.format(version.__version__)
    description = textwrap.dedent("""\
        FSLeyes - the FSL image viewer.

        Use the '--scene' option to load a saved layout ({layouts}).

        If no '--scene' is specified, a default layout is shown or the
        previous layout is restored. If a script is provided via
        the '--runscript' argument, it is assumed that the script sets
        up the scene.
        """.format(layouts=', '.join(allLayouts)))

    # Options for configuring the scene are
    # managed by the parseargs module
    return parseargs.parseArgs(parser,
                               argv,
                               name,
                               prolog=prolog,
                               desc=description,
                               argOpts=['-r', '--runscript'])
Example #4
0
def applyCommandLineArgs(overlayList, displayCtx, argv, panel=None):
    """Applies the command line arguments stored in ``argv`` to the
    :class:`.CanvasPanel` ``panel``. If ``panel is None``, it is assumed
    that ``argv`` only contains overlay arguments.
    """

    # We patch sys.stdout/stderr
    # while parseargs.parseArgs is
    # called so we can capture its
    # output.
    stdout = six.StringIO()
    stderr = six.StringIO()

    if argv[0] == 'fsleyes':
        argv = argv[1:]

    parser = argparse.ArgumentParser(add_help=False)

    try:
        real_stdout = sys.stdout
        real_stderr = sys.stderr
        sys.stdout = stdout
        sys.stderr = stderr
        namespace = parseargs.parseArgs(parser, argv, 'fsleyes')

    except SystemExit as e:
        raise ApplyCLIExit(e.code, stdout.getvalue(), stderr.getvalue())

    finally:
        sys.stdout = real_stdout
        sys.stderr = real_stderr

    parseargs.applyOverlayArgs(namespace, overlayList, displayCtx)

    # No panel, no need to do anything else
    if panel is None:
        return

    sceneOpts = panel.sceneOpts
    parseargs.applySceneArgs(namespace, overlayList, displayCtx, sceneOpts)
Example #5
0
def applyCommandLineArgs(overlayList,
                         displayCtx,
                         argv,
                         panel=None,
                         applyOverlayArgs=True,
                         baseDir=None,
                         **kwargs):
    """Applies the command line arguments stored in ``argv`` to the
    :class:`.CanvasPanel` ``panel``. If ``panel is None``, it is assumed
    that ``argv`` only contains overlay arguments.

    :arg overlayList:      The :class:`.OverlayList`.

    :arg displayCtx:       The :class:`.DisplayContext`. If a ``panel`` is
                           provided, this should be the ``DisplayContext``
                           associated with that panel.

    :arg argv:             List of command line arguments to apply.

    :arg panel:            Optional :class:`.CanvasPanel` to apply the
                           arguments to.

    :arg applyOverlayArgs: If ``False``, overlay arguments are not applied.

    :arg baseDir:          Directory from which to interpret the arguments,
                           in case this is different from the current working
                           directory, and overlays have been specified with
                           relative paths.

    All other keyword arguments are passed to the
    :func:`.parseargs.applyOverlayArgs`  function.
    """

    # We patch sys.stdout/stderr
    # while parseargs.parseArgs is
    # called so we can capture its
    # output.
    stdout = six.StringIO()
    stderr = six.StringIO()

    if argv[0] == 'fsleyes':
        argv = argv[1:]

    parser = argparse.ArgumentParser(add_help=False)

    try:
        real_stdout = sys.stdout
        real_stderr = sys.stderr
        sys.stdout = stdout
        sys.stderr = stderr

        with chdir(baseDir):
            namespace = parseargs.parseArgs(parser, argv, 'fsleyes')

    except SystemExit as e:
        raise ApplyCLIExit(e.code, stdout.getvalue(), stderr.getvalue())

    finally:
        sys.stdout = real_stdout
        sys.stderr = real_stderr

    if baseDir is not None:
        for o in namespace.overlays:
            if not op.isabs(o.overlay):
                o.overlay = op.join(baseDir, o.overlay)

    if applyOverlayArgs:
        parseargs.applyOverlayArgs(namespace, overlayList, displayCtx,
                                   **kwargs)

    if panel is not None:
        sceneOpts = panel.sceneOpts
        parseargs.applySceneArgs(namespace, overlayList, displayCtx, sceneOpts)