From 295809ede65ab723d34951e2f00524f811017420 Mon Sep 17 00:00:00 2001 From: David Bold Date: Mon, 26 Sep 2022 16:01:37 +0200 Subject: [PATCH 1/2] Add hook for submodules for extensions --- .gitignore | 3 +++ xbout/load.py | 9 +++++++++ xbout/modules/__init__.py | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 xbout/modules/__init__.py diff --git a/.gitignore b/.gitignore index 907b66d0..2c1cd8bb 100644 --- a/.gitignore +++ b/.gitignore @@ -108,3 +108,6 @@ venv.bak/ # Generated files: _version.py + +# Submodules +xbout/modules/*/ diff --git a/xbout/load.py b/xbout/load.py index 39c77d6f..bb4b8a3d 100644 --- a/xbout/load.py +++ b/xbout/load.py @@ -12,6 +12,7 @@ from . import geometries from .utils import _set_attrs_on_all_vars, _separate_metadata, _check_filetype, _is_path +from .modules import avail as modules _BOUT_PER_PROC_VARIABLES = [ @@ -240,6 +241,14 @@ def attrs_remove_section(obj, section): else: ds = geometries.apply_geometry(ds, None) + matches = [module for module in modules if module.does_match(ds)] + if len(matches): + assert ( + len(matches) == 1 + ), f"More than module claim to be able to read the dataset: {[x.__file__ for x in matches]}" + (match,) = matches + ds = match.update(ds) + if info == "terse": print("Read in dataset from {}".format(str(Path(datapath)))) elif info: diff --git a/xbout/modules/__init__.py b/xbout/modules/__init__.py new file mode 100644 index 00000000..77dcbed6 --- /dev/null +++ b/xbout/modules/__init__.py @@ -0,0 +1,18 @@ +import pkgutil +import importlib + +avail = [] + + +class skeleton: + name = "not set" + + def does_match(self, ds): + return False + + def update(self, ds): + return ds + + +for module in pkgutil.iter_modules(__path__): + importlib.import_module(f"xbout.modules.{module.name}") From cf03aa47e893c93e8c21e8932177288d65701eba Mon Sep 17 00:00:00 2001 From: David Bold Date: Fri, 30 Sep 2022 12:46:45 +0200 Subject: [PATCH 2/2] Update data set in both code paths --- xbout/load.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/xbout/load.py b/xbout/load.py index bb4b8a3d..87f6ec75 100644 --- a/xbout/load.py +++ b/xbout/load.py @@ -341,6 +341,14 @@ def attrs_remove_section(obj, section): # BOUT++ ds.bout.fine_interpolation_factor = 8 + matches = [module for module in modules if module.does_match(ds)] + if len(matches): + assert ( + len(matches) == 1 + ), f"More than module claim to be able to read the dataset: {[x.__file__ for x in matches]}" + (match,) = matches + ds = match.update(ds) + if info == "terse": print("Read in dataset from {}".format(str(Path(datapath)))) elif info: