forked from SciTools/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
285 lines (225 loc) · 9.28 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# Copyright Iris contributors
#
# This file is part of Iris and is released under the BSD license.
# See LICENSE in the root of the repository for full licensing details.
"""Provides access to Iris-specific configuration values.
The default configuration values can be overridden by creating the file
``iris/etc/site.cfg``. If it exists, this file must conform to the format
defined by :mod:`configparser`.
----------
.. py:data:: iris.config.TEST_DATA_DIR
Local directory where test data exists. Defaults to "test_data"
sub-directory of the Iris package install directory. The test data
directory supports the subset of Iris unit tests that require data.
Directory contents accessed via :func:`iris.tests.get_data_path`.
.. py:data:: iris.config.PALETTE_PATH
The full path to the Iris palette configuration directory
----------
"""
import configparser
import contextlib
import logging
import os.path
import warnings
import iris.warnings
def get_logger(name, datefmt=None, fmt=None, level=None, propagate=None, handler=True):
"""Create a custom class for logging.
Create a :class:`logging.Logger` with a :class:`logging.StreamHandler`
and custom :class:`logging.Formatter`.
Parameters
----------
name :
The name of the logger. Typically this is the module filename that
owns the logger.
datefmt : optional
The date format string of the :class:`logging.Formatter`.
Defaults to ``%d-%m-%Y %H:%M:%S``.
fmt : optional
The additional format string of the :class:`logging.Formatter`.
This is appended to the default format string
``%(asctime)s %(name)s %(levelname)s - %(message)s``.
level : optional
The threshold level of the logger. Defaults to ``INFO``.
propagate : optional
Sets the ``propagate`` attribute of the :class:`logging.Logger`,
which determines whether events logged to this logger will be
passed to the handlers of higher level loggers. Defaults to
``False``.
handler : bool, default=True
Create and attach a :class:`logging.StreamHandler` to the
logger. Defaults to ``True``.
Returns
-------
:class:`logging.Logger`.
"""
if level is None:
# Default logging level.
level = "INFO"
if propagate is None:
# Default logging propagate behaviour.
propagate = False
# Create the named logger.
logger = logging.getLogger(name)
logger.setLevel(level)
logger.propagate = propagate
# Create and add the handler to the logger, if required.
if handler:
if datefmt is None:
# Default date format string.
datefmt = "%d-%m-%Y %H:%M:%S"
# Default format string.
_fmt = "%(asctime)s %(name)s %(levelname)s - %(message)s"
# Append additional format string, if appropriate.
fmt = _fmt if fmt is None else f"{_fmt} {fmt}"
# Create a formatter.
formatter = logging.Formatter(fmt=fmt, datefmt=datefmt)
# Create a logging handler.
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
# Returns simple string options
def get_option(section, option, default=None):
"""Return the option value for the given section.
Returns the option value for the given section, or the default value
if the section/option is not present.
"""
value = default
if config.has_option(section, option):
value = config.get(section, option)
return value
# Returns directory path options
def get_dir_option(section, option, default=None):
"""Return the directory path from the given option and section.
Returns the directory path from the given option and section, or
returns the given default value if the section/option is not present
or does not represent a valid directory.
"""
path = default
if config.has_option(section, option):
c_path = config.get(section, option)
if os.path.isdir(c_path):
path = c_path
else:
msg = (
"Ignoring config item {!r}:{!r} (section:option) as {!r}"
" is not a valid directory path."
)
warnings.warn(
msg.format(section, option, c_path),
category=iris.warnings.IrisIgnoringWarning,
)
return path
# Figure out the full path to the "iris" package.
ROOT_PATH = os.path.abspath(os.path.dirname(__file__))
# The full path to the configuration directory of the active Iris instance.
CONFIG_PATH = os.path.join(ROOT_PATH, "etc")
# Load the optional "site.cfg" file if it exists.
config = configparser.ConfigParser()
config.read([os.path.join(CONFIG_PATH, "site.cfg")])
##################
# Resource options
_RESOURCE_SECTION = "Resources"
TEST_DATA_DIR = get_dir_option(
_RESOURCE_SECTION,
"test_data_dir",
default=os.path.join(os.path.dirname(__file__), "test_data"),
)
# Override the data repository if the appropriate environment variable
# has been set.
override = os.environ.get("OVERRIDE_TEST_DATA_REPOSITORY")
if override:
TEST_DATA_DIR = None
if os.path.isdir(os.path.expanduser(override)):
TEST_DATA_DIR = os.path.abspath(override)
PALETTE_PATH = get_dir_option(
_RESOURCE_SECTION, "palette_path", os.path.join(CONFIG_PATH, "palette")
)
# Runtime options
class NetCDF:
"""Control Iris NetCDF options."""
def __init__(self, conventions_override=None):
"""Set up NetCDF processing options for Iris.
Parameters
----------
conventions_override : bool, optional
Define whether the CF Conventions version (e.g. `CF-1.6`) set when
saving a cube to a NetCDF file should be defined by
Iris (the default) or the cube being saved. If `False`
(the default), specifies that Iris should set the
CF Conventions version when saving cubes as NetCDF files.
If `True`, specifies that the cubes being saved to NetCDF should
set the CF Conventions version for the saved NetCDF files.
Examples
--------
* Specify, for the lifetime of the session, that we want all cubes
written to NetCDF to define their own CF Conventions versions::
iris.config.netcdf.conventions_override = True
iris.save('my_cube', 'my_dataset.nc')
iris.save('my_second_cube', 'my_second_dataset.nc')
* Specify, with a context manager, that we want a cube written to
NetCDF to define its own CF Conventions version::
with iris.config.netcdf.context(conventions_override=True):
iris.save('my_cube', 'my_dataset.nc')
"""
# Define allowed `__dict__` keys first.
self.__dict__["conventions_override"] = None
# Now set specific values.
setattr(self, "conventions_override", conventions_override)
def __repr__(self):
msg = "NetCDF options: {}."
# Automatically populate with all currently accepted kwargs.
options = ["{}={}".format(k, v) for k, v in self.__dict__.items()]
joined = ", ".join(options)
return msg.format(joined)
def __setattr__(self, name, value):
if name not in self.__dict__:
# Can't add new names.
msg = "Cannot set option {!r} for {} configuration."
raise AttributeError(msg.format(name, self.__class__.__name__))
if value is None:
# Set an unset value to the name's default.
value = self._defaults_dict[name]["default"]
if self._defaults_dict[name]["options"] is not None:
# Replace a bad value with a good one if there is a defined set of
# specified good values. If there isn't, we can assume that
# anything goes.
if value not in self._defaults_dict[name]["options"]:
good_value = self._defaults_dict[name]["default"]
wmsg = (
"Attempting to set invalid value {!r} for "
"attribute {!r}. Defaulting to {!r}."
)
warnings.warn(
wmsg.format(value, name, good_value),
category=iris.warnings.IrisDefaultingWarning,
)
value = good_value
self.__dict__[name] = value
@property
def _defaults_dict(self):
# Set this as a property so that it isn't added to `self.__dict__`.
return {
"conventions_override": {
"default": False,
"options": [True, False],
},
}
@contextlib.contextmanager
def context(self, **kwargs):
"""Allow temporary modification of the options via a context manager.
Accepted kwargs are the same as can be supplied to the Option.
"""
# Snapshot the starting state for restoration at the end of the
# contextmanager block.
starting_state = self.__dict__.copy()
# Update the state to reflect the requested changes.
for name, value in kwargs.items():
setattr(self, name, value)
try:
yield
finally:
# Return the state to the starting state.
self.__dict__.clear()
self.__dict__.update(starting_state)
netcdf = NetCDF()