10
10
_local_config = local ()
11
11
12
12
13
- def _init_local ():
13
+ def _try_enable_xarray ():
14
14
global _local_config
15
-
16
15
_local_config .xarray_enabled = True
17
- _local_config .cartopy_enabled = True
18
- _local_config .basemap_enabled = True
19
- _local_config .pyngl_enabled = True
20
- _local_config .cache_size = 20
21
- _local_config .initialized = True
22
-
23
16
try :
24
17
from xarray import DataArray
25
18
except ImportError :
26
19
_local_config .xarray_enabled = False
27
20
21
+
22
+ def _try_enable_cartopy ():
23
+ global _local_config
24
+ _local_config .cartopy_enabled = True
28
25
try :
29
26
from cartopy import crs
30
27
except ImportError :
31
28
_local_config .cartopy_enabled = False
32
29
30
+
31
+ def _try_enable_basemap ():
32
+ global _local_config
33
+ _local_config .basemap_enabled = True
33
34
try :
34
35
from mpl_toolkits .basemap import Basemap
35
36
except ImportError :
36
37
_local_config .basemap_enabled = False
37
38
39
+
40
+ def _try_enable_pyngl ():
41
+ global _local_config
42
+ _local_config .pyngl_enabled = True
38
43
try :
39
44
from Ngl import Resources
40
45
except ImportError :
41
46
_local_config .pyngl_enabled = False
42
47
43
48
49
+ def _init_local ():
50
+ global _local_config
51
+
52
+ _try_enable_xarray ()
53
+ _try_enable_cartopy ()
54
+ _try_enable_basemap ()
55
+ _try_enable_pyngl ()
56
+
57
+ _local_config .cache_size = 20
58
+ _local_config .initialized = True
59
+
60
+
44
61
# Initialize the main thread's configuration
45
62
_init_local ()
46
63
@@ -51,11 +68,11 @@ def init_local():
51
68
def func_wrapper (wrapped , instance , args , kwargs ):
52
69
global _local_config
53
70
try :
54
- init = _local_config .init
71
+ initialized = _local_config .initialized
55
72
except AttributeError :
56
73
_init_local ()
57
74
else :
58
- if not init :
75
+ if not initialized :
59
76
_init_local ()
60
77
61
78
return wrapped (* args , ** kwargs )
@@ -77,17 +94,16 @@ def xarray_enabled():
77
94
78
95
79
96
@init_local ()
80
- def disable_xarray ():
81
- """Disable xarray."""
82
- global _local_config
83
- _local_config .xarray_enabled = False
97
+ def enable_xarray ():
98
+ """Enable xarray if it is installed."""
99
+ _try_enable_xarray ()
84
100
85
101
86
102
@init_local ()
87
- def enable_xarray ():
88
- """Enable xarray."""
103
+ def disable_xarray ():
104
+ """Disable xarray."""
89
105
global _local_config
90
- _local_config .xarray_enabled = True
106
+ _local_config .xarray_enabled = False
91
107
92
108
93
109
@init_local ()
@@ -105,16 +121,15 @@ def cartopy_enabled():
105
121
106
122
@init_local ()
107
123
def enable_cartopy ():
108
- """Enable cartopy."""
109
- global _local_config
110
- _local_config .cartopy_enabled = True
124
+ """Enable cartopy if it is installed."""
125
+ _try_enable_cartopy ()
111
126
112
127
113
128
@init_local ()
114
129
def disable_cartopy ():
115
130
"""Disable cartopy."""
116
131
global _local_config
117
- _local_config .cartopy_enabled = True
132
+ _local_config .cartopy_enabled = False
118
133
119
134
120
135
@init_local ()
@@ -132,16 +147,15 @@ def basemap_enabled():
132
147
133
148
@init_local ()
134
149
def enable_basemap ():
135
- """Enable basemap."""
136
- global _local_config
137
- _local_config .basemap_enabled = True
150
+ """Enable basemap if it is installed."""
151
+ _try_enable_basemap ()
138
152
139
153
140
154
@init_local ()
141
155
def disable_basemap ():
142
156
"""Disable basemap."""
143
157
global _local_config
144
- _local_config .basemap_enabled = True
158
+ _local_config .basemap_enabled = False
145
159
146
160
147
161
@init_local ()
@@ -159,16 +173,15 @@ def pyngl_enabled():
159
173
160
174
@init_local ()
161
175
def enable_pyngl ():
162
- """Enable pyngl."""
163
- global _local_config
164
- _local_config .pyngl_enabled = True
176
+ """Enable pyngl if it is installed."""
177
+ _try_enable_pyngl ()
165
178
166
179
167
180
@init_local ()
168
181
def disable_pyngl ():
169
182
"""Disable pyngl."""
170
183
global _local_config
171
- _local_config .pyngl_enabled = True
184
+ _local_config .pyngl_enabled = False
172
185
173
186
174
187
@init_local ()
0 commit comments