From 1f0f36db622f88cac94b4a8cc89fa396343450b7 Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauld.mccarthy@gmail.com>
Date: Sat, 16 Jan 2016 17:36:10 +0000
Subject: [PATCH] Async.run does not create a separate thread if wx is not
 running. GLModel has to implement GLObject.ready() method.

---
 fsl/__init__.py           |  5 ++++-
 fsl/fsleyes/gl/glmodel.py |  6 ++++++
 fsl/utils/async.py        | 39 +++++++++++++++++++++++----------------
 3 files changed, 33 insertions(+), 17 deletions(-)

diff --git a/fsl/__init__.py b/fsl/__init__.py
index b97174117..38d74dc7a 100644
--- a/fsl/__init__.py
+++ b/fsl/__init__.py
@@ -165,7 +165,10 @@ def runTool(toolName, args, **kwargs):
     """
 
     args = [toolName] + args
-    
+
+    if log.getEffectiveLevel() == logging.DEBUG:
+        args = ['-vvv'] + args
+
     # If we are running from a compiled fsleyes
     # executable, we need to prepend command line
     # arguments with 'cmd' - see the wrapper script
diff --git a/fsl/fsleyes/gl/glmodel.py b/fsl/fsleyes/gl/glmodel.py
index 77ec93e5e..0dd61055d 100644
--- a/fsl/fsleyes/gl/glmodel.py
+++ b/fsl/fsleyes/gl/glmodel.py
@@ -106,6 +106,12 @@ class GLModel(globject.GLObject):
         self.opts    = None
 
         
+    def ready(self):
+        """Overrides :meth:`.GLObject.ready`. Always returns ``True``.
+        """
+        return True
+
+        
     def addListeners(self):
         """Called by :meth:`__init__`. Adds some property listeners to the
         :class:`.Display` and :class:`.ModelOpts` instances so the OpenGL
diff --git a/fsl/utils/async.py b/fsl/utils/async.py
index 65ebf1cae..6d098f30d 100644
--- a/fsl/utils/async.py
+++ b/fsl/utils/async.py
@@ -59,13 +59,18 @@ def run(task, onFinish=None, name=None):
 
     :arg name:     An optional name to use for this task in log statements.
 
-    .. note:: If a ``wx`` application is not running, the ``onFinish``
-              function is called directly from the task thread.
+    :returns: A reference to the ``Thread`` that was created.
+
+    .. note:: If a ``wx`` application is not running, the ``task`` and
+              ``onFinish`` functions will simply be called directly, and
+             the return value will be ``None``.
     """
 
     if name is None:
         name = 'async task'
 
+    haveWX = _haveWX()
+
     def wrapper():
 
         log.debug('Running task "{}"...'.format(name))
@@ -73,24 +78,26 @@ def run(task, onFinish=None, name=None):
 
         log.debug('Task "{}" finished'.format(name))
 
-        if onFinish is not None:
-
-            if _haveWX():
-                import wx
+        if (onFinish is not None):
 
-                log.debug('Scheduling task "{}" finish handler '
-                          'on wx.MainLoop'.format(name))
+            import wx
 
-                wx.CallAfter(onFinish)
-            else:
-                log.debug('Running task "{}" finish handler'.format(name)) 
-                onFinish()
-                
+            log.debug('Scheduling task "{}" finish handler '
+                      'on wx.MainLoop'.format(name))
 
-    thread = threading.Thread(target=wrapper)
-    thread.start()
+            wx.CallAfter(onFinish)
 
-    return thread
+    if haveWX:
+        thread = threading.Thread(target=wrapper)
+        thread.start()
+        return thread
+ 
+    else:
+        log.debug('Running task "{}" directly'.format(name)) 
+        task()
+        log.debug('Running task "{}" finish handler'.format(name)) 
+        onFinish()
+        return None
 
 
 _idleRegistered = False
-- 
GitLab