Skip to content

Commit

Permalink
Avoid try/except on import (equinor#1257)
Browse files Browse the repository at this point in the history
  • Loading branch information
anders-kiaer authored Jan 26, 2024
1 parent bc4ab89 commit 1f6347d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 54 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

TESTS_REQUIRE = [
"bandit",
"black>=22.1",
"black>=22.1,<24",
"dash[testing]",
"flaky",
"isort",
Expand Down
11 changes: 5 additions & 6 deletions webviz_subsurface/_datainput/eclipse_init_io/pvt_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,26 @@
########################################

import abc
import sys
import warnings
from enum import Enum
from typing import Any, Callable, List, Optional, Tuple, Union

import numpy as np
from scipy import interpolate

from ..eclipse_unit import ConvertUnits, EclUnitEnum, EclUnits
from ..units import Unit

# opm is only available for Linux,
# hence, ignore any import exception here to make
# it still possible to use the PvtPlugin on
# machines with other OSes.
#
# NOTE: Functions in this file cannot be used
# on non-Linux OSes.
try:
if sys.platform == "linux":
from opm.io.ecl import EclFile
except ImportError:
pass

from ..eclipse_unit import ConvertUnits, EclUnitEnum, EclUnits
from ..units import Unit


class EclPropertyTableRawData: # pylint: disable=too-few-public-methods
Expand Down
23 changes: 11 additions & 12 deletions webviz_subsurface/_datainput/eclipse_init_io/pvt_gas.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,11 @@
#
########################################

import sys
from typing import Callable, Optional, Tuple, Union

import numpy as np

# opm is only available for Linux,
# hence, ignore any import exception here to make
# it still possible to use the PvtPlugin on
# machines with other OSes.
#
# NOTE: Functions in this file cannot be used
# on non-Linux OSes.
try:
from opm.io.ecl import EclFile
except ImportError:
pass

from ..eclipse_unit import ConvertUnits, CreateUnitConverter, EclUnitEnum, EclUnits
from .pvt_common import (
EclPhaseIndex,
Expand All @@ -64,6 +53,16 @@
surface_mass_density,
)

# opm is only available for Linux,
# hence, ignore any import exception here to make
# it still possible to use the PvtPlugin on
# machines with other OSes.
#
# NOTE: Functions in this file cannot be used
# on non-Linux OSes.
if sys.platform == "linux":
from opm.io.ecl import EclFile


class WetGas(PvxOBase):
"""Class holding a PVT interpolant and access methods for PVT data for wet gas
Expand Down
23 changes: 11 additions & 12 deletions webviz_subsurface/_datainput/eclipse_init_io/pvt_oil.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,11 @@
#
########################################

import sys
from typing import Any, Callable, Optional, Tuple, Union

import numpy as np

# opm is only available for Linux,
# hence, ignore any import exception here to make
# it still possible to use the PvtPlugin on
# machines with other OSes.
#
# NOTE: Functions in this file cannot be used
# on non-Linux OSes.
try:
from opm.io.ecl import EclFile
except ImportError:
pass

from ..eclipse_unit import ConvertUnits, CreateUnitConverter, EclUnitEnum, EclUnits
from .pvt_common import (
EclPhaseIndex,
Expand All @@ -65,6 +54,16 @@
surface_mass_density,
)

# opm is only available for Linux,
# hence, ignore any import exception here to make
# it still possible to use the PvtPlugin on
# machines with other OSes.
#
# NOTE: Functions in this file cannot be used
# on non-Linux OSes.
if sys.platform == "linux":
from opm.io.ecl import EclFile


class LiveOil(PvxOBase):
"""Class holding a PVT interpolant and access methods for PVT data for live oil
Expand Down
23 changes: 11 additions & 12 deletions webviz_subsurface/_datainput/eclipse_init_io/pvt_water.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,11 @@
#
########################################

import sys
from typing import Any, Callable, List, Optional, Union

import numpy as np

# opm is only available for Linux,
# hence, ignore any import exception here to make
# it still possible to use the PvtPlugin on
# machines with other OSes.
#
# NOTE: Functions in this file cannot be used
# on non-Linux OSes.
try:
from opm.io.ecl import EclFile
except ImportError:
pass

from ..eclipse_unit import ConvertUnits, CreateUnitConverter, EclUnitEnum, EclUnits
from .pvt_common import (
EclPhaseIndex,
Expand All @@ -62,6 +51,16 @@
surface_mass_density,
)

# opm is only available for Linux,
# hence, ignore any import exception here to make
# it still possible to use the PvtPlugin on
# machines with other OSes.
#
# NOTE: Functions in this file cannot be used
# on non-Linux OSes.
if sys.platform == "linux":
from opm.io.ecl import EclFile


class WaterImpl(PvxOBase):
"""Class holding a PVT interpolant and access methods for PVT data for water"""
Expand Down
19 changes: 8 additions & 11 deletions webviz_subsurface/_datainput/pvt_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

import numpy as np
import pandas as pd
from webviz_config.common_cache import CACHE
from webviz_config.webviz_store import webvizstore

from .eclipse_init_io.pvt_gas import Gas
from .eclipse_init_io.pvt_oil import Oil
from .eclipse_init_io.pvt_water import Water
from .fmu_input import load_csv, load_ensemble_set

# opm and res2df are only available for Linux,
# hence, ignore any import exception here to make
Expand All @@ -18,19 +25,9 @@
#
# NOTE: Functions in this file cannot be used
# on non-Linux OSes.
try:
if sys.platform == "linux":
import res2df
from opm.io.ecl import EclFile
except ImportError:
pass

from webviz_config.common_cache import CACHE
from webviz_config.webviz_store import webvizstore

from .eclipse_init_io.pvt_gas import Gas
from .eclipse_init_io.pvt_oil import Oil
from .eclipse_init_io.pvt_water import Water
from .fmu_input import load_csv, load_ensemble_set


@CACHE.memoize()
Expand Down

0 comments on commit 1f6347d

Please sign in to comment.