diff --git a/tofu/__init__.py b/tofu/__init__.py index 29c1043d6..d4ba1573e 100644 --- a/tofu/__init__.py +++ b/tofu/__init__.py @@ -69,37 +69,39 @@ import tofu.data as data -try: - import tofu.imas2tofu as imas2tofu - okimas2tofu = True -except Exception as err: - warnings.warn(str(err)) - okimas2tofu = False - -try: - import tofu.mag as mag - okmag = True -except Exception as err: - warnings.warn(str(err)) - okmag = False - -#import tofu.dust as dust - +# ------------------------------------- +# Try importing optional subpackages +# ------------------------------------- + +msg = None +dsub = dict.fromkeys(['imas2tofu', 'mag']) +for sub in dsub.keys(): + try: + exec('import tofu.{0} as {0}'.format(sub)) + dsub[sub] = True + except Exception as err: + dsub[sub] = str(err) + +# ------------------------------------- +# If any error, populate warning and store error message +# ------------------------------------- + +lsubout = [sub for sub in dsub.keys() if dsub[sub] is not True] +if len(lsubout) > 0: + lsubout = ['tofu.{0}'.format(ss) for ss in lsubout] + msg = "\nThe following subpackages are not available:" + msg += "\n - " + "\n - ".join(lsubout) + msg += "\n => see tofu.dsub[] for details on error messages" + warnings.warn(msg) + +# ------------------------------------- +# Add optional subpackages to __all__ +# ------------------------------------- __all__ = ['pathfile','utils','_plot','geom','data'] -if okimas2tofu: - __all__.append('imas2tofu') -if okmag: - __all__.append('mag') - - -del sys, warnings, okimas2tofu, okmag - -#__all__.extend(['geom', 'mesh', 'matcomp', 'data', 'inv']) +for sub in dsub.keys(): + if dsub[sub] is True: + __all__.append(sub) -#__name__ = "" -#__date__ = "$Mar 05, 2014$" -#__copyright__ = "" -#__license__ = "" -#__url__ = "" -#__path__ = +# clean-up the mess +del sys, warnings, lsubout, sub, msg diff --git a/tofu/imas2tofu/__init__.py b/tofu/imas2tofu/__init__.py index ea5371cf7..184fc8773 100644 --- a/tofu/imas2tofu/__init__.py +++ b/tofu/imas2tofu/__init__.py @@ -19,13 +19,12 @@ msg += "\n\nIMAS python API issue\n" msg += "imas could not be imported into tofu ('import imas' failed):\n" msg += " - it may not be installed (optional dependency)\n" - msg += " - or you not have loaded the good working environment\n\n" + msg += " - or you have loaded the wrong working environment\n\n" msg += " => the optional sub-package tofu.imas2tofu is not usable\n" else: msg = str(traceback.format_exc()) msg += "\n\n => the optional sub-package tofu.imas2tofu is not usable\n" - warnings.warn(msg) - del msg, err + raise Exception(msg) __all__ = ['MultiIDSLoader', 'load_Config', 'load_Plasma2D', 'load_Cam', 'load_Data'] diff --git a/tofu/mag/__init__.py b/tofu/mag/__init__.py index 96b103f5b..ef284347e 100644 --- a/tofu/mag/__init__.py +++ b/tofu/mag/__init__.py @@ -7,13 +7,9 @@ import warnings import traceback +msg, err = None, None try: import imas - try: - from tofu.mag.magFieldLines import * - except Exception: - from .magFieldLines import * - del warnings, traceback, magFieldLines, mag_ripple except Exception as err: if str(err) == 'imas not available': msg = "" @@ -25,7 +21,24 @@ else: msg = str(traceback.format_exc()) msg += "\n\n => the optional sub-package tofu.mag is not usable\n" - warnings.warn(msg) - del msg, err + raise Exception(msg) + +try: + import pywed +except Exception: + msg = "pywed not available => no tofu.mag" + raise Exception(msg) + +try: + from tofu.mag.magFieldLines import * +except Exception: + try: + from .magFieldLines import * + except Exception: + msg = "Could not import" + raise Exception(msg) + +del warnings, traceback, magFieldLines, mag_ripple, pywed, imas +del msg, err __all__ = ['MagFieldLines']