forked from psyplot/psyplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwarning.py
More file actions
executable file
·124 lines (95 loc) · 4.23 KB
/
warning.py
File metadata and controls
executable file
·124 lines (95 loc) · 4.23 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
# 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"""
# Disclaimer
# ----------
#
# Copyright (C) 2021 Helmholtz-Zentrum Hereon
# Copyright (C) 2020-2021 Helmholtz-Zentrum Geesthacht
# Copyright (C) 2016-2021 University of Lausanne
#
# This file is part of psyplot and is released under the GNU LGPL-3.O license.
# See COPYING and COPYING.LESSER in the root of the repository for full
# licensing details.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3.0 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU LGPL-3.0 license for more details.
#
# You should have received a copy of the GNU LGPL-3.0 license
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import warnings
import logging
# 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', '\w', PsyPlotWarning, 'psyplot', 0)
if critical:
warnings.filterwarnings('ignore', '\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