Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d61de7f

Browse files
committed
Merged revisions 66653-66654 via svnmerge from
svn+ssh://[email protected]/python/trunk ................ r66653 | benjamin.peterson | 2008-09-27 16:09:10 -0500 (Sat, 27 Sep 2008) | 49 lines Merged revisions 66511,66548-66549,66644,66646-66652 via svnmerge from svn+ssh://[email protected]/sandbox/trunk/2to3/lib2to3 ........ r66511 | benjamin.peterson | 2008-09-18 21:49:27 -0500 (Thu, 18 Sep 2008) | 1 line remove a useless if __name__ == '__main__' ........ r66548 | benjamin.peterson | 2008-09-21 21:14:14 -0500 (Sun, 21 Sep 2008) | 1 line avoid the perils of mutable default arguments ........ r66549 | benjamin.peterson | 2008-09-21 21:26:11 -0500 (Sun, 21 Sep 2008) | 1 line some places in RefactoringTool should raise an error instead of logging it ........ r66644 | benjamin.peterson | 2008-09-27 10:45:10 -0500 (Sat, 27 Sep 2008) | 1 line fix doctest refactoring ........ r66646 | benjamin.peterson | 2008-09-27 11:40:13 -0500 (Sat, 27 Sep 2008) | 1 line don't print to stdout when 2to3 is used as a library ........ r66647 | benjamin.peterson | 2008-09-27 12:28:28 -0500 (Sat, 27 Sep 2008) | 1 line let fixer modules and classes have different prefixes ........ r66648 | benjamin.peterson | 2008-09-27 14:02:13 -0500 (Sat, 27 Sep 2008) | 1 line raise errors when 2to3 is used as a library ........ r66649 | benjamin.peterson | 2008-09-27 14:03:38 -0500 (Sat, 27 Sep 2008) | 1 line fix docstring ........ r66650 | benjamin.peterson | 2008-09-27 14:22:21 -0500 (Sat, 27 Sep 2008) | 1 line make use of enumerate ........ r66651 | benjamin.peterson | 2008-09-27 14:24:13 -0500 (Sat, 27 Sep 2008) | 1 line revert last revision; it breaks things ........ r66652 | benjamin.peterson | 2008-09-27 16:03:06 -0500 (Sat, 27 Sep 2008) | 1 line add tests for lib2to3.refactor ........ ................ r66654 | benjamin.peterson | 2008-09-27 16:12:20 -0500 (Sat, 27 Sep 2008) | 1 line enable refactor tests ................
1 parent 027951f commit d61de7f

13 files changed

Lines changed: 269 additions & 42 deletions

File tree

Lib/lib2to3/main.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010
from . import refactor
1111

1212

13+
class StdoutRefactoringTool(refactor.RefactoringTool):
14+
"""
15+
Prints output to stdout.
16+
"""
17+
18+
def log_error(self, msg, *args, **kwargs):
19+
self.errors.append((msg, args, kwargs))
20+
self.logger.error(msg, *args, **kwargs)
21+
22+
def print_output(self, lines):
23+
for line in lines:
24+
print(line)
25+
26+
1327
def main(fixer_pkg, args=None):
1428
"""Main program.
1529
@@ -68,7 +82,7 @@ def main(fixer_pkg, args=None):
6882
fixer_names = avail_names if "all" in options.fix else explicit
6983
else:
7084
fixer_names = avail_names
71-
rt = refactor.RefactoringTool(fixer_names, rt_opts, explicit=explicit)
85+
rt = StdoutRefactoringTool(fixer_names, rt_opts, explicit=explicit)
7286

7387
# Refactor all files and directories passed as arguments
7488
if not rt.errors:
@@ -80,7 +94,3 @@ def main(fixer_pkg, args=None):
8094

8195
# Return error status (0 if rt.errors is zero)
8296
return int(bool(rt.errors))
83-
84-
85-
if __name__ == "__main__":
86-
sys.exit(main())

Lib/lib2to3/refactor.py

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,18 @@ def get_fixers_from_package(pkg_name):
9090
for fix_name in get_all_fix_names(pkg_name, False)]
9191

9292

93+
class FixerError(Exception):
94+
"""A fixer could not be loaded."""
95+
96+
9397
class RefactoringTool(object):
9498

9599
_default_options = {"print_function": False}
96100

97-
def __init__(self, fixer_names, options=None, explicit=[]):
101+
CLASS_PREFIX = "Fix" # The prefix for fixer classes
102+
FILE_PREFIX = "fix_" # The prefix for modules with a fixer within
103+
104+
def __init__(self, fixer_names, options=None, explicit=None):
98105
"""Initializer.
99106
100107
Args:
@@ -103,7 +110,7 @@ def __init__(self, fixer_names, options=None, explicit=[]):
103110
explicit: a list of fixers to run even if they are explicit.
104111
"""
105112
self.fixers = fixer_names
106-
self.explicit = explicit
113+
self.explicit = explicit or []
107114
self.options = self._default_options.copy()
108115
if options is not None:
109116
self.options.update(options)
@@ -134,29 +141,17 @@ def get_fixers(self):
134141
pre_order_fixers = []
135142
post_order_fixers = []
136143
for fix_mod_path in self.fixers:
137-
try:
138-
mod = __import__(fix_mod_path, {}, {}, ["*"])
139-
except ImportError:
140-
self.log_error("Can't load transformation module %s",
141-
fix_mod_path)
142-
continue
144+
mod = __import__(fix_mod_path, {}, {}, ["*"])
143145
fix_name = fix_mod_path.rsplit(".", 1)[-1]
144-
if fix_name.startswith("fix_"):
145-
fix_name = fix_name[4:]
146+
if fix_name.startswith(self.FILE_PREFIX):
147+
fix_name = fix_name[len(self.FILE_PREFIX):]
146148
parts = fix_name.split("_")
147-
class_name = "Fix" + "".join([p.title() for p in parts])
149+
class_name = self.CLASS_PREFIX + "".join([p.title() for p in parts])
148150
try:
149151
fix_class = getattr(mod, class_name)
150152
except AttributeError:
151-
self.log_error("Can't find %s.%s",
152-
fix_name, class_name)
153-
continue
154-
try:
155-
fixer = fix_class(self.options, self.fixer_log)
156-
except Exception as err:
157-
self.log_error("Can't instantiate fixes.fix_%s.%s()",
158-
fix_name, class_name, exc_info=True)
159-
continue
153+
raise FixerError("Can't find %s.%s" % (fix_name, class_name))
154+
fixer = fix_class(self.options, self.fixer_log)
160155
if fixer.explicit and self.explicit is not True and \
161156
fix_mod_path not in self.explicit:
162157
self.log_message("Skipping implicit fixer: %s", fix_name)
@@ -168,17 +163,16 @@ def get_fixers(self):
168163
elif fixer.order == "post":
169164
post_order_fixers.append(fixer)
170165
else:
171-
raise ValueError("Illegal fixer order: %r" % fixer.order)
166+
raise FixerError("Illegal fixer order: %r" % fixer.order)
172167

173168
key_func = operator.attrgetter("run_order")
174169
pre_order_fixers.sort(key=key_func)
175170
post_order_fixers.sort(key=key_func)
176171
return (pre_order_fixers, post_order_fixers)
177172

178173
def log_error(self, msg, *args, **kwds):
179-
"""Increments error count and log a message."""
180-
self.errors.append((msg, args, kwds))
181-
self.logger.error(msg, *args, **kwds)
174+
"""Called when an error occurs."""
175+
raise
182176

183177
def log_message(self, msg, *args):
184178
"""Hook to log a message."""
@@ -191,13 +185,17 @@ def log_debug(self, msg, *args):
191185
msg = msg % args
192186
self.logger.debug(msg)
193187

188+
def print_output(self, lines):
189+
"""Called with lines of output to give to the user."""
190+
pass
191+
194192
def refactor(self, items, write=False, doctests_only=False):
195193
"""Refactor a list of files and directories."""
196194
for dir_or_file in items:
197195
if os.path.isdir(dir_or_file):
198-
self.refactor_dir(dir_or_file, write)
196+
self.refactor_dir(dir_or_file, write, doctests_only)
199197
else:
200-
self.refactor_file(dir_or_file, write)
198+
self.refactor_file(dir_or_file, write, doctests_only)
201199

202200
def refactor_dir(self, dir_name, write=False, doctests_only=False):
203201
"""Descends down a directory and refactor every Python file found.
@@ -348,12 +346,11 @@ def processed_file(self, new_text, filename, old_text=None, write=False):
348346
if old_text == new_text:
349347
self.log_debug("No changes to %s", filename)
350348
return
351-
diff_texts(old_text, new_text, filename)
352-
if not write:
353-
self.log_debug("Not writing changes to %s", filename)
354-
return
349+
self.print_output(diff_texts(old_text, new_text, filename))
355350
if write:
356351
self.write_file(new_text, filename, old_text)
352+
else:
353+
self.log_debug("Not writing changes to %s", filename)
357354

358355
def write_file(self, new_text, filename, old_text=None):
359356
"""Writes a string to a file.
@@ -528,10 +525,9 @@ def gen_lines(self, block, indent):
528525

529526

530527
def diff_texts(a, b, filename):
531-
"""Prints a unified diff of two strings."""
528+
"""Return a unified diff of two strings."""
532529
a = a.splitlines()
533530
b = b.splitlines()
534-
for line in difflib.unified_diff(a, b, filename, filename,
535-
"(original)", "(refactored)",
536-
lineterm=""):
537-
print(line)
531+
return difflib.unified_diff(a, b, filename, filename,
532+
"(original)", "(refactored)",
533+
lineterm="")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from lib2to3.fixer_base import BaseFix
2+
3+
class FixBadOrder(BaseFix):
4+
5+
order = "crazy"

Lib/lib2to3/tests/data/fixers/myfixes/__init__.py

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from lib2to3.fixer_base import BaseFix
2+
3+
class FixExplicit(BaseFix):
4+
explicit = True
5+
6+
def match(self): return False
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from lib2to3.fixer_base import BaseFix
2+
3+
class FixFirst(BaseFix):
4+
run_order = 1
5+
6+
def match(self, node): return False
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from lib2to3.fixer_base import BaseFix
2+
3+
class FixLast(BaseFix):
4+
5+
run_order = 10
6+
7+
def match(self, node): return False
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from lib2to3.fixer_base import BaseFix
2+
from lib2to3.fixer_util import Name
3+
4+
class FixParrot(BaseFix):
5+
"""
6+
Change functions named 'parrot' to 'cheese'.
7+
"""
8+
9+
PATTERN = """funcdef < 'def' name='parrot' any* >"""
10+
11+
def transform(self, node, results):
12+
name = results["name"]
13+
name.replace(Name("cheese", name.get_prefix()))
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from lib2to3.fixer_base import BaseFix
2+
3+
class FixPreorder(BaseFix):
4+
order = "pre"
5+
6+
def match(self, node): return False
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This is empty so trying to fetch the fixer class gives an AttributeError

0 commit comments

Comments
 (0)