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