Commit 8d7c7ec0 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

BF: need to search multiple config directories - new function, findConfigDirs,

which return all candidate directories
parent da176971
......@@ -12,13 +12,14 @@ conventions.
from .util import findConfigDir # noqa
from .custom import (loader, # noqa
from .util import (findConfigDir, # noqa
from .custom import (loader, # noqa
from .datatable import (DataTable, # noqa
from .datatable import (DataTable, # noqa
......@@ -182,7 +182,16 @@ def generateColumnName(variable, visit, instance):
def findConfigDir(dirname='configs'):
"""Returns the named FUNPACK "configuration" directory.
"""Returns the first entry from ``findConfigDirs``. If
``$FUNPACK_CONFIG_DIR`` is set, it will be returned. Otherwise, it will
be the location of the `funpack/configs/` directory as described in
return findConfigDirs(dirname)
def findConfigDirs(dirname='configs'):
"""Returns a list of candidate FUNPACK configuration directories.
The FUNPACK FMRIB configuration installs its config/table files into
``<python>/lib/python<X.Y>/site-packages/funpack/configs/``. If
......@@ -192,9 +201,12 @@ def findConfigDir(dirname='configs'):
However, if FUNPACK is being executed from a source checkout, we have to
use ``site.getsitepackages`` to find the location of the config directory.
The ``dirname`` argument may also be set to ``plugins``, in which case the
path to the ``funpack.plugins`` module will be returned.
The ``$FUNPACK_CONFIG_DIR`` environment variable can also be used to
point to a configuration directory - this function will search for
point to a configuration directory - if set, the returned list will include
``$FUNPACK_CONFIG_DIR/`` at the beginning.
A ``RuntimeError`` is raised if the config directory cannot be found.
......@@ -209,18 +221,23 @@ def findConfigDir(dirname='configs'):
# python env (<pyenv>/lib/pythonX.Y/site-packages/funpack/configs/), or
# - within the funpack package directory, (<thisdir>/configs/)
cfgdirs = []
cfgdirs = []
candidates = []
if 'FUNPACK_CONFIG_DIR' in os.environ:
cfgdirs.extend(op.join(sitedir, 'funpack', dirname)
for sitedir in site.getsitepackages())
cfgdirs.append(op.join(op.dirname(__file__), dirname))
candidates.extend(op.join(sitedir, 'funpack', dirname)
for sitedir in site.getsitepackages())
candidates.append(op.join(op.dirname(__file__), dirname))
for candidate in candidates:
if op.isdir(candidate):
if len(cfgdirs) == 0:
raise RuntimeError('Cannot find FUNPACK configuration directory!')
for cfgdir in cfgdirs:
if op.isdir(cfgdir):
return cfgdir
raise RuntimeError('Cannot find FUNPACK configuration directory!')
return cfgdirs
def findTableFile(filename):
......@@ -260,7 +277,7 @@ def findConfigFile(filename, suffix='.cfg', dirname='configs'):
if op.isfile(filename):
return op.abspath(filename)
cfgdir = findConfigDir(dirname)
cfgdirs = findConfigDirs(dirname)
# Built-in config files can be specified
# with (in order of precedence):
......@@ -273,7 +290,7 @@ def findConfigFile(filename, suffix='.cfg', dirname='configs'):
f'{filename.replace(".", op.sep)}{suffix}']
for cand in candidates:
for cfgdir, cand in it.product(cfgdirs, candidates):
cand = op.abspath(op.join(cfgdir, cand))
if op.isfile(cand):
return cand
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment