Commit 3a3cb431 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

RF: New findConfigDir function, used to find lib/site-packages/funpack/

parent 207e76cb
......@@ -12,12 +12,13 @@ conventions.
"""
from .custom import (loader, # noqa
from .util import findConfigDir # noqa
from .custom import (loader, # noqa
sniffer,
formatter,
exporter,
processor,
metaproc,
cleaner)
from .datatable import (DataTable, # noqa
from .datatable import (DataTable, # noqa
Column)
......@@ -181,6 +181,48 @@ def generateColumnName(variable, visit, instance):
return '{}-{}.{}'.format(variable, visit, instance)
def findConfigDir(dirname='configs'):
"""Returns the named FUNPACK "configuration" directory.
The FUNPACK FMRIB configuration installs its config/table files into
``<python>/lib/python<X.Y>/site-packages/funpack/configs/``. If
FUNPACK is installed into that Python environment, this directory
will be alongside the FUNPACK source code.
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 ``$FUNPACK_CONFIG_DIR`` environment variable can also be used to
point to a configuration directory - this function will search for
``$FUNPACK_CONFIG_DIR/{dirname}``.
A ``RuntimeError`` is raised if the config directory cannot be found.
"""
# The user can also refer to "built-in"
# config files just by giving a file path
# with/without suffix, relative to one of
# the following locations (in order of
# precedence):
#
# - in $FUNPACK_CONFIG_DIR, or
# - within the funpack package directory, (<thisdir>/configs/) or,
# - if we are running from a git checkout, installed in the running
# python env (<pyenv>/lib/pythonX.Y/site-packages/funpack/configs/)
cfgdirs = []
if 'FUNPACK_CONFIG_DIR' in os.environ:
cfgdirs.append(os.environ['FUNPACK_CONFIG_DIR'])
cfgdirs.append(op.join(op.dirname(__file__), dirname))
cfgdirs.extend(op.join(sitedir, 'funpack', dirname)
for sitedir in site.getsitepackages())
for cfgdir in cfgdirs:
if op.isdir(cfgdir):
return cfgdir
raise RuntimeError('Cannot find FUNPACK configuration directory!')
def findTableFile(filename):
"""Searches for a FUNPACK table tile - see :func:`findConfigFile`. """
return findConfigFile(filename, '.tsv')
......@@ -218,24 +260,7 @@ def findConfigFile(filename, suffix='.cfg', dirname='configs'):
if op.isfile(filename):
return op.abspath(filename)
# The user can also refer to "built-in"
# config files just by giving a file path
# with/without suffix, relative to one of
# the following locations (in order of
# precedence):
#
# - in $FUNPACK_CONFIG_DIR, or
# - within the funpack package directory, (<thisdir>/configs/) or,
# - if we are running from a git checkout, installed in the running
# python env (<pyenv>/lib/pythonX.Y/site-packages/funpack/configs/)
cfgdirs = []
if 'FUNPACK_CONFIG_DIR' in os.environ:
cfgdirs.append(os.environ['FUNPACK_CONFIG_DIR'])
cfgdirs.append(op.join(op.dirname(__file__), dirname))
cfgdirs.extend(op.join(sitedir, 'funpack', dirname)
for sitedir in site.getsitepackages())
cfgdir = findConfigDir(dirname)
# Built-in config files can be specified
# with (in order of precedence):
......@@ -248,7 +273,7 @@ def findConfigFile(filename, suffix='.cfg', dirname='configs'):
f'{filename}{suffix}',
f'{filename.replace(".", op.sep)}{suffix}']
for cfgdir, cand in it.product(cfgdirs, candidates):
for cand in 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