-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathwarning.py
More file actions
executable file
·128 lines (96 loc) · 3.68 KB
/
warning.py
File metadata and controls
executable file
·128 lines (96 loc) · 3.68 KB
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
# coding: utf-8
"""Warning module of the psyplot python module.
This module controls the warning behaviour of the module via the python
builtin warnings module and introduces three new warning classes:
..autosummay::
PsPylotRuntimeWarning
PsyPlotWarning
PsyPlotCritical"""
# SPDX-FileCopyrightText: 2016-2024 University of Lausanne
# SPDX-FileCopyrightText: 2020-2021 Helmholtz-Zentrum Geesthacht
# SPDX-FileCopyrightText: 2021-2024 Helmholtz-Zentrum hereon GmbH
#
# SPDX-License-Identifier: LGPL-3.0-only
import logging
import warnings
# disable a warning about "comparison to 'None' in backend_pdf which occurs
# in the matplotlib.backends.backend_pdf.PdfPages class
warnings.filterwarnings(
"ignore",
"comparison",
FutureWarning,
"matplotlib.backends.backend_pdf",
2264,
)
# disable a warning about "np.array_split" that occurs for certain numpy
# versions
warnings.filterwarnings(
"ignore",
"in the future np.array_split will retain",
FutureWarning,
"numpy.lib.shape_base",
431,
)
# disable a warning about "elementwise comparison of a string" in the
# matplotlib.collection.Collection.get_edgecolor method that occurs for certain
# matplotlib and numpy versions
warnings.filterwarnings(
"ignore",
"elementwise comparison failed",
FutureWarning,
"matplotlib.collections",
590,
)
logger = logging.getLogger(__name__)
class PsyPlotRuntimeWarning(RuntimeWarning):
"""Runtime warning that appears only ones"""
pass
class PsyPlotWarning(UserWarning):
"""Normal UserWarning for psyplot module"""
pass
class PsyPlotCritical(UserWarning):
"""Critical UserWarning for psyplot module"""
pass
warnings.simplefilter("always", PsyPlotWarning, append=True)
warnings.simplefilter("always", PsyPlotCritical, append=True)
def disable_warnings(critical=False):
"""Function that disables all warnings and all critical warnings (if
critical evaluates to True) related to the psyplot Module.
Please note that you can also configure the warnings via the
psyplot.warning logger (logging.getLogger(psyplot.warning))."""
warnings.filterwarnings("ignore", r"\w", PsyPlotWarning, "psyplot", 0)
if critical:
warnings.filterwarnings("ignore", r"\w", PsyPlotCritical, "psyplot", 0)
def warn(message, category=PsyPlotWarning, logger=None):
"""wrapper around the warnings.warn function for non-critical warnings.
logger may be a logging.Logger instance"""
if logger is not None:
message = "[Warning by %s]\n%s" % (logger.name, message)
warnings.warn(message, category, stacklevel=3)
def critical(message, category=PsyPlotCritical, logger=None):
"""wrapper around the warnings.warn function for critical warnings.
logger may be a logging.Logger instance"""
if logger is not None:
message = "[Critical warning by %s]\n%s" % (logger.name, message)
warnings.warn(message, category, stacklevel=2)
old_showwarning = warnings.showwarning
def customwarn(message, category, filename, lineno, *args, **kwargs):
"""Use the psyplot.warning logger for categories being out of
PsyPlotWarning and PsyPlotCritical and the default warnings.showwarning
function for all the others."""
if category is PsyPlotWarning:
logger.warning(
warnings.formatwarning(
"\n%s" % message, category, filename, lineno
)
)
elif category is PsyPlotCritical:
logger.critical(
warnings.formatwarning(
"\n%s" % message, category, filename, lineno
),
exc_info=True,
)
else:
old_showwarning(message, category, filename, lineno, *args, **kwargs)
warnings.showwarning = customwarn