From 44ad40d54a3e21c91f77d2ad4058d22568e624cc Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauld.mccarthy@gmail.com>
Date: Tue, 8 Mar 2016 21:36:44 +0000
Subject: [PATCH] Help pages weren't working due to my own silliness.

---
 fsl/__init__.py      | 10 +++++++--
 fsl/tools/bet.py     |  1 -
 fsl/tools/feat.py    |  1 -
 fsl/tools/flirt.py   |  1 -
 fsl/utils/webpage.py | 52 +++++++++++++++++++++++++++-----------------
 5 files changed, 40 insertions(+), 25 deletions(-)

diff --git a/fsl/__init__.py b/fsl/__init__.py
index d80e672ba..a99ecb4f6 100644
--- a/fsl/__init__.py
+++ b/fsl/__init__.py
@@ -289,7 +289,7 @@ def _loadFSLTool(moduleName):
 
     # Each FSL tool module may specify several things
     toolName  = getattr(module, 'FSL_TOOLNAME',  None)
-    helpPage  = getattr(module, 'FSL_HELPPAGE',  'index')
+    helpPage  = getattr(module, 'FSL_HELPPAGE',  None)
     init      = getattr(module, 'FSL_INIT',      None)
     parseArgs = getattr(module, 'FSL_PARSEARGS', None)
     context   = getattr(module, 'FSL_CONTEXT',   None)
@@ -600,10 +600,16 @@ def _buildGUI(args, fslTool, toolCtx):
 
     actions = []
 
+    def help(ev):
+        if fslTool.helpPage is not None:
+            webpage.openPage(fslTool.helpPage)
+        else:
+            webpage.openLocalHelp(fslTool.toolName)
+
     actions.append((
         wx.ID_HELP,
         '{} Help'.format(fslTool.toolName),
-        lambda *ev: webpage.openFSLHelp(fslTool.helpPage)))
+        help))
 
     for (name, func) in fslTool.actions:
         actions.append((wx.ID_ANY, name, lambda ev, f=func: f(frame, toolCtx)))
diff --git a/fsl/tools/bet.py b/fsl/tools/bet.py
index cb0fe09ff..4b81d2963 100644
--- a/fsl/tools/bet.py
+++ b/fsl/tools/bet.py
@@ -336,7 +336,6 @@ def runBet(parent, opts):
 
 
 FSL_TOOLNAME  = 'BET'
-FSL_HELPPAGE  = 'bet'
 FSL_CONTEXT   = lambda *a: Options()
 FSL_INTERFACE = interface
 FSL_ACTIONS   = [('Run BET', runBet)]
diff --git a/fsl/tools/feat.py b/fsl/tools/feat.py
index b0a30e73e..38451096b 100644
--- a/fsl/tools/feat.py
+++ b/fsl/tools/feat.py
@@ -551,6 +551,5 @@ def interface(parent, args, featOpts):
 
 
 FSL_TOOLNAME  = 'FEAT'
-FSL_HELPPAGE  = 'feat'
 FSL_CONTEXT   = lambda *a: Options()
 FSL_INTERFACE = interface
diff --git a/fsl/tools/flirt.py b/fsl/tools/flirt.py
index b89c3bcac..3529c616c 100644
--- a/fsl/tools/flirt.py
+++ b/fsl/tools/flirt.py
@@ -195,7 +195,6 @@ def interface(parent, args, opts):
 
 
 FSL_TOOLNAME  = 'FLIRT'
-FSL_HELPPAGE  = 'flirt'
 FSL_CONTEXT   = lambda *a: Options()
 FSL_INTERFACE = interface
 FSL_ACTIONS   = [('Run FLIRT', runFlirt)]
diff --git a/fsl/utils/webpage.py b/fsl/utils/webpage.py
index a8d15ffb6..c532ec23d 100644
--- a/fsl/utils/webpage.py
+++ b/fsl/utils/webpage.py
@@ -13,38 +13,50 @@ The following functions are provided:
    :nosignatures:
 
    openPage
-   openFSLHelp
+   localHelpUrl
+   openLocalHelp
 """
 
-import os
-import webbrowser
+import            os
+import os.path as op
+import            webbrowser
 
 
 def openPage(url):
     """Opens the given URL in the system-default web browser."""
     webbrowser.open(url)
 
-    
-def openFSLHelp(toolName):
-    """Attempts to open the FSL help documentation for the given FSL tool.
-    If the ``$FSLDIR`` environment variable is not set, pops up a warning
-    message instead.
+
+def localHelpUrl(toolName):
+    """Checks the ``$FSLDIR`` to see if a local help page exists for the
+    FSL tool with the specified name.
     """
+    fsldir = os.environ.get('FSLDIR', None)
 
-    import wx
+    if fsldir is None:
+        return None
 
-    fsldir = os.environ.get('FSLDIR', None)
-    url    = 'file://{}/doc/redirects/{}.html'.format(fsldir, toolName)
+    toolName = toolName.lower()
+    localUrl = op.join(fsldir, 'doc', 'redirects', '{}.html'.format(toolName))
+
+    if op.exists(localUrl):
+        import urlparse
+        import urllib
+        return urlparse.urljoin(
+            'file:', urllib.pathname2url(localUrl)) 
 
-    if fsldir is not None:
-        openPage(url)
+    return None
+
+
+def openLocalHelp(toolName):
+    """Attempts to open the locally hosted FSL help documentation
+    for the given FSL tool. If there is no help page for the
+    given tool, attempts to open the FSL wiki.
+    """
 
-    else:
+    localUrl = localHelpUrl(toolName)
 
-        msg = 'The FSLDIR environment variable is not set - I don\'t '\
-            'know where to find the FSL documentation.'
+    if localUrl is None:
+        localUrl = "http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/"
 
-        wx.MessageDialog(
-            None,
-            message=msg,
-            style=wx.OK | wx.ICON_ERROR).ShowModal()
+    openPage(localUrl)
-- 
GitLab