Skip to content

Commit 63c0638

Browse files
authored
Merge pull request #167 from shaib/load-user-fixers
Support user-provided fixers (fixes #166)
2 parents 6f151f5 + 85dbc62 commit 63c0638

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

docs/fixers.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ then use the ``--no-six`` flag.
2121
Fixers use the API defined by 2to3. For details of how this works, and how to
2222
implement your own fixers, see `Extending 2to3 with your own fixers, at
2323
python3porting.com <http://python3porting.com/fixers.html>`_.
24+
``python-modernize`` will try to load fixers whose full dotted-path is specified
25+
as a ``-f`` argument, but will fail if they are not found. By default, fixers
26+
will not be found in the current directory; use ``--fixers-here`` to make
27+
``python-modernize`` look for them there, or see the `Python tutorial on
28+
modules <https://docs.python.org/3/tutorial/modules.html>`_ (in particular,
29+
the parts on the `search path
30+
<https://docs.python.org/3/tutorial/modules.html#the-module-search-path>`_
31+
and `packages <https://docs.python.org/3/tutorial/modules.html#packages>`_)
32+
for more info on how Python finds modules.
2433

2534

2635
Default

libmodernize/main.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import sys
1111
import logging
1212
import optparse
13+
import os
1314

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

27+
2628
def format_usage(usage):
2729
"""Method that doesn't output "Usage:" prefix"""
2830
return usage
2931

32+
3033
def main(args=None):
3134
"""Main program.
3235
@@ -41,11 +44,13 @@ def main(args=None):
4144
parser.add_option("--no-diffs", action="store_true",
4245
help="Don't show diffs of the refactoring.")
4346
parser.add_option("-l", "--list-fixes", action="store_true",
44-
help="List available transformations.")
47+
help="List standard transformations.")
4548
parser.add_option("-d", "--doctests_only", action="store_true",
4649
help="Fix up doctests only.")
4750
parser.add_option("-f", "--fix", action="append", default=[],
4851
help="Each FIX specifies a transformation; '-f default' includes default fixers.")
52+
parser.add_option("--fixers-here", action="store_true",
53+
help="Add current working directory to python path (so fixers can be found)")
4954
parser.add_option("-j", "--processes", action="store", default=1,
5055
type="int", help="Run 2to3 concurrently.")
5156
parser.add_option("-x", "--nofix", action="append", default=[],
@@ -80,7 +85,7 @@ def main(args=None):
8085
if not options.write and options.nobackups:
8186
parser.error("Can't use '-n' without '-w'.")
8287
if options.list_fixes:
83-
print("Available transformations for the -f/--fix and -x/--nofix options:")
88+
print("Standard transformations available for the -f/--fix and -x/--nofix options:")
8489
for fixname in sorted(avail_fixes):
8590
print(" {} ({})".format(fixname, fixname.split(".fix_", 1)[1]))
8691
print()
@@ -97,6 +102,8 @@ def main(args=None):
97102
return 2
98103
if options.print_function:
99104
flags["print_function"] = True
105+
if options.fixers_here:
106+
sys.path.append(os.getcwd())
100107

101108
# Set up logging handler
102109
level = logging.DEBUG if options.verbose else logging.INFO
@@ -113,7 +120,8 @@ def main(args=None):
113120
if tgt == fix or tgt.endswith(".fix_{}".format(fix)):
114121
matched = tgt
115122
unwanted_fixes.add(matched)
116-
if matched is None:
123+
break
124+
else:
117125
print("Error: fix '{}' was not found".format(fix),
118126
file=sys.stderr)
119127
return 2
@@ -147,10 +155,10 @@ def main(args=None):
147155
if tgt == fix or tgt.endswith(".fix_{}".format(fix)):
148156
matched = tgt
149157
explicit.add(matched)
150-
if matched is None:
151-
print("Error: fix '{}' was not found".format(fix),
152-
file=sys.stderr)
153-
return 2
158+
break
159+
else:
160+
# A non-standard fix -- trust user to have supplied path
161+
explicit.add(fix)
154162
requested = default_fixes.union(explicit) if default_present else explicit
155163
else:
156164
requested = default_fixes

0 commit comments

Comments
 (0)