##// END OF EJS Templates
Paul Mueller: pycolorize considers 'no arguments' as 'read data from stdin'"
vivainio -
Show More
@@ -28,7 +28,7 b''
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 $Id: PyColorize.py 2341 2007-05-15 14:44:30Z vivainio $"""
31 $Id: PyColorize.py 2342 2007-05-15 14:46:58Z vivainio $"""
32
32
33 __all__ = ['ANSICodeColors','Parser']
33 __all__ = ['ANSICodeColors','Parser']
34
34
@@ -244,8 +244,8 b' class Parser:'
244 owrite('%s%s%s' % (color,toktext,colors.normal))
244 owrite('%s%s%s' % (color,toktext,colors.normal))
245
245
246 def main(argv=None):
246 def main(argv=None):
247 """Run as a command-line script: colorize a python file using ANSI color
247 """Run as a command-line script: colorize a python file or stdin using ANSI
248 escapes and print to stdout.
248 color escapes and print to stdout.
249
249
250 Inputs:
250 Inputs:
251
251
@@ -253,9 +253,10 b' def main(argv=None):'
253 arguments. If None, use sys.argv[1:].
253 arguments. If None, use sys.argv[1:].
254 """
254 """
255
255
256 usage_msg = """%prog [options] filename
256 usage_msg = """%prog [options] [filename]
257
257
258 Colorize a python file using ANSI color escapes and print to stdout."""
258 Colorize a python file or stdin using ANSI color escapes and print to stdout.
259 If no filename is given, or if filename is -, read standard input."""
259
260
260 parser = optparse.OptionParser(usage=usage_msg)
261 parser = optparse.OptionParser(usage=usage_msg)
261 newopt = parser.add_option
262 newopt = parser.add_option
@@ -267,18 +268,34 b' Colorize a python file using ANSI color escapes and print to stdout."""'
267
268
268 opts,args = parser.parse_args(argv)
269 opts,args = parser.parse_args(argv)
269
270
270 if len(args) != 1:
271 if len(args) > 1:
271 parser.error("you must give one filename.")
272 parser.error("you must give at most one filename.")
273
274 if len(args) == 0:
275 fname = '-' # no filename given; setup to read from stdin
276 else:
277 fname = args[0]
278
279 if fname == '-':
280 stream = sys.stdin
281 else:
282 stream = file(fname)
272
283
273 # write colorized version to stdout
274 fname = args[0]
275 parser = Parser()
284 parser = Parser()
285
286 # we need nested try blocks because pre-2.5 python doesn't support unified
287 # try-except-finally
276 try:
288 try:
277 parser.format(file(fname).read(),scheme=opts.scheme_name)
289 try:
278 except IOError,msg:
290 # write colorized version to stdout
279 # if user reads through a pager and quits, don't print traceback
291 parser.format(stream.read(),scheme=opts.scheme_name)
280 if msg.args != (32,'Broken pipe'):
292 except IOError,msg:
281 raise
293 # if user reads through a pager and quits, don't print traceback
294 if msg.args != (32,'Broken pipe'):
295 raise
296 finally:
297 if stream is not sys.stdin:
298 stream.close() # in case a non-handled exception happened above
282
299
283 if __name__ == "__main__":
300 if __name__ == "__main__":
284 main()
301 main()
@@ -16,12 +16,14 b''
16 .\" .sp <n> insert n+1 empty lines
16 .\" .sp <n> insert n+1 empty lines
17 .\" for manpage-specific macros, see man(7)
17 .\" for manpage-specific macros, see man(7)
18 .SH NAME
18 .SH NAME
19 pycolor \- Colorize a python file using ANSI and print to stdout.
19 pycolor \- Colorize a python file or stdin using ANSI and print to stdout.
20 .SH SYNOPSIS
20 .SH SYNOPSIS
21 .B pycolor
21 .B pycolor
22 .RI [ options ] " file"
22 .RI [ options ]
23 .RI [ file ]
23 .SH DESCRIPTION
24 .SH DESCRIPTION
24 Prints a colorized version of the input file to standard out.
25 Prints a colorized version of the input file (or standard input if no file is
26 given, or the file name - is given) to standard out.
25 .SH OPTIONS
27 .SH OPTIONS
26 .TP
28 .TP
27 .B \-h, \-\-help
29 .B \-h, \-\-help
General Comments 0
You need to be logged in to leave comments. Login now