excolors.py
192 lines
| 6.4 KiB
| text/x-python
|
PythonLexer
fperez
|
r46 | # -*- coding: utf-8 -*- | ||
""" | ||||
Color schemes for exception handling code in IPython. | ||||
Fernando Perez
|
r1853 | """ | ||
fperez
|
r46 | |||
Segev Finer
|
r23317 | import os | ||
Matthias Bussonnier
|
r21777 | |||
fperez
|
r46 | #***************************************************************************** | ||
fperez
|
r88 | # Copyright (C) 2005-2006 Fernando Perez <fperez@colorado.edu> | ||
fperez
|
r46 | # | ||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#***************************************************************************** | ||||
Brian Granger
|
r2010 | from IPython.utils.coloransi import ColorSchemeTable, TermColors, ColorScheme | ||
fperez
|
r46 | |||
Fernando Perez
|
r1845 | def exception_colors(): | ||
"""Return a color table with fields for exception reporting. | ||||
The table is an instance of ColorSchemeTable with schemes added for | ||||
Matthias Bussonnier
|
r22609 | 'Neutral', 'Linux', 'LightBG' and 'NoColor' and fields for exception handling filled | ||
Fernando Perez
|
r1845 | in. | ||
Examples: | ||||
>>> ec = exception_colors() | ||||
>>> ec.active_scheme_name | ||||
'' | ||||
Thomas Kluyver
|
r13394 | >>> print(ec.active_colors) | ||
Fernando Perez
|
r1845 | None | ||
Bernardo B. Marques
|
r4872 | Now we activate a color scheme: | ||
Fernando Perez
|
r1845 | >>> ec.set_active_scheme('NoColor') | ||
>>> ec.active_scheme_name | ||||
'NoColor' | ||||
Thomas Kluyver
|
r7016 | >>> sorted(ec.active_colors.keys()) | ||
Matthias Bussonnier
|
r28711 | ['Normal', 'breakpoint_disabled', 'breakpoint_enabled', 'caret', 'em', | ||
'excName', 'filename', 'filenameEm', 'line', 'lineno', 'linenoEm', 'name', | ||||
'nameEm', 'normalEm', 'prompt', 'topline', 'vName', 'val', 'valEm'] | ||||
Fernando Perez
|
r1845 | """ | ||
Bernardo B. Marques
|
r4872 | |||
Fernando Perez
|
r1845 | ex_colors = ColorSchemeTable() | ||
# Populate it with color schemes | ||||
Matthias Bussonnier
|
r28587 | C = TermColors # shorthand and local lookup | ||
ex_colors.add_scheme( | ||||
ColorScheme( | ||||
"NoColor", | ||||
{ | ||||
# The color to be used for the top line | ||||
"topline": C.NoColor, | ||||
# The colors to be used in the traceback | ||||
"filename": C.NoColor, | ||||
"lineno": C.NoColor, | ||||
"name": C.NoColor, | ||||
"vName": C.NoColor, | ||||
"val": C.NoColor, | ||||
"em": C.NoColor, | ||||
# Emphasized colors for the last frame of the traceback | ||||
"normalEm": C.NoColor, | ||||
"filenameEm": C.NoColor, | ||||
"linenoEm": C.NoColor, | ||||
"nameEm": C.NoColor, | ||||
"valEm": C.NoColor, | ||||
# Colors for printing the exception | ||||
"excName": C.NoColor, | ||||
"line": C.NoColor, | ||||
"caret": C.NoColor, | ||||
"Normal": C.NoColor, | ||||
Matthias Bussonnier
|
r28711 | # debugger | ||
"prompt": C.NoColor, | ||||
"breakpoint_enabled": C.NoColor, | ||||
"breakpoint_disabled": C.NoColor, | ||||
Matthias Bussonnier
|
r28587 | }, | ||
) | ||||
) | ||||
Fernando Perez
|
r1845 | |||
# make some schemes as instances so we can copy them for modification easily | ||||
Matthias Bussonnier
|
r28587 | ex_colors.add_scheme( | ||
ColorScheme( | ||||
"Linux", | ||||
{ | ||||
# The color to be used for the top line | ||||
"topline": C.LightRed, | ||||
# The colors to be used in the traceback | ||||
"filename": C.Green, | ||||
"lineno": C.Green, | ||||
"name": C.Purple, | ||||
"vName": C.Cyan, | ||||
"val": C.Green, | ||||
"em": C.LightCyan, | ||||
# Emphasized colors for the last frame of the traceback | ||||
"normalEm": C.LightCyan, | ||||
"filenameEm": C.LightGreen, | ||||
"linenoEm": C.LightGreen, | ||||
"nameEm": C.LightPurple, | ||||
"valEm": C.LightBlue, | ||||
# Colors for printing the exception | ||||
"excName": C.LightRed, | ||||
"line": C.Yellow, | ||||
"caret": C.White, | ||||
"Normal": C.Normal, | ||||
Matthias Bussonnier
|
r28711 | # debugger | ||
"prompt": C.Green, | ||||
"breakpoint_enabled": C.LightRed, | ||||
"breakpoint_disabled": C.Red, | ||||
Matthias Bussonnier
|
r28587 | }, | ||
) | ||||
) | ||||
Fernando Perez
|
r1845 | |||
# For light backgrounds, swap dark/light colors | ||||
Matthias Bussonnier
|
r28587 | ex_colors.add_scheme( | ||
ColorScheme( | ||||
"LightBG", | ||||
{ | ||||
# The color to be used for the top line | ||||
"topline": C.Red, | ||||
# The colors to be used in the traceback | ||||
"filename": C.LightGreen, | ||||
"lineno": C.LightGreen, | ||||
"name": C.LightPurple, | ||||
"vName": C.Cyan, | ||||
"val": C.LightGreen, | ||||
"em": C.Cyan, | ||||
# Emphasized colors for the last frame of the traceback | ||||
"normalEm": C.Cyan, | ||||
"filenameEm": C.Green, | ||||
"linenoEm": C.Green, | ||||
"nameEm": C.Purple, | ||||
"valEm": C.Blue, | ||||
# Colors for printing the exception | ||||
"excName": C.Red, | ||||
# "line": C.Brown, # brown often is displayed as yellow | ||||
"line": C.Red, | ||||
"caret": C.Normal, | ||||
"Normal": C.Normal, | ||||
Matthias Bussonnier
|
r28711 | # debugger | ||
"prompt": C.Blue, | ||||
"breakpoint_enabled": C.LightRed, | ||||
"breakpoint_disabled": C.Red, | ||||
Matthias Bussonnier
|
r28587 | }, | ||
) | ||||
) | ||||
ex_colors.add_scheme( | ||||
ColorScheme( | ||||
"Neutral", | ||||
{ | ||||
# The color to be used for the top line | ||||
"topline": C.Red, | ||||
# The colors to be used in the traceback | ||||
"filename": C.LightGreen, | ||||
"lineno": C.LightGreen, | ||||
"name": C.LightPurple, | ||||
"vName": C.Cyan, | ||||
"val": C.LightGreen, | ||||
"em": C.Cyan, | ||||
# Emphasized colors for the last frame of the traceback | ||||
"normalEm": C.Cyan, | ||||
"filenameEm": C.Green, | ||||
"linenoEm": C.Green, | ||||
"nameEm": C.Purple, | ||||
"valEm": C.Blue, | ||||
# Colors for printing the exception | ||||
"excName": C.Red, | ||||
# line = C.Brown, # brown often is displayed as yellow | ||||
"line": C.Red, | ||||
"caret": C.Normal, | ||||
"Normal": C.Normal, | ||||
Matthias Bussonnier
|
r28711 | # debugger | ||
"prompt": C.Blue, | ||||
"breakpoint_enabled": C.LightRed, | ||||
"breakpoint_disabled": C.Red, | ||||
Matthias Bussonnier
|
r28587 | }, | ||
) | ||||
) | ||||
Matthias Bussonnier
|
r22609 | |||
Segev Finer
|
r23317 | # Hack: the 'neutral' colours are not very visible on a dark background on | ||
# Windows. Since Windows command prompts have a dark background by default, and | ||||
# relatively few users are likely to alter that, we will use the 'Linux' colours, | ||||
# designed for a dark background, as the default on Windows. | ||||
if os.name == "nt": | ||||
ex_colors.add_scheme(ex_colors['Linux'].copy('Neutral')) | ||||
Matthias Bussonnier
|
r22609 | |||
Fernando Perez
|
r1845 | return ex_colors | ||