##// END OF EJS Templates
Paul Mueller: switch pycolor to use optparse
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 2274 2007-04-26 14:41:43Z jdh2358 $"""
31 $Id: PyColorize.py 2341 2007-05-15 14:44:30Z vivainio $"""
32
32
33 __all__ = ['ANSICodeColors','Parser']
33 __all__ = ['ANSICodeColors','Parser']
34
34
@@ -38,6 +38,7 b" _scheme_default = 'Linux'"
38 import cStringIO
38 import cStringIO
39 import keyword
39 import keyword
40 import os
40 import os
41 import optparse
41 import string
42 import string
42 import sys
43 import sys
43 import token
44 import token
@@ -242,42 +243,38 b' class Parser:'
242 # send text
243 # send text
243 owrite('%s%s%s' % (color,toktext,colors.normal))
244 owrite('%s%s%s' % (color,toktext,colors.normal))
244
245
245 def main():
246 def main(argv=None):
246 """Colorize a python file using ANSI color escapes and print to stdout.
247 """Run as a command-line script: colorize a python file using ANSI color
248 escapes and print to stdout.
247
249
248 Usage:
250 Inputs:
249 %s [-s scheme] filename
250
251
251 Options:
252 - argv(None): a list of strings like sys.argv[1:] giving the command-line
253 arguments. If None, use sys.argv[1:].
254 """
252
255
253 -s scheme: give the color scheme to use. Currently only 'Linux'
256 usage_msg = """%prog [options] filename
254 (default) and 'LightBG' and 'NoColor' are implemented (give without
255 quotes). """
256
257
257 def usage():
258 Colorize a python file using ANSI color escapes and print to stdout."""
258 print >> sys.stderr, main.__doc__ % sys.argv[0]
259 sys.exit(1)
260
259
261 # FIXME: rewrite this to at least use getopt
260 parser = optparse.OptionParser(usage=usage_msg)
262 try:
261 newopt = parser.add_option
263 if sys.argv[1] == '-s':
262 newopt('-s','--scheme',metavar='NAME',dest='scheme_name',action='store',
264 scheme_name = sys.argv[2]
263 choices=['Linux','LightBG','NoColor'],default=_scheme_default,
265 del sys.argv[1:3]
264 help="give the color scheme to use. Currently only 'Linux'\
266 else:
265 (default) and 'LightBG' and 'NoColor' are implemented (give without\
267 scheme_name = _scheme_default
266 quotes)")
268
267
269 except:
268 opts,args = parser.parse_args(argv)
270 usage()
271
269
272 try:
270 if len(args) != 1:
273 fname = sys.argv[1]
271 parser.error("you must give one filename.")
274 except:
275 usage()
276
272
277 # write colorized version to stdout
273 # write colorized version to stdout
274 fname = args[0]
278 parser = Parser()
275 parser = Parser()
279 try:
276 try:
280 parser.format(file(fname).read(),scheme = scheme_name)
277 parser.format(file(fname).read(),scheme=opts.scheme_name)
281 except IOError,msg:
278 except IOError,msg:
282 # if user reads through a pager and quits, don't print traceback
279 # if user reads through a pager and quits, don't print traceback
283 if msg.args != (32,'Broken pipe'):
280 if msg.args != (32,'Broken pipe'):
@@ -6,7 +6,7 b' Requires Python 2.3 or newer.'
6
6
7 This file contains all the classes and helper functions specific to IPython.
7 This file contains all the classes and helper functions specific to IPython.
8
8
9 $Id: iplib.py 2297 2007-04-30 11:08:46Z vivainio $
9 $Id: iplib.py 2341 2007-05-15 14:44:30Z vivainio $
10 """
10 """
11
11
12 #*****************************************************************************
12 #*****************************************************************************
@@ -1288,6 +1288,9 b' want to merge them back into the new files.""" % locals()'
1288 Currently it handles auto-indent only."""
1288 Currently it handles auto-indent only."""
1289
1289
1290 #debugx('self.indent_current_nsp','pre_readline:')
1290 #debugx('self.indent_current_nsp','pre_readline:')
1291 print "prer",self.indent_current_nsp # dbg
1292 print
1293
1291 self.readline.insert_text(self.indent_current_str())
1294 self.readline.insert_text(self.indent_current_str())
1292
1295
1293 def init_readline(self):
1296 def init_readline(self):
@@ -1806,6 +1809,7 b' want to merge them back into the new files.""" % locals()'
1806 #debugx('line')
1809 #debugx('line')
1807 #debugx('self.indent_current_nsp')
1810 #debugx('self.indent_current_nsp')
1808 if self.autoindent:
1811 if self.autoindent:
1812 print "ind level", self.indent_current_nsp #dbg
1809 if line:
1813 if line:
1810 inisp = num_ini_spaces(line)
1814 inisp = num_ini_spaces(line)
1811 if inisp < self.indent_current_nsp:
1815 if inisp < self.indent_current_nsp:
@@ -2,7 +2,7 b''
2 .\" First parameter, NAME, should be all caps
2 .\" First parameter, NAME, should be all caps
3 .\" Second parameter, SECTION, should be 1-8, maybe w/ subsection
3 .\" Second parameter, SECTION, should be 1-8, maybe w/ subsection
4 .\" other parameters are allowed: see man(7), man(1)
4 .\" other parameters are allowed: see man(7), man(1)
5 .TH PYCOLOR 1 "March 21, 2003"
5 .TH PYCOLOR 1 "May 12, 2007"
6 .\" Please adjust this date whenever revising the manpage.
6 .\" Please adjust this date whenever revising the manpage.
7 .\"
7 .\"
8 .\" Some roff macros, for reference:
8 .\" Some roff macros, for reference:
@@ -24,7 +24,10 b' pycolor \\- Colorize a python file using ANSI and print to stdout.'
24 Prints a colorized version of the input file to standard out.
24 Prints a colorized version of the input file to standard out.
25 .SH OPTIONS
25 .SH OPTIONS
26 .TP
26 .TP
27 .B \-s <scheme>
27 .B \-h, \-\-help
28 Output a brief help message.
29 .TP
30 .B \-s, \-\-scheme <scheme>
28 Give the color scheme to use. Currently only Linux (default),
31 Give the color scheme to use. Currently only Linux (default),
29 LightBG, and NOColor are implemented.
32 LightBG, and NOColor are implemented.
30 .SH AUTHOR
33 .SH AUTHOR
General Comments 0
You need to be logged in to leave comments. Login now