Skip to content

Commit 8951b61

Browse files
committed
Merged revisions 66174-66175,66177 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ................ r66174 | benjamin.peterson | 2008-09-02 19:21:32 -0500 (Tue, 02 Sep 2008) | 15 lines Merged revisions 66173 via svnmerge from svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3 ........ r66173 | benjamin.peterson | 2008-09-02 18:57:48 -0500 (Tue, 02 Sep 2008) | 8 lines A little 2to3 refactoring python#3637 This moves command line logic from refactor.py to a new file called main.py. RefactoringTool now merely deals with the actual fixers and refactoring; options processing for example is abstracted out. This patch was reviewed by Gregory P. Smith. ........ ................ r66175 | benjamin.peterson | 2008-09-02 20:53:28 -0500 (Tue, 02 Sep 2008) | 1 line update 2to3 script from 2to3 trunk ................ r66177 | benjamin.peterson | 2008-09-02 21:14:03 -0500 (Tue, 02 Sep 2008) | 9 lines Merged revisions 66176 via svnmerge from svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3 ........ r66176 | benjamin.peterson | 2008-09-02 21:04:06 -0500 (Tue, 02 Sep 2008) | 1 line fix typo ........ ................
1 parent d8976f1 commit 8951b61

8 files changed

Lines changed: 216 additions & 171 deletions

File tree

Doc/library/2to3.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
.. sectionauthor:: Benjamin Peterson
77

8-
2to3 is a Python program that reads your Python 2.x source code and applies a
9-
series of *fixers* to transform it into valid Python 3.x code.
8+
2to3 is a Python program that reads Python 2.x source code and applies a series
9+
of *fixers* to transform it into valid Python 3.x code. The standard library
10+
contains a rich set of fixers that will handle almost all code. It is, however,
11+
possible to write your own fixers.
1012

1113

1214
Using 2to3

Lib/lib2to3/fixer_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ def __init__(self, options, log):
4747
"""Initializer. Subclass may override.
4848
4949
Args:
50-
options: an optparse.Values instance which can be used
51-
to inspect the command line options.
50+
options: an dict containing the options passed to RefactoringTool
51+
that could be used to customize the fixer through the command line.
5252
log: a list to append warnings and other messages to.
5353
"""
5454
self.options = options

Lib/lib2to3/main.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"""
2+
Main program for 2to3.
3+
"""
4+
5+
import sys
6+
import os
7+
import logging
8+
import optparse
9+
10+
from . import refactor
11+
12+
13+
def main(fixer_pkg, args=None):
14+
"""Main program.
15+
16+
Args:
17+
fixer_pkg: the name of a package where the fixers are located.
18+
args: optional; a list of command line arguments. If omitted,
19+
sys.argv[1:] is used.
20+
21+
Returns a suggested exit status (0, 1, 2).
22+
"""
23+
# Set up option parser
24+
parser = optparse.OptionParser(usage="refactor.py [options] file|dir ...")
25+
parser.add_option("-d", "--doctests_only", action="store_true",
26+
help="Fix up doctests only")
27+
parser.add_option("-f", "--fix", action="append", default=[],
28+
help="Each FIX specifies a transformation; default all")
29+
parser.add_option("-l", "--list-fixes", action="store_true",
30+
help="List available transformations (fixes/fix_*.py)")
31+
parser.add_option("-p", "--print-function", action="store_true",
32+
help="Modify the grammar so that print() is a function")
33+
parser.add_option("-v", "--verbose", action="store_true",
34+
help="More verbose logging")
35+
parser.add_option("-w", "--write", action="store_true",
36+
help="Write back modified files")
37+
38+
# Parse command line arguments
39+
refactor_stdin = False
40+
options, args = parser.parse_args(args)
41+
if options.list_fixes:
42+
print "Available transformations for the -f/--fix option:"
43+
for fixname in refactor.get_all_fix_names(fixer_pkg):
44+
print fixname
45+
if not args:
46+
return 0
47+
if not args:
48+
print >>sys.stderr, "At least one file or directory argument required."
49+
print >>sys.stderr, "Use --help to show usage."
50+
return 2
51+
if "-" in args:
52+
refactor_stdin = True
53+
if options.write:
54+
print >>sys.stderr, "Can't write to stdin."
55+
return 2
56+
57+
# Set up logging handler
58+
level = logging.DEBUG if options.verbose else logging.INFO
59+
logging.basicConfig(format='%(name)s: %(message)s', level=level)
60+
61+
# Initialize the refactoring tool
62+
rt_opts = {"print_function" : options.print_function}
63+
avail_names = refactor.get_fixers_from_package(fixer_pkg)
64+
explicit = []
65+
if options.fix:
66+
explicit = [fixer_pkg + ".fix_" + fix
67+
for fix in options.fix if fix != "all"]
68+
fixer_names = avail_names if "all" in options.fix else explicit
69+
else:
70+
fixer_names = avail_names
71+
rt = refactor.RefactoringTool(fixer_names, rt_opts, explicit=explicit)
72+
73+
# Refactor all files and directories passed as arguments
74+
if not rt.errors:
75+
if refactor_stdin:
76+
rt.refactor_stdin()
77+
else:
78+
rt.refactor(args, options.write, options.doctests_only)
79+
rt.summarize()
80+
81+
# Return error status (0 if rt.errors is zero)
82+
return int(bool(rt.errors))
83+
84+
85+
if __name__ == "__main__":
86+
sys.exit(main())

0 commit comments

Comments
 (0)