Skip to content

Commit

Permalink
Support user-provided fixers
Browse files Browse the repository at this point in the history
Avoid erroring out when a non-standard fixer is specified, and instead
hand it over to the refactoring tool so it can try to load it.

While at it, add a command-line option to add the current directory
to sys.path, to make it easier for users to let their fixers be found.
  • Loading branch information
shaib committed Apr 30, 2018
1 parent 8be1a59 commit bc95172
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
6 changes: 6 additions & 0 deletions docs/fixers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ then use the ``--no-six`` flag.
Fixers use the API defined by 2to3. For details of how this works, and how to
implement your own fixers, see `Extending 2to3 with your own fixers, at
python3porting.com <http://python3porting.com/fixers.html>`_.
``python-modernize`` will try to load fixers whose full dotted-path is specified
as a ``-f`` argument, but will fail if they are not found. Note that, by
default, the current directory is not on the Python-Path when you run the
command; the ``-H``/``--fixers-here`` option is provided in order to add it,
as a shorthand for explicit manipulation of the ``PYTHONPATH`` environment
variable.


Default
Expand Down
16 changes: 11 additions & 5 deletions libmodernize/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sys
import logging
import optparse
import os

from lib2to3.main import warn, StdoutRefactoringTool
from lib2to3 import refactor
Expand All @@ -23,10 +24,12 @@
Usage: modernize [options] file|dir ...
""" % __version__


def format_usage(usage):
"""Method that doesn't output "Usage:" prefix"""
return usage


def main(args=None):
"""Main program.
Expand All @@ -41,11 +44,13 @@ def main(args=None):
parser.add_option("--no-diffs", action="store_true",
help="Don't show diffs of the refactoring.")
parser.add_option("-l", "--list-fixes", action="store_true",
help="List available transformations.")
help="List standard transformations.")
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; '-f default' includes default fixers.")
parser.add_option("-H", "--fixers-here", action="store_true",
help="Add current working directory to python path (so fixers can be found)")
parser.add_option("-j", "--processes", action="store", default=1,
type="int", help="Run 2to3 concurrently.")
parser.add_option("-x", "--nofix", action="append", default=[],
Expand Down Expand Up @@ -80,7 +85,7 @@ def main(args=None):
if not options.write and options.nobackups:
parser.error("Can't use '-n' without '-w'.")
if options.list_fixes:
print("Available transformations for the -f/--fix and -x/--nofix options:")
print("Standard transformations available for the -f/--fix and -x/--nofix options:")
for fixname in sorted(avail_fixes):
print(" {} ({})".format(fixname, fixname.split(".fix_", 1)[1]))
print()
Expand All @@ -97,6 +102,8 @@ def main(args=None):
return 2
if options.print_function:
flags["print_function"] = True
if options.fixers_here:
sys.path.append(os.getcwd())

# Set up logging handler
level = logging.DEBUG if options.verbose else logging.INFO
Expand Down Expand Up @@ -150,9 +157,8 @@ def main(args=None):
explicit.add(matched)
break
else:
print("Error: fix '{}' was not found".format(fix),
file=sys.stderr)
return 2
# A non-standard fix -- trust user to have supplied path
explicit.add(fix)
requested = default_fixes.union(explicit) if default_present else explicit
else:
requested = default_fixes
Expand Down

0 comments on commit bc95172

Please sign in to comment.