##// END OF EJS Templates
Fix color schemes in Linux...
Tayfun Sen -
Show More
@@ -1,339 +1,342 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Class and program to colorize python source code for ANSI terminals.
3 Class and program to colorize python source code for ANSI terminals.
4
4
5 Based on an HTML code highlighter by Jurgen Hermann found at:
5 Based on an HTML code highlighter by Jurgen Hermann found at:
6 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52298
6 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52298
7
7
8 Modifications by Fernando Perez (fperez@colorado.edu).
8 Modifications by Fernando Perez (fperez@colorado.edu).
9
9
10 Information on the original HTML highlighter follows:
10 Information on the original HTML highlighter follows:
11
11
12 MoinMoin - Python Source Parser
12 MoinMoin - Python Source Parser
13
13
14 Title: Colorize Python source using the built-in tokenizer
14 Title: Colorize Python source using the built-in tokenizer
15
15
16 Submitter: Jurgen Hermann
16 Submitter: Jurgen Hermann
17 Last Updated:2001/04/06
17 Last Updated:2001/04/06
18
18
19 Version no:1.2
19 Version no:1.2
20
20
21 Description:
21 Description:
22
22
23 This code is part of MoinMoin (http://moin.sourceforge.net/) and converts
23 This code is part of MoinMoin (http://moin.sourceforge.net/) and converts
24 Python source code to HTML markup, rendering comments, keywords,
24 Python source code to HTML markup, rendering comments, keywords,
25 operators, numeric and string literals in different colors.
25 operators, numeric and string literals in different colors.
26
26
27 It shows how to use the built-in keyword, token and tokenize modules to
27 It shows how to use the built-in keyword, token and tokenize modules to
28 scan Python source code and re-emit it with no changes to its original
28 scan Python source code and re-emit it with no changes to its original
29 formatting (which is the hard part).
29 formatting (which is the hard part).
30 """
30 """
31 from __future__ import print_function
31 from __future__ import print_function
32 from __future__ import absolute_import
32 from __future__ import absolute_import
33 from __future__ import unicode_literals
33 from __future__ import unicode_literals
34
34
35 __all__ = ['ANSICodeColors','Parser']
35 __all__ = ['ANSICodeColors','Parser']
36
36
37 _scheme_default = 'Linux'
37 _scheme_default = 'Linux'
38
38
39
39
40 # Imports
40 # Imports
41 import keyword
41 import keyword
42 import os
42 import os
43 import sys
43 import sys
44 import token
44 import token
45 import tokenize
45 import tokenize
46
46
47 try:
47 try:
48 generate_tokens = tokenize.generate_tokens
48 generate_tokens = tokenize.generate_tokens
49 except AttributeError:
49 except AttributeError:
50 # Python 3. Note that we use the undocumented _tokenize because it expects
50 # Python 3. Note that we use the undocumented _tokenize because it expects
51 # strings, not bytes. See also Python issue #9969.
51 # strings, not bytes. See also Python issue #9969.
52 generate_tokens = tokenize._tokenize
52 generate_tokens = tokenize._tokenize
53
53
54 from IPython.utils.coloransi import TermColors, InputTermColors ,ColorScheme, ColorSchemeTable
54 from IPython.utils.coloransi import TermColors, InputTermColors ,ColorScheme, ColorSchemeTable
55 from IPython.utils.py3compat import PY3
55 from IPython.utils.py3compat import PY3
56
56
57 if PY3:
57 if PY3:
58 from io import StringIO
58 from io import StringIO
59 else:
59 else:
60 from StringIO import StringIO
60 from StringIO import StringIO
61
61
62 #############################################################################
62 #############################################################################
63 ### Python Source Parser (does Hilighting)
63 ### Python Source Parser (does Hilighting)
64 #############################################################################
64 #############################################################################
65
65
66 _KEYWORD = token.NT_OFFSET + 1
66 _KEYWORD = token.NT_OFFSET + 1
67 _TEXT = token.NT_OFFSET + 2
67 _TEXT = token.NT_OFFSET + 2
68
68
69 #****************************************************************************
69 #****************************************************************************
70 # Builtin color schemes
70 # Builtin color schemes
71
71
72 Colors = TermColors # just a shorthand
72 Colors = TermColors # just a shorthand
73
73
74 # Build a few color schemes
74 # Build a few color schemes
75 NoColor = ColorScheme(
75 NoColor = ColorScheme(
76 'NoColor',{
76 'NoColor',{
77 'header' : Colors.NoColor,
77 'header' : Colors.NoColor,
78 token.NUMBER : Colors.NoColor,
78 token.NUMBER : Colors.NoColor,
79 token.OP : Colors.NoColor,
79 token.OP : Colors.NoColor,
80 token.STRING : Colors.NoColor,
80 token.STRING : Colors.NoColor,
81 tokenize.COMMENT : Colors.NoColor,
81 tokenize.COMMENT : Colors.NoColor,
82 token.NAME : Colors.NoColor,
82 token.NAME : Colors.NoColor,
83 token.ERRORTOKEN : Colors.NoColor,
83 token.ERRORTOKEN : Colors.NoColor,
84
84
85 _KEYWORD : Colors.NoColor,
85 _KEYWORD : Colors.NoColor,
86 _TEXT : Colors.NoColor,
86 _TEXT : Colors.NoColor,
87
87
88 'in_prompt' : InputTermColors.NoColor, # Input prompt
88 'in_prompt' : InputTermColors.NoColor, # Input prompt
89 'in_number' : InputTermColors.NoColor, # Input prompt number
89 'in_number' : InputTermColors.NoColor, # Input prompt number
90 'in_prompt2' : InputTermColors.NoColor, # Continuation prompt
90 'in_prompt2' : InputTermColors.NoColor, # Continuation prompt
91 'in_normal' : InputTermColors.NoColor, # color off (usu. Colors.Normal)
91
92
92 'out_prompt' : InputTermColors.NoColor, # Output prompt
93 'out_prompt' : Colors.NoColor, # Output prompt
93 'out_number' : InputTermColors.NoColor, # Output prompt number
94 'out_number' : Colors.NoColor, # Output prompt number
94
95
95 'normal' : InputTermColors.NoColor # color off (usu. Colors.Normal)
96 'normal' : Colors.NoColor # color off (usu. Colors.Normal)
96 } )
97 } )
97
98
98 LinuxColors = ColorScheme(
99 LinuxColors = ColorScheme(
99 'Linux',{
100 'Linux',{
100 'header' : Colors.LightRed,
101 'header' : Colors.LightRed,
101 token.NUMBER : Colors.LightCyan,
102 token.NUMBER : Colors.LightCyan,
102 token.OP : Colors.Yellow,
103 token.OP : Colors.Yellow,
103 token.STRING : Colors.LightBlue,
104 token.STRING : Colors.LightBlue,
104 tokenize.COMMENT : Colors.LightRed,
105 tokenize.COMMENT : Colors.LightRed,
105 token.NAME : Colors.Normal,
106 token.NAME : Colors.Normal,
106 token.ERRORTOKEN : Colors.Red,
107 token.ERRORTOKEN : Colors.Red,
107
108
108 _KEYWORD : Colors.LightGreen,
109 _KEYWORD : Colors.LightGreen,
109 _TEXT : Colors.Yellow,
110 _TEXT : Colors.Yellow,
110
111
111 'in_prompt' : InputTermColors.Green,
112 'in_prompt' : InputTermColors.Green,
112 'in_number' : InputTermColors.LightGreen,
113 'in_number' : InputTermColors.LightGreen,
113 'in_prompt2' : InputTermColors.Green,
114 'in_prompt2' : InputTermColors.Green,
115 'in_normal' : InputTermColors.Normal, # color off (usu. Colors.Normal)
114
116
115 'out_prompt' : InputTermColors.Red,
117 'out_prompt' : Colors.Red,
116 'out_number' : InputTermColors.LightRed,
118 'out_number' : Colors.LightRed,
117
119
118 'normal' : InputTermColors.Normal # color off (usu. Colors.Normal)
120 'normal' : Colors.Normal # color off (usu. Colors.Normal)
119 } )
121 } )
120
122
121 LightBGColors = ColorScheme(
123 LightBGColors = ColorScheme(
122 'LightBG',{
124 'LightBG',{
123 'header' : Colors.Red,
125 'header' : Colors.Red,
124 token.NUMBER : Colors.Cyan,
126 token.NUMBER : Colors.Cyan,
125 token.OP : Colors.Blue,
127 token.OP : Colors.Blue,
126 token.STRING : Colors.Blue,
128 token.STRING : Colors.Blue,
127 tokenize.COMMENT : Colors.Red,
129 tokenize.COMMENT : Colors.Red,
128 token.NAME : Colors.Normal,
130 token.NAME : Colors.Normal,
129 token.ERRORTOKEN : Colors.Red,
131 token.ERRORTOKEN : Colors.Red,
130
132
131 _KEYWORD : Colors.Green,
133 _KEYWORD : Colors.Green,
132 _TEXT : Colors.Blue,
134 _TEXT : Colors.Blue,
133
135
134 'in_prompt' : InputTermColors.Blue,
136 'in_prompt' : InputTermColors.Blue,
135 'in_number' : InputTermColors.LightBlue,
137 'in_number' : InputTermColors.LightBlue,
136 'in_prompt2' : InputTermColors.Blue,
138 'in_prompt2' : InputTermColors.Blue,
139 'in_normal' : InputTermColors.Normal, # color off (usu. Colors.Normal)
137
140
138 'out_prompt' : InputTermColors.Red,
141 'out_prompt' : Colors.Red,
139 'out_number' : InputTermColors.LightRed,
142 'out_number' : Colors.LightRed,
140
143
141 'normal' : InputTermColors.Normal # color off (usu. Colors.Normal)
144 'normal' : Colors.Normal # color off (usu. Colors.Normal)
142 } )
145 } )
143
146
144 # Build table of color schemes (needed by the parser)
147 # Build table of color schemes (needed by the parser)
145 ANSICodeColors = ColorSchemeTable([NoColor,LinuxColors,LightBGColors],
148 ANSICodeColors = ColorSchemeTable([NoColor,LinuxColors,LightBGColors],
146 _scheme_default)
149 _scheme_default)
147
150
148 class Parser:
151 class Parser:
149 """ Format colored Python source.
152 """ Format colored Python source.
150 """
153 """
151
154
152 def __init__(self, color_table=None,out = sys.stdout):
155 def __init__(self, color_table=None,out = sys.stdout):
153 """ Create a parser with a specified color table and output channel.
156 """ Create a parser with a specified color table and output channel.
154
157
155 Call format() to process code.
158 Call format() to process code.
156 """
159 """
157 self.color_table = color_table and color_table or ANSICodeColors
160 self.color_table = color_table and color_table or ANSICodeColors
158 self.out = out
161 self.out = out
159
162
160 def format(self, raw, out = None, scheme = ''):
163 def format(self, raw, out = None, scheme = ''):
161 return self.format2(raw, out, scheme)[0]
164 return self.format2(raw, out, scheme)[0]
162
165
163 def format2(self, raw, out = None, scheme = ''):
166 def format2(self, raw, out = None, scheme = ''):
164 """ Parse and send the colored source.
167 """ Parse and send the colored source.
165
168
166 If out and scheme are not specified, the defaults (given to
169 If out and scheme are not specified, the defaults (given to
167 constructor) are used.
170 constructor) are used.
168
171
169 out should be a file-type object. Optionally, out can be given as the
172 out should be a file-type object. Optionally, out can be given as the
170 string 'str' and the parser will automatically return the output in a
173 string 'str' and the parser will automatically return the output in a
171 string."""
174 string."""
172
175
173 string_output = 0
176 string_output = 0
174 if out == 'str' or self.out == 'str' or \
177 if out == 'str' or self.out == 'str' or \
175 isinstance(self.out,StringIO):
178 isinstance(self.out,StringIO):
176 # XXX - I don't really like this state handling logic, but at this
179 # XXX - I don't really like this state handling logic, but at this
177 # point I don't want to make major changes, so adding the
180 # point I don't want to make major changes, so adding the
178 # isinstance() check is the simplest I can do to ensure correct
181 # isinstance() check is the simplest I can do to ensure correct
179 # behavior.
182 # behavior.
180 out_old = self.out
183 out_old = self.out
181 self.out = StringIO()
184 self.out = StringIO()
182 string_output = 1
185 string_output = 1
183 elif out is not None:
186 elif out is not None:
184 self.out = out
187 self.out = out
185
188
186 # Fast return of the unmodified input for NoColor scheme
189 # Fast return of the unmodified input for NoColor scheme
187 if scheme == 'NoColor':
190 if scheme == 'NoColor':
188 error = False
191 error = False
189 self.out.write(raw)
192 self.out.write(raw)
190 if string_output:
193 if string_output:
191 return raw,error
194 return raw,error
192 else:
195 else:
193 return None,error
196 return None,error
194
197
195 # local shorthands
198 # local shorthands
196 colors = self.color_table[scheme].colors
199 colors = self.color_table[scheme].colors
197 self.colors = colors # put in object so __call__ sees it
200 self.colors = colors # put in object so __call__ sees it
198
201
199 # Remove trailing whitespace and normalize tabs
202 # Remove trailing whitespace and normalize tabs
200 self.raw = raw.expandtabs().rstrip()
203 self.raw = raw.expandtabs().rstrip()
201
204
202 # store line offsets in self.lines
205 # store line offsets in self.lines
203 self.lines = [0, 0]
206 self.lines = [0, 0]
204 pos = 0
207 pos = 0
205 raw_find = self.raw.find
208 raw_find = self.raw.find
206 lines_append = self.lines.append
209 lines_append = self.lines.append
207 while 1:
210 while 1:
208 pos = raw_find('\n', pos) + 1
211 pos = raw_find('\n', pos) + 1
209 if not pos: break
212 if not pos: break
210 lines_append(pos)
213 lines_append(pos)
211 lines_append(len(self.raw))
214 lines_append(len(self.raw))
212
215
213 # parse the source and write it
216 # parse the source and write it
214 self.pos = 0
217 self.pos = 0
215 text = StringIO(self.raw)
218 text = StringIO(self.raw)
216
219
217 error = False
220 error = False
218 try:
221 try:
219 for atoken in generate_tokens(text.readline):
222 for atoken in generate_tokens(text.readline):
220 self(*atoken)
223 self(*atoken)
221 except tokenize.TokenError as ex:
224 except tokenize.TokenError as ex:
222 msg = ex.args[0]
225 msg = ex.args[0]
223 line = ex.args[1][0]
226 line = ex.args[1][0]
224 self.out.write("%s\n\n*** ERROR: %s%s%s\n" %
227 self.out.write("%s\n\n*** ERROR: %s%s%s\n" %
225 (colors[token.ERRORTOKEN],
228 (colors[token.ERRORTOKEN],
226 msg, self.raw[self.lines[line]:],
229 msg, self.raw[self.lines[line]:],
227 colors.normal)
230 colors.normal)
228 )
231 )
229 error = True
232 error = True
230 self.out.write(colors.normal+'\n')
233 self.out.write(colors.normal+'\n')
231 if string_output:
234 if string_output:
232 output = self.out.getvalue()
235 output = self.out.getvalue()
233 self.out = out_old
236 self.out = out_old
234 return (output, error)
237 return (output, error)
235 return (None, error)
238 return (None, error)
236
239
237 def __call__(self, toktype, toktext, start_pos, end_pos, line):
240 def __call__(self, toktype, toktext, start_pos, end_pos, line):
238 """ Token handler, with syntax highlighting."""
241 """ Token handler, with syntax highlighting."""
239 (srow,scol) = start_pos
242 (srow,scol) = start_pos
240 (erow,ecol) = end_pos
243 (erow,ecol) = end_pos
241 colors = self.colors
244 colors = self.colors
242 owrite = self.out.write
245 owrite = self.out.write
243
246
244 # line separator, so this works across platforms
247 # line separator, so this works across platforms
245 linesep = os.linesep
248 linesep = os.linesep
246
249
247 # calculate new positions
250 # calculate new positions
248 oldpos = self.pos
251 oldpos = self.pos
249 newpos = self.lines[srow] + scol
252 newpos = self.lines[srow] + scol
250 self.pos = newpos + len(toktext)
253 self.pos = newpos + len(toktext)
251
254
252 # send the original whitespace, if needed
255 # send the original whitespace, if needed
253 if newpos > oldpos:
256 if newpos > oldpos:
254 owrite(self.raw[oldpos:newpos])
257 owrite(self.raw[oldpos:newpos])
255
258
256 # skip indenting tokens
259 # skip indenting tokens
257 if toktype in [token.INDENT, token.DEDENT]:
260 if toktype in [token.INDENT, token.DEDENT]:
258 self.pos = newpos
261 self.pos = newpos
259 return
262 return
260
263
261 # map token type to a color group
264 # map token type to a color group
262 if token.LPAR <= toktype <= token.OP:
265 if token.LPAR <= toktype <= token.OP:
263 toktype = token.OP
266 toktype = token.OP
264 elif toktype == token.NAME and keyword.iskeyword(toktext):
267 elif toktype == token.NAME and keyword.iskeyword(toktext):
265 toktype = _KEYWORD
268 toktype = _KEYWORD
266 color = colors.get(toktype, colors[_TEXT])
269 color = colors.get(toktype, colors[_TEXT])
267
270
268 #print '<%s>' % toktext, # dbg
271 #print '<%s>' % toktext, # dbg
269
272
270 # Triple quoted strings must be handled carefully so that backtracking
273 # Triple quoted strings must be handled carefully so that backtracking
271 # in pagers works correctly. We need color terminators on _each_ line.
274 # in pagers works correctly. We need color terminators on _each_ line.
272 if linesep in toktext:
275 if linesep in toktext:
273 toktext = toktext.replace(linesep, '%s%s%s' %
276 toktext = toktext.replace(linesep, '%s%s%s' %
274 (colors.normal,linesep,color))
277 (colors.normal,linesep,color))
275
278
276 # send text
279 # send text
277 owrite('%s%s%s' % (color,toktext,colors.normal))
280 owrite('%s%s%s' % (color,toktext,colors.normal))
278
281
279 def main(argv=None):
282 def main(argv=None):
280 """Run as a command-line script: colorize a python file or stdin using ANSI
283 """Run as a command-line script: colorize a python file or stdin using ANSI
281 color escapes and print to stdout.
284 color escapes and print to stdout.
282
285
283 Inputs:
286 Inputs:
284
287
285 - argv(None): a list of strings like sys.argv[1:] giving the command-line
288 - argv(None): a list of strings like sys.argv[1:] giving the command-line
286 arguments. If None, use sys.argv[1:].
289 arguments. If None, use sys.argv[1:].
287 """
290 """
288
291
289 usage_msg = """%prog [options] [filename]
292 usage_msg = """%prog [options] [filename]
290
293
291 Colorize a python file or stdin using ANSI color escapes and print to stdout.
294 Colorize a python file or stdin using ANSI color escapes and print to stdout.
292 If no filename is given, or if filename is -, read standard input."""
295 If no filename is given, or if filename is -, read standard input."""
293
296
294 import optparse
297 import optparse
295 parser = optparse.OptionParser(usage=usage_msg)
298 parser = optparse.OptionParser(usage=usage_msg)
296 newopt = parser.add_option
299 newopt = parser.add_option
297 newopt('-s','--scheme',metavar='NAME',dest='scheme_name',action='store',
300 newopt('-s','--scheme',metavar='NAME',dest='scheme_name',action='store',
298 choices=['Linux','LightBG','NoColor'],default=_scheme_default,
301 choices=['Linux','LightBG','NoColor'],default=_scheme_default,
299 help="give the color scheme to use. Currently only 'Linux'\
302 help="give the color scheme to use. Currently only 'Linux'\
300 (default) and 'LightBG' and 'NoColor' are implemented (give without\
303 (default) and 'LightBG' and 'NoColor' are implemented (give without\
301 quotes)")
304 quotes)")
302
305
303 opts,args = parser.parse_args(argv)
306 opts,args = parser.parse_args(argv)
304
307
305 if len(args) > 1:
308 if len(args) > 1:
306 parser.error("you must give at most one filename.")
309 parser.error("you must give at most one filename.")
307
310
308 if len(args) == 0:
311 if len(args) == 0:
309 fname = '-' # no filename given; setup to read from stdin
312 fname = '-' # no filename given; setup to read from stdin
310 else:
313 else:
311 fname = args[0]
314 fname = args[0]
312
315
313 if fname == '-':
316 if fname == '-':
314 stream = sys.stdin
317 stream = sys.stdin
315 else:
318 else:
316 try:
319 try:
317 stream = open(fname)
320 stream = open(fname)
318 except IOError as msg:
321 except IOError as msg:
319 print(msg, file=sys.stderr)
322 print(msg, file=sys.stderr)
320 sys.exit(1)
323 sys.exit(1)
321
324
322 parser = Parser()
325 parser = Parser()
323
326
324 # we need nested try blocks because pre-2.5 python doesn't support unified
327 # we need nested try blocks because pre-2.5 python doesn't support unified
325 # try-except-finally
328 # try-except-finally
326 try:
329 try:
327 try:
330 try:
328 # write colorized version to stdout
331 # write colorized version to stdout
329 parser.format(stream.read(),scheme=opts.scheme_name)
332 parser.format(stream.read(),scheme=opts.scheme_name)
330 except IOError as msg:
333 except IOError as msg:
331 # if user reads through a pager and quits, don't print traceback
334 # if user reads through a pager and quits, don't print traceback
332 if msg.args != (32,'Broken pipe'):
335 if msg.args != (32,'Broken pipe'):
333 raise
336 raise
334 finally:
337 finally:
335 if stream is not sys.stdin:
338 if stream is not sys.stdin:
336 stream.close() # in case a non-handled exception happened above
339 stream.close() # in case a non-handled exception happened above
337
340
338 if __name__ == "__main__":
341 if __name__ == "__main__":
339 main()
342 main()
General Comments 0
You need to be logged in to leave comments. Login now