Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of config for third party packages #169

Merged
merged 1 commit into from
Apr 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 42 additions & 29 deletions src/wrf/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,54 @@
_local_config = local()


def _init_local():
def _try_enable_xarray():
global _local_config

_local_config.xarray_enabled = True
_local_config.cartopy_enabled = True
_local_config.basemap_enabled = True
_local_config.pyngl_enabled = True
_local_config.cache_size = 20
_local_config.initialized = True

try:
from xarray import DataArray
except ImportError:
_local_config.xarray_enabled = False


def _try_enable_cartopy():
global _local_config
_local_config.cartopy_enabled = True
try:
from cartopy import crs
except ImportError:
_local_config.cartopy_enabled = False


def _try_enable_basemap():
global _local_config
_local_config.basemap_enabled = True
try:
from mpl_toolkits.basemap import Basemap
except ImportError:
_local_config.basemap_enabled = False


def _try_enable_pyngl():
global _local_config
_local_config.pyngl_enabled = True
try:
from Ngl import Resources
except ImportError:
_local_config.pyngl_enabled = False


def _init_local():
global _local_config

_try_enable_xarray()
_try_enable_cartopy()
_try_enable_basemap()
_try_enable_pyngl()

_local_config.cache_size = 20
_local_config.initialized = True


# Initialize the main thread's configuration
_init_local()

Expand All @@ -51,11 +68,11 @@ def init_local():
def func_wrapper(wrapped, instance, args, kwargs):
global _local_config
try:
init = _local_config.init
initialized = _local_config.initialized
except AttributeError:
_init_local()
else:
if not init:
if not initialized:
_init_local()

return wrapped(*args, **kwargs)
Expand All @@ -77,17 +94,16 @@ def xarray_enabled():


@init_local()
def disable_xarray():
"""Disable xarray."""
global _local_config
_local_config.xarray_enabled = False
def enable_xarray():
"""Enable xarray if it is installed."""
_try_enable_xarray()


@init_local()
def enable_xarray():
"""Enable xarray."""
def disable_xarray():
"""Disable xarray."""
global _local_config
_local_config.xarray_enabled = True
_local_config.xarray_enabled = False


@init_local()
Expand All @@ -105,16 +121,15 @@ def cartopy_enabled():

@init_local()
def enable_cartopy():
"""Enable cartopy."""
global _local_config
_local_config.cartopy_enabled = True
"""Enable cartopy if it is installed."""
_try_enable_cartopy()


@init_local()
def disable_cartopy():
"""Disable cartopy."""
global _local_config
_local_config.cartopy_enabled = True
_local_config.cartopy_enabled = False


@init_local()
Expand All @@ -132,16 +147,15 @@ def basemap_enabled():

@init_local()
def enable_basemap():
"""Enable basemap."""
global _local_config
_local_config.basemap_enabled = True
"""Enable basemap if it is installed."""
_try_enable_basemap()


@init_local()
def disable_basemap():
"""Disable basemap."""
global _local_config
_local_config.basemap_enabled = True
_local_config.basemap_enabled = False


@init_local()
Expand All @@ -159,16 +173,15 @@ def pyngl_enabled():

@init_local()
def enable_pyngl():
"""Enable pyngl."""
global _local_config
_local_config.pyngl_enabled = True
"""Enable pyngl if it is installed."""
_try_enable_pyngl()


@init_local()
def disable_pyngl():
"""Disable pyngl."""
global _local_config
_local_config.pyngl_enabled = True
_local_config.pyngl_enabled = False


@init_local()
Expand Down