fsleyes.cliserver

This module implements a simple client-server architecture which can be used to call an existing FSLeyes instance from the command line.

When FSLeyes is started with the --cliserver option, the runserver() function is called to start a server thread. On subsequent invocations of FSLeyes (with the --cliserver option), instead of starting up a new FSLeyes instance, the command-line arguments are passed to the original instance via the send() function.

When the runserver() function is called, it uses the fsl.utils.settings module to save the TCP port number to a file called cliserver.txt(). This file is used by the send() function to determine the port to connect to, and by the isRunning() function to determine whether or not a server is running.

class fsleyes.cliserver.CLIServerAction(*args, **kwargs)[source]

Bases: argparse.Action

Custom argparse.Action for applying the --cliserver command-line option.

If a server is not running, the namespace.cliserver attribute is set to True. Otherwise, the remaining arguments are passed to the send() function, and the process is closed via sys.exit.

In the former case (a server is not already running), the fsleyes.main module will start a server via runserver() at a later point in time.

__init__(*args, **kwargs)[source]

Create a CLIServerAction.

Parameters

allArgs – Sequence of arguments that should be passed to the send() function, if this action is invoked as a client. If None, it is set to sys.argv[1:].

All other arguments are passed to argparse.Action.__init__.

__call__(parser, namespace, values, option_string)[source]

Call self as a function.

__module__ = 'fsleyes.cliserver'
exception fsleyes.cliserver.AlreadyRunningError[source]

Bases: Exception

Raised by runserver() if a server loop is already running.

__module__ = 'fsleyes.cliserver'
__weakref__

list of weak references to the object (if defined)

exception fsleyes.cliserver.NotRunningError[source]

Bases: Exception

Raised by send() if a server loop is not running.

__module__ = 'fsleyes.cliserver'
__weakref__

list of weak references to the object (if defined)

fsleyes.cliserver.runserver(overlayList, displayCtx, ev=None)[source]

Starts a thread which runs the _serverloop() function.

If a server is already running, within this or any other FSLeyes instance, an AlreadyRunningError is raised.

Every line that is received is assumed to contain command line arguments specifying overlays to be loaded; these are passed to the applyCommandLineArgs() function.

Parameters
  • overlayList – The OverlayList

  • displayCtx – The master DisplayContext

  • ev – Optional threading.Event which can be used to terminate the server thread.

fsleyes.cliserver.isRunning()[source]

Returns True if (it looks like) a server is running, False otherwise.

fsleyes.cliserver._serverloop(callback, ev=None)[source]

Starts a TCP server which runs forever.

The server port number is written to the FSLeyes settings directoy in a file called cliserver.txt (see fsl.utils.settings). Then, every line of text received on the socket is passed to the callback function.

Parameters
  • callback – Callback function to which every line that is received is passed.

  • ev – Optional threading.Event which can be used to signal the server thread to stop.

fsleyes.cliserver.send(line)[source]

If a cli server is running (see runserver() and _serverloop()), the given args are sent to it.

A NotRunningError is raised if a server loop is not running.