[Python-checkins] r66646 - in sandbox/trunk/2to3/lib2to3: main.py refactor.py

benjamin.peterson python-checkins at python.org
Sat Sep 27 18:40:14 CEST 2008


Author: benjamin.peterson
Date: Sat Sep 27 18:40:13 2008
New Revision: 66646

Log:
don't print to stdout when 2to3 is used as a library

Modified:
   sandbox/trunk/2to3/lib2to3/main.py
   sandbox/trunk/2to3/lib2to3/refactor.py

Modified: sandbox/trunk/2to3/lib2to3/main.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/main.py	(original)
+++ sandbox/trunk/2to3/lib2to3/main.py	Sat Sep 27 18:40:13 2008
@@ -10,6 +10,16 @@
 from . import refactor
 
 
+class StdoutRefactoringTool(refactor.RefactoringTool):
+    """
+    Prints output to stdout.
+    """
+
+    def print_output(self, lines):
+        for line in lines:
+            print line
+
+
 def main(fixer_pkg, args=None):
     """Main program.
 
@@ -68,7 +78,7 @@
         fixer_names = avail_names if "all" in options.fix else explicit
     else:
         fixer_names = avail_names
-    rt = refactor.RefactoringTool(fixer_names, rt_opts, explicit=explicit)
+    rt = StdoutRefactoringTool(fixer_names, rt_opts, explicit=explicit)
 
     # Refactor all files and directories passed as arguments
     if not rt.errors:

Modified: sandbox/trunk/2to3/lib2to3/refactor.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/refactor.py	(original)
+++ sandbox/trunk/2to3/lib2to3/refactor.py	Sat Sep 27 18:40:13 2008
@@ -183,6 +183,10 @@
             msg = msg % args
         self.logger.debug(msg)
 
+    def print_output(self, lines):
+        """Called with lines of output to give to the user."""
+        pass
+
     def refactor(self, items, write=False, doctests_only=False):
         """Refactor a list of files and directories."""
         for dir_or_file in items:
@@ -340,12 +344,11 @@
         if old_text == new_text:
             self.log_debug("No changes to %s", filename)
             return
-        diff_texts(old_text, new_text, filename)
-        if not write:
-            self.log_debug("Not writing changes to %s", filename)
-            return
+        self.print_output(diff_texts(old_text, new_text, filename))
         if write:
             self.write_file(new_text, filename, old_text)
+        else:
+            self.log_debug("Not writing changes to %s", filename)
 
     def write_file(self, new_text, filename, old_text=None):
         """Writes a string to a file.
@@ -520,10 +523,9 @@
 
 
 def diff_texts(a, b, filename):
-    """Prints a unified diff of two strings."""
+    """Return a unified diff of two strings."""
     a = a.splitlines()
     b = b.splitlines()
-    for line in difflib.unified_diff(a, b, filename, filename,
-                                     "(original)", "(refactored)",
-                                     lineterm=""):
-        print line
+    return difflib.unified_diff(a, b, filename, filename,
+                                "(original)", "(refactored)",
+                                lineterm="")


More information about the Python-checkins mailing list