Skip to content

Commit 001cd71

Browse files
committed
ENH: Add LibraryBaseInterface
1 parent dc09e00 commit 001cd71

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

nipype/interfaces/base/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"""
1111
from .core import (Interface, BaseInterface, SimpleInterface, CommandLine,
1212
StdOutCommandLine, MpiCommandLine, SEMLikeCommandLine,
13-
PackageInfo)
13+
LibraryBaseInterface, PackageInfo)
1414

1515
from .specs import (BaseTraitedSpec, TraitedSpec, DynamicTraitedSpec,
1616
BaseInterfaceInputSpec, CommandLineInputSpec,

nipype/interfaces/base/core.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,35 @@ def _format_arg(self, name, spec, value):
12671267
return super(SEMLikeCommandLine, self)._format_arg(name, spec, value)
12681268

12691269

1270+
class LibraryBaseInterface(BaseInterface):
1271+
_pkg = None
1272+
imports = ()
1273+
1274+
def __init__(self, check_import=True, *args, **kwargs):
1275+
super(LibraryBaseInterface, self).__init__(*args, **kwargs)
1276+
if check_import:
1277+
import importlib
1278+
failed_imports = []
1279+
for pkg in (self._pkg,) + tuple(self.imports):
1280+
try:
1281+
importlib.import_module(pkg)
1282+
except ImportError:
1283+
failed_imports.append(pkg)
1284+
if failed_imports:
1285+
iflogger.warn('Unable to import %s; %s interface may fail to '
1286+
'run', failed_imports, self.__class__.__name__)
1287+
1288+
@property
1289+
def version(self):
1290+
if self._version is None:
1291+
import importlib
1292+
try:
1293+
self._version = importlib.import_module(self._pkg).__version__
1294+
except (ImportError, AttributeError):
1295+
pass
1296+
return super(LibraryBaseInterface, self).version
1297+
1298+
12701299
class PackageInfo(object):
12711300
_version = None
12721301
version_cmd = None

nipype/interfaces/dipy/base.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os.path as op
77
import numpy as np
88
from ... import logging
9-
from ..base import (traits, File, isdefined, BaseInterface,
9+
from ..base import (traits, File, isdefined, LibraryBaseInterface,
1010
BaseInterfaceInputSpec)
1111

1212
IFLOGGER = logging.getLogger('interface')
@@ -32,16 +32,11 @@ def dipy_version():
3232
return dipy.__version__
3333

3434

35-
class DipyBaseInterface(BaseInterface):
35+
class DipyBaseInterface(LibraryBaseInterface):
3636
"""
3737
A base interface for py:mod:`dipy` computations
3838
"""
39-
40-
def __init__(self, **inputs):
41-
if no_dipy():
42-
IFLOGGER.warn('dipy was not found')
43-
# raise ImportError('dipy was not found')
44-
super(DipyBaseInterface, self).__init__(**inputs)
39+
_pkg = 'dipy'
4540

4641

4742
class DipyBaseInterfaceInputSpec(BaseInterfaceInputSpec):

0 commit comments

Comments
 (0)