Skip to content

Commit

Permalink
Merge pull request #167 from shaib/load-user-fixers
Browse files Browse the repository at this point in the history
Support user-provided fixers (fixes #166)
  • Loading branch information
takluyver authored May 6, 2018
2 parents 6f151f5 + 85dbc62 commit 63c0638
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
9 changes: 9 additions & 0 deletions docs/fixers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ 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. By default, fixers
will not be found in the current directory; use ``--fixers-here`` to make
``python-modernize`` look for them there, or see the `Python tutorial on
modules <https://docs.python.org/3/tutorial/modules.html>`_ (in particular,
the parts on the `search path
<https://docs.python.org/3/tutorial/modules.html#the-module-search-path>`_
and `packages <https://docs.python.org/3/tutorial/modules.html#packages>`_)
for more info on how Python finds modules.


Default
Expand Down
22 changes: 15 additions & 7 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("--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 All @@ -113,7 +120,8 @@ def main(args=None):
if tgt == fix or tgt.endswith(".fix_{}".format(fix)):
matched = tgt
unwanted_fixes.add(matched)
if matched is None:
break
else:
print("Error: fix '{}' was not found".format(fix),
file=sys.stderr)
return 2
Expand Down Expand Up @@ -147,10 +155,10 @@ def main(args=None):
if tgt == fix or tgt.endswith(".fix_{}".format(fix)):
matched = tgt
explicit.add(matched)
if matched is None:
print("Error: fix '{}' was not found".format(fix),
file=sys.stderr)
return 2
break
else:
# 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 63c0638

Please sign in to comment.