Kaydet (Commit) 8951b617 authored tarafından Benjamin Peterson's avatar Benjamin Peterson

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 #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
  ........
................
üst d8976f12
......@@ -5,8 +5,10 @@
.. sectionauthor:: Benjamin Peterson
2to3 is a Python program that reads your Python 2.x source code and applies a
series of *fixers* to transform it into valid Python 3.x code.
2to3 is a Python program that reads Python 2.x source code and applies a series
of *fixers* to transform it into valid Python 3.x code. The standard library
contains a rich set of fixers that will handle almost all code. It is, however,
possible to write your own fixers.
Using 2to3
......
......@@ -47,8 +47,8 @@ class BaseFix(object):
"""Initializer. Subclass may override.
Args:
options: an optparse.Values instance which can be used
to inspect the command line options.
options: an dict containing the options passed to RefactoringTool
that could be used to customize the fixer through the command line.
log: a list to append warnings and other messages to.
"""
self.options = options
......
"""
Main program for 2to3.
"""
import sys
import os
import logging
import optparse
from . import refactor
def main(fixer_pkg, args=None):
"""Main program.
Args:
fixer_pkg: the name of a package where the fixers are located.
args: optional; a list of command line arguments. If omitted,
sys.argv[1:] is used.
Returns a suggested exit status (0, 1, 2).
"""
# Set up option parser
parser = optparse.OptionParser(usage="refactor.py [options] file|dir ...")
parser.add_option("-d", "--doctests_only", action="store_true",
help="Fix up doctests only")
parser.add_option("-f", "--fix", action="append", default=[],
help="Each FIX specifies a transformation; default all")
parser.add_option("-l", "--list-fixes", action="store_true",
help="List available transformations (fixes/fix_*.py)")
parser.add_option("-p", "--print-function", action="store_true",
help="Modify the grammar so that print() is a function")
parser.add_option("-v", "--verbose", action="store_true",
help="More verbose logging")
parser.add_option("-w", "--write", action="store_true",
help="Write back modified files")
# Parse command line arguments
refactor_stdin = False
options, args = parser.parse_args(args)
if options.list_fixes:
print "Available transformations for the -f/--fix option:"
for fixname in refactor.get_all_fix_names(fixer_pkg):
print fixname
if not args:
return 0
if not args:
print >>sys.stderr, "At least one file or directory argument required."
print >>sys.stderr, "Use --help to show usage."
return 2
if "-" in args:
refactor_stdin = True
if options.write:
print >>sys.stderr, "Can't write to stdin."
return 2
# Set up logging handler
level = logging.DEBUG if options.verbose else logging.INFO
logging.basicConfig(format='%(name)s: %(message)s', level=level)
# Initialize the refactoring tool
rt_opts = {"print_function" : options.print_function}
avail_names = refactor.get_fixers_from_package(fixer_pkg)
explicit = []
if options.fix:
explicit = [fixer_pkg + ".fix_" + fix
for fix in options.fix if fix != "all"]
fixer_names = avail_names if "all" in options.fix else explicit
else:
fixer_names = avail_names
rt = refactor.RefactoringTool(fixer_names, rt_opts, explicit=explicit)
# Refactor all files and directories passed as arguments
if not rt.errors:
if refactor_stdin:
rt.refactor_stdin()
else:
rt.refactor(args, options.write, options.doctests_only)
rt.summarize()
# Return error status (0 if rt.errors is zero)
return int(bool(rt.errors))
if __name__ == "__main__":
sys.exit(main())
This diff is collapsed.
......@@ -13,6 +13,7 @@ from textwrap import dedent
# Local imports
from .. import pytree
from .. import refactor
from ..pgen2 import driver
test_dir = os.path.dirname(__file__)
......@@ -38,6 +39,21 @@ def run_all_tests(test_mod=None, tests=None):
def reformat(string):
return dedent(string) + "\n\n"
def get_refactorer(fixers=None, options=None):
"""
A convenience function for creating a RefactoringTool for tests.
fixers is a list of fixers for the RefactoringTool to use. By default
"lib2to3.fixes.*" is used. options is an optional dictionary of options to
be passed to the RefactoringTool.
"""
if fixers is not None:
fixers = ["lib2to3.fixes.fix_" + fix for fix in fixers]
else:
fixers = refactor.get_fixers_from_package("lib2to3.fixes")
options = options or {}
return refactor.RefactoringTool(fixers, options, explicit=True)
def all_project_files():
for dirpath, dirnames, filenames in os.walk(proj_dir):
for filename in filenames:
......
......@@ -19,17 +19,11 @@ import unittest
from .. import pytree
from .. import refactor
class Options:
def __init__(self, **kwargs):
for k, v in list(kwargs.items()):
setattr(self, k, v)
self.verbose = False
class Test_all(support.TestCase):
def setUp(self):
options = Options(fix=["all", "idioms", "ws_comma", "buffer"],
print_function=False)
self.refactor = refactor.RefactoringTool("lib2to3/fixes", options)
options = {"print_function" : False}
self.refactor = support.get_refactorer(options=options)
def test_all_project_files(self):
for filepath in support.all_project_files():
......
......@@ -21,19 +21,12 @@ from .. import refactor
from .. import fixer_util
class Options:
def __init__(self, **kwargs):
for k, v in list(kwargs.items()):
setattr(self, k, v)
self.verbose = False
class FixerTestCase(support.TestCase):
def setUp(self, fix_list=None):
if not fix_list:
if fix_list is None:
fix_list = [self.fixer]
options = Options(fix=fix_list, print_function=False)
self.refactor = refactor.RefactoringTool("lib2to3/fixes", options)
options = {"print_function" : False}
self.refactor = support.get_refactorer(fix_list, options)
self.fixer_log = []
self.filename = "<string>"
......@@ -70,10 +63,10 @@ class FixerTestCase(support.TestCase):
self.failUnlessEqual(self.fixer_log, [])
def assert_runs_after(self, *names):
fix = [self.fixer]
fix.extend(names)
options = Options(fix=fix, print_function=False)
r = refactor.RefactoringTool("lib2to3/fixes", options)
fixes = [self.fixer]
fixes.extend(names)
options = {"print_function" : False}
r = support.get_refactorer(fixes, options)
(pre, post) = r.get_fixers()
n = "fix_" + self.fixer
if post and post[-1].__class__.__module__.endswith(n):
......
#!/usr/bin/env python
from lib2to3 import refactor
from lib2to3.main import main
import sys
import os
fixers = os.path.join(os.path.dirname(refactor.__file__), "fixes")
sys.exit(refactor.main(fixers))
sys.exit(main("lib2to3.fixes"))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment