Show More
@@ -1,184 +1,166 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | Color schemes for exception handling code in IPython. |
|
3 | Color schemes for exception handling code in IPython. | |
4 | """ |
|
4 | """ | |
5 |
|
5 | |||
6 | import os |
|
6 | import os | |
7 | import warnings |
|
7 | import warnings | |
8 |
|
8 | |||
9 | #***************************************************************************** |
|
9 | #***************************************************************************** | |
10 | # Copyright (C) 2005-2006 Fernando Perez <fperez@colorado.edu> |
|
10 | # Copyright (C) 2005-2006 Fernando Perez <fperez@colorado.edu> | |
11 | # |
|
11 | # | |
12 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | # Distributed under the terms of the BSD License. The full license is in | |
13 | # the file COPYING, distributed as part of this software. |
|
13 | # the file COPYING, distributed as part of this software. | |
14 | #***************************************************************************** |
|
14 | #***************************************************************************** | |
15 |
|
15 | |||
16 | from IPython.utils.coloransi import ColorSchemeTable, TermColors, ColorScheme |
|
16 | from IPython.utils.coloransi import ColorSchemeTable, TermColors, ColorScheme | |
17 |
|
17 | |||
18 | def exception_colors(): |
|
18 | def exception_colors(): | |
19 | """Return a color table with fields for exception reporting. |
|
19 | """Return a color table with fields for exception reporting. | |
20 |
|
20 | |||
21 | The table is an instance of ColorSchemeTable with schemes added for |
|
21 | The table is an instance of ColorSchemeTable with schemes added for | |
22 | 'Neutral', 'Linux', 'LightBG' and 'NoColor' and fields for exception handling filled |
|
22 | 'Neutral', 'Linux', 'LightBG' and 'NoColor' and fields for exception handling filled | |
23 | in. |
|
23 | in. | |
24 |
|
24 | |||
25 | Examples: |
|
25 | Examples: | |
26 |
|
26 | |||
27 | >>> ec = exception_colors() |
|
27 | >>> ec = exception_colors() | |
28 | >>> ec.active_scheme_name |
|
28 | >>> ec.active_scheme_name | |
29 | '' |
|
29 | '' | |
30 | >>> print(ec.active_colors) |
|
30 | >>> print(ec.active_colors) | |
31 | None |
|
31 | None | |
32 |
|
32 | |||
33 | Now we activate a color scheme: |
|
33 | Now we activate a color scheme: | |
34 | >>> ec.set_active_scheme('NoColor') |
|
34 | >>> ec.set_active_scheme('NoColor') | |
35 | >>> ec.active_scheme_name |
|
35 | >>> ec.active_scheme_name | |
36 | 'NoColor' |
|
36 | 'NoColor' | |
37 | >>> sorted(ec.active_colors.keys()) |
|
37 | >>> sorted(ec.active_colors.keys()) | |
38 | ['Normal', 'caret', 'em', 'excName', 'filename', 'filenameEm', 'line', |
|
38 | ['Normal', 'caret', 'em', 'excName', 'filename', 'filenameEm', 'line', | |
39 | 'lineno', 'linenoEm', 'name', 'nameEm', 'normalEm', 'topline', 'vName', |
|
39 | 'lineno', 'linenoEm', 'name', 'nameEm', 'normalEm', 'topline', 'vName', | |
40 | 'val', 'valEm'] |
|
40 | 'val', 'valEm'] | |
41 | """ |
|
41 | """ | |
42 |
|
42 | |||
43 | ex_colors = ColorSchemeTable() |
|
43 | ex_colors = ColorSchemeTable() | |
44 |
|
44 | |||
45 | # Populate it with color schemes |
|
45 | # Populate it with color schemes | |
46 | C = TermColors # shorthand and local lookup |
|
46 | C = TermColors # shorthand and local lookup | |
47 | ex_colors.add_scheme(ColorScheme( |
|
47 | ex_colors.add_scheme(ColorScheme( | |
48 | 'NoColor', |
|
48 | 'NoColor', | |
49 | # The color to be used for the top line |
|
49 | # The color to be used for the top line | |
50 | topline = C.NoColor, |
|
50 | topline = C.NoColor, | |
51 |
|
51 | |||
52 | # The colors to be used in the traceback |
|
52 | # The colors to be used in the traceback | |
53 | filename = C.NoColor, |
|
53 | filename = C.NoColor, | |
54 | lineno = C.NoColor, |
|
54 | lineno = C.NoColor, | |
55 | name = C.NoColor, |
|
55 | name = C.NoColor, | |
56 | vName = C.NoColor, |
|
56 | vName = C.NoColor, | |
57 | val = C.NoColor, |
|
57 | val = C.NoColor, | |
58 | em = C.NoColor, |
|
58 | em = C.NoColor, | |
59 |
|
59 | |||
60 | # Emphasized colors for the last frame of the traceback |
|
60 | # Emphasized colors for the last frame of the traceback | |
61 | normalEm = C.NoColor, |
|
61 | normalEm = C.NoColor, | |
62 | filenameEm = C.NoColor, |
|
62 | filenameEm = C.NoColor, | |
63 | linenoEm = C.NoColor, |
|
63 | linenoEm = C.NoColor, | |
64 | nameEm = C.NoColor, |
|
64 | nameEm = C.NoColor, | |
65 | valEm = C.NoColor, |
|
65 | valEm = C.NoColor, | |
66 |
|
66 | |||
67 | # Colors for printing the exception |
|
67 | # Colors for printing the exception | |
68 | excName = C.NoColor, |
|
68 | excName = C.NoColor, | |
69 | line = C.NoColor, |
|
69 | line = C.NoColor, | |
70 | caret = C.NoColor, |
|
70 | caret = C.NoColor, | |
71 | Normal = C.NoColor |
|
71 | Normal = C.NoColor | |
72 | )) |
|
72 | )) | |
73 |
|
73 | |||
74 | # make some schemes as instances so we can copy them for modification easily |
|
74 | # make some schemes as instances so we can copy them for modification easily | |
75 | ex_colors.add_scheme(ColorScheme( |
|
75 | ex_colors.add_scheme(ColorScheme( | |
76 | 'Linux', |
|
76 | 'Linux', | |
77 | # The color to be used for the top line |
|
77 | # The color to be used for the top line | |
78 | topline = C.LightRed, |
|
78 | topline = C.LightRed, | |
79 |
|
79 | |||
80 | # The colors to be used in the traceback |
|
80 | # The colors to be used in the traceback | |
81 | filename = C.Green, |
|
81 | filename = C.Green, | |
82 | lineno = C.Green, |
|
82 | lineno = C.Green, | |
83 | name = C.Purple, |
|
83 | name = C.Purple, | |
84 | vName = C.Cyan, |
|
84 | vName = C.Cyan, | |
85 | val = C.Green, |
|
85 | val = C.Green, | |
86 | em = C.LightCyan, |
|
86 | em = C.LightCyan, | |
87 |
|
87 | |||
88 | # Emphasized colors for the last frame of the traceback |
|
88 | # Emphasized colors for the last frame of the traceback | |
89 | normalEm = C.LightCyan, |
|
89 | normalEm = C.LightCyan, | |
90 | filenameEm = C.LightGreen, |
|
90 | filenameEm = C.LightGreen, | |
91 | linenoEm = C.LightGreen, |
|
91 | linenoEm = C.LightGreen, | |
92 | nameEm = C.LightPurple, |
|
92 | nameEm = C.LightPurple, | |
93 | valEm = C.LightBlue, |
|
93 | valEm = C.LightBlue, | |
94 |
|
94 | |||
95 | # Colors for printing the exception |
|
95 | # Colors for printing the exception | |
96 | excName = C.LightRed, |
|
96 | excName = C.LightRed, | |
97 | line = C.Yellow, |
|
97 | line = C.Yellow, | |
98 | caret = C.White, |
|
98 | caret = C.White, | |
99 | Normal = C.Normal |
|
99 | Normal = C.Normal | |
100 | )) |
|
100 | )) | |
101 |
|
101 | |||
102 | # For light backgrounds, swap dark/light colors |
|
102 | # For light backgrounds, swap dark/light colors | |
103 | ex_colors.add_scheme(ColorScheme( |
|
103 | ex_colors.add_scheme(ColorScheme( | |
104 | 'LightBG', |
|
104 | 'LightBG', | |
105 | # The color to be used for the top line |
|
105 | # The color to be used for the top line | |
106 | topline = C.Red, |
|
106 | topline = C.Red, | |
107 |
|
107 | |||
108 | # The colors to be used in the traceback |
|
108 | # The colors to be used in the traceback | |
109 | filename = C.LightGreen, |
|
109 | filename = C.LightGreen, | |
110 | lineno = C.LightGreen, |
|
110 | lineno = C.LightGreen, | |
111 | name = C.LightPurple, |
|
111 | name = C.LightPurple, | |
112 | vName = C.Cyan, |
|
112 | vName = C.Cyan, | |
113 | val = C.LightGreen, |
|
113 | val = C.LightGreen, | |
114 | em = C.Cyan, |
|
114 | em = C.Cyan, | |
115 |
|
115 | |||
116 | # Emphasized colors for the last frame of the traceback |
|
116 | # Emphasized colors for the last frame of the traceback | |
117 | normalEm = C.Cyan, |
|
117 | normalEm = C.Cyan, | |
118 | filenameEm = C.Green, |
|
118 | filenameEm = C.Green, | |
119 | linenoEm = C.Green, |
|
119 | linenoEm = C.Green, | |
120 | nameEm = C.Purple, |
|
120 | nameEm = C.Purple, | |
121 | valEm = C.Blue, |
|
121 | valEm = C.Blue, | |
122 |
|
122 | |||
123 | # Colors for printing the exception |
|
123 | # Colors for printing the exception | |
124 | excName = C.Red, |
|
124 | excName = C.Red, | |
125 | #line = C.Brown, # brown often is displayed as yellow |
|
125 | #line = C.Brown, # brown often is displayed as yellow | |
126 | line = C.Red, |
|
126 | line = C.Red, | |
127 | caret = C.Normal, |
|
127 | caret = C.Normal, | |
128 | Normal = C.Normal, |
|
128 | Normal = C.Normal, | |
129 | )) |
|
129 | )) | |
130 |
|
130 | |||
131 | ex_colors.add_scheme(ColorScheme( |
|
131 | ex_colors.add_scheme(ColorScheme( | |
132 | 'Neutral', |
|
132 | 'Neutral', | |
133 | # The color to be used for the top line |
|
133 | # The color to be used for the top line | |
134 | topline = C.Red, |
|
134 | topline = C.Red, | |
135 |
|
135 | |||
136 | # The colors to be used in the traceback |
|
136 | # The colors to be used in the traceback | |
137 | filename = C.LightGreen, |
|
137 | filename = C.LightGreen, | |
138 | lineno = C.LightGreen, |
|
138 | lineno = C.LightGreen, | |
139 | name = C.LightPurple, |
|
139 | name = C.LightPurple, | |
140 | vName = C.Cyan, |
|
140 | vName = C.Cyan, | |
141 | val = C.LightGreen, |
|
141 | val = C.LightGreen, | |
142 | em = C.Cyan, |
|
142 | em = C.Cyan, | |
143 |
|
143 | |||
144 | # Emphasized colors for the last frame of the traceback |
|
144 | # Emphasized colors for the last frame of the traceback | |
145 | normalEm = C.Cyan, |
|
145 | normalEm = C.Cyan, | |
146 | filenameEm = C.Green, |
|
146 | filenameEm = C.Green, | |
147 | linenoEm = C.Green, |
|
147 | linenoEm = C.Green, | |
148 | nameEm = C.Purple, |
|
148 | nameEm = C.Purple, | |
149 | valEm = C.Blue, |
|
149 | valEm = C.Blue, | |
150 |
|
150 | |||
151 | # Colors for printing the exception |
|
151 | # Colors for printing the exception | |
152 | excName = C.Red, |
|
152 | excName = C.Red, | |
153 | #line = C.Brown, # brown often is displayed as yellow |
|
153 | #line = C.Brown, # brown often is displayed as yellow | |
154 | line = C.Red, |
|
154 | line = C.Red, | |
155 | caret = C.Normal, |
|
155 | caret = C.Normal, | |
156 | Normal = C.Normal, |
|
156 | Normal = C.Normal, | |
157 | )) |
|
157 | )) | |
158 |
|
158 | |||
159 | # Hack: the 'neutral' colours are not very visible on a dark background on |
|
159 | # Hack: the 'neutral' colours are not very visible on a dark background on | |
160 | # Windows. Since Windows command prompts have a dark background by default, and |
|
160 | # Windows. Since Windows command prompts have a dark background by default, and | |
161 | # relatively few users are likely to alter that, we will use the 'Linux' colours, |
|
161 | # relatively few users are likely to alter that, we will use the 'Linux' colours, | |
162 | # designed for a dark background, as the default on Windows. |
|
162 | # designed for a dark background, as the default on Windows. | |
163 | if os.name == "nt": |
|
163 | if os.name == "nt": | |
164 | ex_colors.add_scheme(ex_colors['Linux'].copy('Neutral')) |
|
164 | ex_colors.add_scheme(ex_colors['Linux'].copy('Neutral')) | |
165 |
|
165 | |||
166 | return ex_colors |
|
166 | return ex_colors | |
167 |
|
||||
168 | class Deprec(object): |
|
|||
169 |
|
||||
170 | def __init__(self, wrapped_obj): |
|
|||
171 | self.wrapped=wrapped_obj |
|
|||
172 |
|
||||
173 | def __getattr__(self, name): |
|
|||
174 | val = getattr(self.wrapped, name) |
|
|||
175 | warnings.warn("Using ExceptionColors global is deprecated and will be removed in IPython 6.0", |
|
|||
176 | DeprecationWarning, stacklevel=2) |
|
|||
177 | # using getattr after warnings break ipydoctest in weird way for 3.5 |
|
|||
178 | return val |
|
|||
179 |
|
||||
180 | # For backwards compatibility, keep around a single global object. Note that |
|
|||
181 | # this should NOT be used, the factory function should be used instead, since |
|
|||
182 | # these objects are stateful and it's very easy to get strange bugs if any code |
|
|||
183 | # modifies the module-level object's state. |
|
|||
184 | ExceptionColors = Deprec(exception_colors()) |
|
General Comments 0
You need to be logged in to leave comments.
Login now