Skip to content

Commit 6e4ad71

Browse files
authored
Merge pull request #389 from jparise/pasteurize-fixer-names
Port futurize's fix selection logic to pasteurize
2 parents 7cd06c6 + 87bcb83 commit 6e4ad71

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

src/libpasteurize/main.py

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,27 @@ def main(args=None):
114114
level = logging.DEBUG if options.verbose else logging.INFO
115115
logging.basicConfig(format='%(name)s: %(message)s', level=level)
116116

117-
# Initialize the refactoring tool
118-
unwanted_fixes = set(fixer_pkg + ".fix_" + fix for fix in options.nofix)
117+
unwanted_fixes = set()
118+
for fix in options.nofix:
119+
if ".fix_" in fix:
120+
unwanted_fixes.add(fix)
121+
else:
122+
# Infer the full module name for the fixer.
123+
# First ensure that no names clash (e.g.
124+
# lib2to3.fixes.fix_blah and libfuturize.fixes.fix_blah):
125+
found = [f for f in avail_fixes
126+
if f.endswith('fix_{0}'.format(fix))]
127+
if len(found) > 1:
128+
print("Ambiguous fixer name. Choose a fully qualified "
129+
"module name instead from these:\n" +
130+
"\n".join(" " + myf for myf in found),
131+
file=sys.stderr)
132+
return 2
133+
elif len(found) == 0:
134+
print("Unknown fixer. Use --list-fixes or -l for a list.",
135+
file=sys.stderr)
136+
return 2
137+
unwanted_fixes.add(found[0])
119138

120139
extra_fixes = set()
121140
if options.all_imports:
@@ -124,8 +143,45 @@ def main(args=None):
124143
extra_fixes.add(prefix + 'fix_add_future_standard_library_import')
125144
extra_fixes.add(prefix + 'fix_add_all_future_builtins')
126145

127-
fixer_names = avail_fixes | extra_fixes - unwanted_fixes
146+
explicit = set()
147+
if options.fix:
148+
all_present = False
149+
for fix in options.fix:
150+
if fix == 'all':
151+
all_present = True
152+
else:
153+
if ".fix_" in fix:
154+
explicit.add(fix)
155+
else:
156+
# Infer the full module name for the fixer.
157+
# First ensure that no names clash (e.g.
158+
# lib2to3.fixes.fix_blah and libpasteurize.fixes.fix_blah):
159+
found = [f for f in avail_fixes
160+
if f.endswith('fix_{0}'.format(fix))]
161+
if len(found) > 1:
162+
print("Ambiguous fixer name. Choose a fully qualified "
163+
"module name instead from these:\n" +
164+
"\n".join(" " + myf for myf in found),
165+
file=sys.stderr)
166+
return 2
167+
elif len(found) == 0:
168+
print("Unknown fixer. Use --list-fixes or -l for a list.",
169+
file=sys.stderr)
170+
return 2
171+
explicit.add(found[0])
172+
if len(explicit & unwanted_fixes) > 0:
173+
print("Conflicting usage: the following fixers have been "
174+
"simultaneously requested and disallowed:\n" +
175+
"\n".join(" " + myf for myf in (explicit & unwanted_fixes)),
176+
file=sys.stderr)
177+
return 2
178+
requested = avail_fixes.union(explicit) if all_present else explicit
179+
else:
180+
requested = avail_fixes.union(explicit)
128181

182+
fixer_names = requested | extra_fixes - unwanted_fixes
183+
184+
# Initialize the refactoring tool
129185
rt = StdoutRefactoringTool(sorted(fixer_names), flags, set(),
130186
options.nobackups, not options.no_diffs)
131187

0 commit comments

Comments
 (0)