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:
ActionCustom
argparse.Actionfor applying the--cliservercommand-line option.If a server is not running, the
namespace.cliserverattribute is set toTrue. Otherwise, the remaining arguments are passed to thesend()function, and the process is closed viasys.exit.In the former case (a server is not already running), the
fsleyes.mainmodule will start a server viarunserver()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. IfNone, it is set tosys.argv[1:].
All other arguments are passed to
argparse.Action.__init__.
- __module__ = 'fsleyes.cliserver'
- exception fsleyes.cliserver.AlreadyRunningError[source]
Bases:
ExceptionRaised by
runserver()if a server loop is already running.- __module__ = 'fsleyes.cliserver'
- __weakref__
list of weak references to the object
- exception fsleyes.cliserver.NotRunningError[source]
Bases:
ExceptionRaised by
send()if a server loop is not running.- __module__ = 'fsleyes.cliserver'
- __weakref__
list of weak references to the object
- 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
AlreadyRunningErroris 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
OverlayListdisplayCtx – The master
DisplayContextev – Optional
threading.Eventwhich can be used to terminate the server thread.
- fsleyes.cliserver.isRunning()[source]
Returns
Trueif (it looks like) a server is running,Falseotherwise.
- 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(seefsl.utils.settings). Then, every line of text received on the socket is passed to thecallbackfunction.- Parameters:
callback – Callback function to which every line that is received is passed.
ev – Optional
threading.Eventwhich 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 givenargsare sent to it.A
NotRunningErroris raised if a server loop is not running.