From a650bc98be8198beb9bc8eac9bcd3352dfa13c68 Mon Sep 17 00:00:00 2001 From: VEZINET Didier Date: Tue, 5 Nov 2019 13:56:22 +0100 Subject: [PATCH 1/4] [Issue243] Less verbose warnings --- tofu/__init__.py | 8 ++++++-- tofu/imas2tofu/__init__.py | 2 +- tofu/version.py | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tofu/__init__.py b/tofu/__init__.py index 8ffba0a2d..1183cbb87 100644 --- a/tofu/__init__.py +++ b/tofu/__init__.py @@ -77,14 +77,18 @@ import tofu.imas2tofu as imas2tofu okimas2tofu = True except Exception as err: - warnings.warn(str(err)) + msg = "\n sub-package tofu.imas2tofu unavailable" + warnings.warn(msg) + # warnings.warn(str(err)) okimas2tofu = False try: import tofu.mag as mag okmag = True except Exception as err: - warnings.warn(str(err)) + msg = "\n sub-package tofu.mag unavailable" + warnings.warn(msg) + # warnings.warn(str(err)) okmag = False #import tofu.dust as dust diff --git a/tofu/imas2tofu/__init__.py b/tofu/imas2tofu/__init__.py index ea5371cf7..d93d07514 100644 --- a/tofu/imas2tofu/__init__.py +++ b/tofu/imas2tofu/__init__.py @@ -19,7 +19,7 @@ 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()) diff --git a/tofu/version.py b/tofu/version.py index 9b4e6a6dc..9d010635f 100644 --- a/tofu/version.py +++ b/tofu/version.py @@ -1,2 +1,2 @@ # Do not edit, pipeline versioning governed by git tags! -__version__ = '1.4.1-177-g5b98902' +__version__ = '1.4.1-188-g044d87f' From f0e042829ff290ebefdd3caf85b0bcc4c0c9950c Mon Sep 17 00:00:00 2001 From: VEZINET Didier Date: Tue, 5 Nov 2019 14:24:34 +0100 Subject: [PATCH 2/4] [Issue243] Better formatting of warning and subpackages' __init__ raise Exceptions --- tofu/__init__.py | 16 ++++++++-------- tofu/imas2tofu/__init__.py | 3 +-- tofu/mag/__init__.py | 26 +++++++++++++++++++------- tofu/version.py | 2 +- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/tofu/__init__.py b/tofu/__init__.py index 1183cbb87..f1322f9d3 100644 --- a/tofu/__init__.py +++ b/tofu/__init__.py @@ -72,26 +72,26 @@ import tofu.geom as geom import tofu.data as data - +lsub = [] try: import tofu.imas2tofu as imas2tofu okimas2tofu = True except Exception as err: - msg = "\n sub-package tofu.imas2tofu unavailable" - warnings.warn(msg) - # warnings.warn(str(err)) + lsub.append('imas2tofu') okimas2tofu = False try: import tofu.mag as mag okmag = True except Exception as err: - msg = "\n sub-package tofu.mag unavailable" - warnings.warn(msg) - # warnings.warn(str(err)) + lsub.append('mag') okmag = False -#import tofu.dust as dust +if len(lsub) > 0: + lsub = ['tofu.{0}'.format(ss) for ss in lsub] + msg = "\nThe following subpackages are not available:" + msg += "\n - " + "\n - ".join(lsub) + warnings.warn(msg) __all__ = ['pathfile','utils','_plot','geom','data'] diff --git a/tofu/imas2tofu/__init__.py b/tofu/imas2tofu/__init__.py index d93d07514..184fc8773 100644 --- a/tofu/imas2tofu/__init__.py +++ b/tofu/imas2tofu/__init__.py @@ -24,8 +24,7 @@ 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..7a7bc7341 100644 --- a/tofu/mag/__init__.py +++ b/tofu/mag/__init__.py @@ -9,11 +9,6 @@ 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 +20,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 +del msg, err __all__ = ['MagFieldLines'] diff --git a/tofu/version.py b/tofu/version.py index 9d010635f..eb58bb7be 100644 --- a/tofu/version.py +++ b/tofu/version.py @@ -1,2 +1,2 @@ # Do not edit, pipeline versioning governed by git tags! -__version__ = '1.4.1-188-g044d87f' +__version__ = '1.4.1-189-ga650bc9' From d014493a70a80585d21b0b3c4ab8dc5b4721efd0 Mon Sep 17 00:00:00 2001 From: VEZINET Didier Date: Tue, 5 Nov 2019 14:38:29 +0100 Subject: [PATCH 3/4] [Issue243] Storing error messages in tofu.dsub --- tofu/__init__.py | 64 +++++++++++++++++++++----------------------- tofu/mag/__init__.py | 3 ++- tofu/version.py | 2 +- 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/tofu/__init__.py b/tofu/__init__.py index f1322f9d3..6e4488279 100644 --- a/tofu/__init__.py +++ b/tofu/__init__.py @@ -72,42 +72,40 @@ import tofu.geom as geom import tofu.data as data -lsub = [] -try: - import tofu.imas2tofu as imas2tofu - okimas2tofu = True -except Exception as err: - lsub.append('imas2tofu') - okimas2tofu = False - -try: - import tofu.mag as mag - okmag = True -except Exception as err: - lsub.append('mag') - okmag = False - -if len(lsub) > 0: - lsub = ['tofu.{0}'.format(ss) for ss in lsub] + +# ------------------------------------- +# 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] != 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(lsub) + 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] == 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/mag/__init__.py b/tofu/mag/__init__.py index 7a7bc7341..ef284347e 100644 --- a/tofu/mag/__init__.py +++ b/tofu/mag/__init__.py @@ -7,6 +7,7 @@ import warnings import traceback +msg, err = None, None try: import imas except Exception as err: @@ -37,7 +38,7 @@ msg = "Could not import" raise Exception(msg) -del warnings, traceback, magFieldLines, mag_ripple +del warnings, traceback, magFieldLines, mag_ripple, pywed, imas del msg, err __all__ = ['MagFieldLines'] diff --git a/tofu/version.py b/tofu/version.py index eb58bb7be..dce5d5166 100644 --- a/tofu/version.py +++ b/tofu/version.py @@ -1,2 +1,2 @@ # Do not edit, pipeline versioning governed by git tags! -__version__ = '1.4.1-189-ga650bc9' +__version__ = '1.4.1-190-gf0e0428' From 4752814c7a28e652739f1970d0dab78e1f9946f9 Mon Sep 17 00:00:00 2001 From: VEZINET Didier Date: Tue, 5 Nov 2019 15:10:21 +0100 Subject: [PATCH 4/4] [Issue243] PEP8 Compliance --- tofu/__init__.py | 4 ++-- tofu/version.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tofu/__init__.py b/tofu/__init__.py index 6e4488279..5a884cfe9 100644 --- a/tofu/__init__.py +++ b/tofu/__init__.py @@ -90,7 +90,7 @@ # If any error, populate warning and store error message # ------------------------------------- -lsubout = [sub for sub in dsub.keys() if dsub[sub] != True] +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:" @@ -104,7 +104,7 @@ __all__ = ['pathfile','utils','_plot','geom','data'] for sub in dsub.keys(): - if dsub[sub] == True: + if dsub[sub] is True: __all__.append(sub) # clean-up the mess diff --git a/tofu/version.py b/tofu/version.py index dce5d5166..5c202c35c 100644 --- a/tofu/version.py +++ b/tofu/version.py @@ -1,2 +1,2 @@ # Do not edit, pipeline versioning governed by git tags! -__version__ = '1.4.1-190-gf0e0428' +__version__ = '1.4.1-191-gd014493'