Show More
@@ -1,170 +1,179 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Tools for coloring text in ANSI terminals. |
|
3 | 3 | |
|
4 | 4 | $Id: ColorANSI.py 2167 2007-03-21 06:57:50Z fperez $""" |
|
5 | 5 | |
|
6 | 6 | #***************************************************************************** |
|
7 | 7 | # Copyright (C) 2002-2006 Fernando Perez. <fperez@colorado.edu> |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | 10 | # the file COPYING, distributed as part of this software. |
|
11 | 11 | #***************************************************************************** |
|
12 | 12 | |
|
13 | 13 | from IPython import Release |
|
14 | 14 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
15 | 15 | __license__ = Release.license |
|
16 | 16 | |
|
17 | 17 | __all__ = ['TermColors','InputTermColors','ColorScheme','ColorSchemeTable'] |
|
18 | 18 | |
|
19 | 19 | import os |
|
20 | 20 | |
|
21 | 21 | from IPython.ipstruct import Struct |
|
22 | 22 | |
|
23 | 23 | def make_color_table(in_class): |
|
24 | 24 | """Build a set of color attributes in a class. |
|
25 | 25 | |
|
26 | 26 | Helper function for building the *TermColors classes.""" |
|
27 | 27 | |
|
28 | 28 | color_templates = ( |
|
29 | 29 | ("Black" , "0;30"), |
|
30 | 30 | ("Red" , "0;31"), |
|
31 | 31 | ("Green" , "0;32"), |
|
32 | 32 | ("Brown" , "0;33"), |
|
33 | 33 | ("Blue" , "0;34"), |
|
34 | 34 | ("Purple" , "0;35"), |
|
35 | 35 | ("Cyan" , "0;36"), |
|
36 | 36 | ("LightGray" , "0;37"), |
|
37 | 37 | ("DarkGray" , "1;30"), |
|
38 | 38 | ("LightRed" , "1;31"), |
|
39 | 39 | ("LightGreen" , "1;32"), |
|
40 | 40 | ("Yellow" , "1;33"), |
|
41 | 41 | ("LightBlue" , "1;34"), |
|
42 | 42 | ("LightPurple" , "1;35"), |
|
43 | 43 | ("LightCyan" , "1;36"), |
|
44 | 44 | ("White" , "1;37"), ) |
|
45 | 45 | |
|
46 | 46 | for name,value in color_templates: |
|
47 | 47 | setattr(in_class,name,in_class._base % value) |
|
48 | 48 | |
|
49 | # Automatically add Blink versions of the above colors. This | |
|
50 | # just involves setting the attribute field (before the first | |
|
51 | # semicolon) to '5'. | |
|
52 | # Note: this is a little easter egg contributed by Peter Wang from | |
|
53 | # Enthought at the Scipy2008 sprint :) | |
|
54 | value = '5'+value[1:] | |
|
55 | setattr(in_class,"Blink"+name,in_class._base % value) | |
|
56 | ||
|
57 | ||
|
49 | 58 | class TermColors: |
|
50 | 59 | """Color escape sequences. |
|
51 | 60 | |
|
52 | 61 | This class defines the escape sequences for all the standard (ANSI?) |
|
53 | 62 | colors in terminals. Also defines a NoColor escape which is just the null |
|
54 | 63 | string, suitable for defining 'dummy' color schemes in terminals which get |
|
55 | 64 | confused by color escapes. |
|
56 | 65 | |
|
57 | 66 | This class should be used as a mixin for building color schemes.""" |
|
58 | 67 | |
|
59 | 68 | NoColor = '' # for color schemes in color-less terminals. |
|
60 | 69 | Normal = '\033[0m' # Reset normal coloring |
|
61 | 70 | _base = '\033[%sm' # Template for all other colors |
|
62 | 71 | |
|
63 | 72 | # Build the actual color table as a set of class attributes: |
|
64 | 73 | make_color_table(TermColors) |
|
65 | 74 | |
|
66 | 75 | class InputTermColors: |
|
67 | 76 | """Color escape sequences for input prompts. |
|
68 | 77 | |
|
69 | 78 | This class is similar to TermColors, but the escapes are wrapped in \001 |
|
70 | 79 | and \002 so that readline can properly know the length of each line and |
|
71 | 80 | can wrap lines accordingly. Use this class for any colored text which |
|
72 | 81 | needs to be used in input prompts, such as in calls to raw_input(). |
|
73 | 82 | |
|
74 | 83 | This class defines the escape sequences for all the standard (ANSI?) |
|
75 | 84 | colors in terminals. Also defines a NoColor escape which is just the null |
|
76 | 85 | string, suitable for defining 'dummy' color schemes in terminals which get |
|
77 | 86 | confused by color escapes. |
|
78 | 87 | |
|
79 | 88 | This class should be used as a mixin for building color schemes.""" |
|
80 | 89 | |
|
81 | 90 | NoColor = '' # for color schemes in color-less terminals. |
|
82 | 91 | |
|
83 | 92 | if os.name == 'nt' and os.environ.get('TERM','dumb') == 'emacs': |
|
84 | 93 | # (X)emacs on W32 gets confused with \001 and \002 so we remove them |
|
85 | 94 | Normal = '\033[0m' # Reset normal coloring |
|
86 | 95 | _base = '\033[%sm' # Template for all other colors |
|
87 | 96 | else: |
|
88 | 97 | Normal = '\001\033[0m\002' # Reset normal coloring |
|
89 | 98 | _base = '\001\033[%sm\002' # Template for all other colors |
|
90 | 99 | |
|
91 | 100 | # Build the actual color table as a set of class attributes: |
|
92 | 101 | make_color_table(InputTermColors) |
|
93 | 102 | |
|
94 | 103 | class ColorScheme: |
|
95 | 104 | """Generic color scheme class. Just a name and a Struct.""" |
|
96 | 105 | def __init__(self,__scheme_name_,colordict=None,**colormap): |
|
97 | 106 | self.name = __scheme_name_ |
|
98 | 107 | if colordict is None: |
|
99 | 108 | self.colors = Struct(**colormap) |
|
100 | 109 | else: |
|
101 | 110 | self.colors = Struct(colordict) |
|
102 | 111 | |
|
103 | 112 | def copy(self,name=None): |
|
104 | 113 | """Return a full copy of the object, optionally renaming it.""" |
|
105 | 114 | if name is None: |
|
106 | 115 | name = self.name |
|
107 | 116 | return ColorScheme(name,self.colors.__dict__) |
|
108 | 117 | |
|
109 | 118 | class ColorSchemeTable(dict): |
|
110 | 119 | """General class to handle tables of color schemes. |
|
111 | 120 | |
|
112 | 121 | It's basically a dict of color schemes with a couple of shorthand |
|
113 | 122 | attributes and some convenient methods. |
|
114 | 123 | |
|
115 | 124 | active_scheme_name -> obvious |
|
116 | 125 | active_colors -> actual color table of the active scheme""" |
|
117 | 126 | |
|
118 | 127 | def __init__(self,scheme_list=None,default_scheme=''): |
|
119 | 128 | """Create a table of color schemes. |
|
120 | 129 | |
|
121 | 130 | The table can be created empty and manually filled or it can be |
|
122 | 131 | created with a list of valid color schemes AND the specification for |
|
123 | 132 | the default active scheme. |
|
124 | 133 | """ |
|
125 | 134 | |
|
126 | 135 | # create object attributes to be set later |
|
127 | 136 | self.active_scheme_name = '' |
|
128 | 137 | self.active_colors = None |
|
129 | 138 | |
|
130 | 139 | if scheme_list: |
|
131 | 140 | if default_scheme == '': |
|
132 | 141 | raise ValueError,'you must specify the default color scheme' |
|
133 | 142 | for scheme in scheme_list: |
|
134 | 143 | self.add_scheme(scheme) |
|
135 | 144 | self.set_active_scheme(default_scheme) |
|
136 | 145 | |
|
137 | 146 | def copy(self): |
|
138 | 147 | """Return full copy of object""" |
|
139 | 148 | return ColorSchemeTable(self.values(),self.active_scheme_name) |
|
140 | 149 | |
|
141 | 150 | def add_scheme(self,new_scheme): |
|
142 | 151 | """Add a new color scheme to the table.""" |
|
143 | 152 | if not isinstance(new_scheme,ColorScheme): |
|
144 | 153 | raise ValueError,'ColorSchemeTable only accepts ColorScheme instances' |
|
145 | 154 | self[new_scheme.name] = new_scheme |
|
146 | 155 | |
|
147 | 156 | def set_active_scheme(self,scheme,case_sensitive=0): |
|
148 | 157 | """Set the currently active scheme. |
|
149 | 158 | |
|
150 | 159 | Names are by default compared in a case-insensitive way, but this can |
|
151 | 160 | be changed by setting the parameter case_sensitive to true.""" |
|
152 | 161 | |
|
153 | 162 | scheme_names = self.keys() |
|
154 | 163 | if case_sensitive: |
|
155 | 164 | valid_schemes = scheme_names |
|
156 | 165 | scheme_test = scheme |
|
157 | 166 | else: |
|
158 | 167 | valid_schemes = [s.lower() for s in scheme_names] |
|
159 | 168 | scheme_test = scheme.lower() |
|
160 | 169 | try: |
|
161 | 170 | scheme_idx = valid_schemes.index(scheme_test) |
|
162 | 171 | except ValueError: |
|
163 | 172 | raise ValueError,'Unrecognized color scheme: ' + scheme + \ |
|
164 | 173 | '\nValid schemes: '+str(scheme_names).replace("'', ",'') |
|
165 | 174 | else: |
|
166 | 175 | active = scheme_names[scheme_idx] |
|
167 | 176 | self.active_scheme_name = active |
|
168 | 177 | self.active_colors = self[active].colors |
|
169 | 178 | # Now allow using '' as an index for the current active scheme |
|
170 | 179 | self[''] = self[active] |
@@ -1,85 +1,85 b'' | |||
|
1 | 1 | #!python |
|
2 | 2 | """Windows-specific part of the installation""" |
|
3 | 3 | |
|
4 | 4 | import os, sys, shutil |
|
5 | 5 | |
|
6 | 6 | def mkshortcut(target,description,link_file,*args,**kw): |
|
7 | 7 | """make a shortcut if it doesn't exist, and register its creation""" |
|
8 | 8 | |
|
9 | 9 | create_shortcut(target, description, link_file,*args,**kw) |
|
10 | 10 | file_created(link_file) |
|
11 | 11 | |
|
12 | 12 | def install(): |
|
13 | 13 | """Routine to be run by the win32 installer with the -install switch.""" |
|
14 | 14 | |
|
15 | 15 | from IPython.Release import version |
|
16 | 16 | |
|
17 | 17 | # Get some system constants |
|
18 | 18 | prefix = sys.prefix |
|
19 | 19 | python = prefix + r'\python.exe' |
|
20 | 20 | # Lookup path to common startmenu ... |
|
21 | 21 | ip_dir = get_special_folder_path('CSIDL_COMMON_PROGRAMS') + r'\IPython' |
|
22 | 22 | |
|
23 | 23 | # Some usability warnings at installation time. I don't want them at the |
|
24 | 24 | # top-level, so they don't appear if the user is uninstalling. |
|
25 | 25 | try: |
|
26 | 26 | import ctypes |
|
27 | 27 | except ImportError: |
|
28 | 28 | print ('To take full advantage of IPython, you need ctypes from:\n' |
|
29 | 29 | 'http://sourceforge.net/projects/ctypes') |
|
30 | 30 | |
|
31 | 31 | try: |
|
32 | 32 | import win32con |
|
33 | 33 | except ImportError: |
|
34 | 34 | print ('To take full advantage of IPython, you need pywin32 from:\n' |
|
35 | 35 | 'http://starship.python.net/crew/mhammond/win32/Downloads.html') |
|
36 | 36 | |
|
37 | 37 | try: |
|
38 | 38 | import readline |
|
39 | 39 | except ImportError: |
|
40 | 40 | print ('To take full advantage of IPython, you need readline from:\n' |
|
41 | 41 | 'http://sourceforge.net/projects/uncpythontools') |
|
42 | 42 | |
|
43 | ipybase = '"'+prefix+r'\scripts\ipython"' | |
|
43 | ipybase = '"' + prefix + r'\scripts\ipython"' | |
|
44 | 44 | # Create IPython entry ... |
|
45 | 45 | if not os.path.isdir(ip_dir): |
|
46 | 46 | os.mkdir(ip_dir) |
|
47 | 47 | directory_created(ip_dir) |
|
48 | 48 | |
|
49 | 49 | # Create program shortcuts ... |
|
50 | 50 | f = ip_dir + r'\IPython.lnk' |
|
51 | 51 | a = ipybase |
|
52 | 52 | mkshortcut(python,'IPython',f,a) |
|
53 | 53 | |
|
54 | 54 | f = ip_dir + r'\pysh.lnk' |
|
55 | 55 | a = ipybase+' -p sh' |
|
56 | 56 | mkshortcut(python,'IPython command prompt mode',f,a) |
|
57 | 57 | |
|
58 | 58 | f = ip_dir + r'\scipy.lnk' |
|
59 | 59 | a = ipybase+' -pylab -p scipy' |
|
60 | 60 | mkshortcut(python,'IPython scipy profile',f,a) |
|
61 | 61 | |
|
62 |
# Create documentation shortcuts ... |
|
|
62 | # Create documentation shortcuts ... | |
|
63 | 63 | t = prefix + r'\share\doc\ipython\manual\ipython.pdf' |
|
64 | 64 | f = ip_dir + r'\Manual in PDF.lnk' |
|
65 | 65 | mkshortcut(t,r'IPython Manual - PDF-Format',f) |
|
66 | 66 | |
|
67 |
t = prefix + r'\share\doc\ipython\manual\i |
|
|
67 | t = prefix + r'\share\doc\ipython\manual\html\index.html' | |
|
68 | 68 | f = ip_dir + r'\Manual in HTML.lnk' |
|
69 | 69 | mkshortcut(t,'IPython Manual - HTML-Format',f) |
|
70 | 70 | |
|
71 | 71 | # make ipython.py |
|
72 | 72 | shutil.copy(prefix + r'\scripts\ipython', prefix + r'\scripts\ipython.py') |
|
73 | 73 | |
|
74 | 74 | def remove(): |
|
75 | 75 | """Routine to be run by the win32 installer with the -remove switch.""" |
|
76 | 76 | pass |
|
77 | 77 | |
|
78 | 78 | # main() |
|
79 | 79 | if len(sys.argv) > 1: |
|
80 | 80 | if sys.argv[1] == '-install': |
|
81 | 81 | install() |
|
82 | 82 | elif sys.argv[1] == '-remove': |
|
83 | 83 | remove() |
|
84 | 84 | else: |
|
85 | 85 | print "Script was called with option %s" % sys.argv[1] |
General Comments 0
You need to be logged in to leave comments.
Login now