Show More
@@ -1,60 +1,60 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | Utilities for warnings. Shoudn't we just use the built in warnings module. |
|
3 | Utilities for warnings. Shoudn't we just use the built in warnings module. | |
4 | """ |
|
4 | """ | |
5 |
|
5 | |||
6 | # Copyright (c) IPython Development Team. |
|
6 | # Copyright (c) IPython Development Team. | |
7 | # Distributed under the terms of the Modified BSD License. |
|
7 | # Distributed under the terms of the Modified BSD License. | |
8 |
|
8 | |||
9 | from __future__ import print_function |
|
9 | from __future__ import print_function | |
10 |
|
10 | |||
11 | import sys |
|
11 | import sys | |
12 |
|
12 | |||
13 | from IPython.utils import io |
|
13 | from IPython.utils import io | |
14 |
|
14 | |||
15 | from warnings import warn |
|
15 | import warnings | |
16 |
|
16 | |||
17 | warn("The module IPython.utils.warn is deprecated, use the standard warnings module instead", DeprecationWarning) |
|
17 | warnings.warn("The module IPython.utils.warn is deprecated, use the standard warnings module instead", DeprecationWarning) | |
18 |
|
18 | |||
19 | def warn(msg,level=2,exit_val=1): |
|
19 | def warn(msg,level=2,exit_val=1): | |
20 | """Standard warning printer. Gives formatting consistency. |
|
20 | """Standard warning printer. Gives formatting consistency. | |
21 |
|
21 | |||
22 | Output is sent to io.stderr (sys.stderr by default). |
|
22 | Output is sent to io.stderr (sys.stderr by default). | |
23 |
|
23 | |||
24 | Options: |
|
24 | Options: | |
25 |
|
25 | |||
26 | -level(2): allows finer control: |
|
26 | -level(2): allows finer control: | |
27 | 0 -> Do nothing, dummy function. |
|
27 | 0 -> Do nothing, dummy function. | |
28 | 1 -> Print message. |
|
28 | 1 -> Print message. | |
29 | 2 -> Print 'WARNING:' + message. (Default level). |
|
29 | 2 -> Print 'WARNING:' + message. (Default level). | |
30 | 3 -> Print 'ERROR:' + message. |
|
30 | 3 -> Print 'ERROR:' + message. | |
31 | 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val). |
|
31 | 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val). | |
32 |
|
32 | |||
33 | -exit_val (1): exit value returned by sys.exit() for a level 4 |
|
33 | -exit_val (1): exit value returned by sys.exit() for a level 4 | |
34 | warning. Ignored for all other levels.""" |
|
34 | warning. Ignored for all other levels.""" | |
35 |
|
35 | |||
36 | warn("The module IPython.utils.warn is deprecated, use the standard warnings module instead", DeprecationWarning) |
|
36 | warnings.warn("The module IPython.utils.warn is deprecated, use the standard warnings module instead", DeprecationWarning) | |
37 | if level>0: |
|
37 | if level>0: | |
38 | header = ['','','WARNING: ','ERROR: ','FATAL ERROR: '] |
|
38 | header = ['','','WARNING: ','ERROR: ','FATAL ERROR: '] | |
39 | print(header[level], msg, sep='', file=io.stderr) |
|
39 | print(header[level], msg, sep='', file=io.stderr) | |
40 | if level == 4: |
|
40 | if level == 4: | |
41 | print('Exiting.\n', file=io.stderr) |
|
41 | print('Exiting.\n', file=io.stderr) | |
42 | sys.exit(exit_val) |
|
42 | sys.exit(exit_val) | |
43 |
|
43 | |||
44 |
|
44 | |||
45 | def info(msg): |
|
45 | def info(msg): | |
46 | """Equivalent to warn(msg,level=1).""" |
|
46 | """Equivalent to warn(msg,level=1).""" | |
47 |
|
47 | |||
48 | warn(msg,level=1) |
|
48 | warn(msg,level=1) | |
49 |
|
49 | |||
50 |
|
50 | |||
51 | def error(msg): |
|
51 | def error(msg): | |
52 | """Equivalent to warn(msg,level=3).""" |
|
52 | """Equivalent to warn(msg,level=3).""" | |
53 |
|
53 | |||
54 | warn(msg,level=3) |
|
54 | warn(msg,level=3) | |
55 |
|
55 | |||
56 |
|
56 | |||
57 | def fatal(msg,exit_val=1): |
|
57 | def fatal(msg,exit_val=1): | |
58 | """Equivalent to warn(msg,exit_val=exit_val,level=4).""" |
|
58 | """Equivalent to warn(msg,exit_val=exit_val,level=4).""" | |
59 |
|
59 | |||
60 | warn(msg,exit_val=exit_val,level=4) |
|
60 | warn(msg,exit_val=exit_val,level=4) |
General Comments 0
You need to be logged in to leave comments.
Login now