[Python-checkins] cpython: Issue #10639: reindent.py tool now accepts a --newline option to specify the

jason.coombs python-checkins at python.org
Tue Jul 26 17:59:26 CEST 2011


http://hg.python.org/cpython/rev/4feb889d3bed
changeset:   71506:4feb889d3bed
user:        Jason R. Coombs <jaraco at jaraco.com>
date:        Tue Jul 26 11:38:04 2011 -0400
summary:
  Issue #10639: reindent.py tool now accepts a --newline option to specify the newline to be used in the output of converted files.

files:
  Misc/NEWS                 |   1 +
  Tools/scripts/reindent.py |  16 ++++++++++++----
  2 files changed, 13 insertions(+), 4 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -1075,6 +1075,7 @@
 
 - Issue #10639: reindent.py no longer converts newlines and will raise
   an error if attempting to convert a file with mixed newlines.
+  "--newline" option added to specify new line character.
 
 Extension Modules
 -----------------
diff --git a/Tools/scripts/reindent.py b/Tools/scripts/reindent.py
--- a/Tools/scripts/reindent.py
+++ b/Tools/scripts/reindent.py
@@ -8,6 +8,8 @@
 -r (--recurse)  Recurse.   Search for all .py files in subdirectories too.
 -n (--nobackup) No backup. Does not make a ".bak" file before reindenting.
 -v (--verbose)  Verbose.   Print informative msgs; else no output.
+   (--newline)  Newline.   Specify the newline character to use (CRLF, LF).
+                           Default is the same as the original file.
 -h (--help)     Help.      Print this usage information and exit.
 
 Change Python (.py) files to use 4-space indents and no hard tab characters.
@@ -65,10 +67,11 @@
 
 def main():
     import getopt
-    global verbose, recurse, dryrun, makebackup
+    global verbose, recurse, dryrun, makebackup, spec_newline
+    spec_newline = None
     try:
         opts, args = getopt.getopt(sys.argv[1:], "drnvh",
-                        ["dryrun", "recurse", "nobackup", "verbose", "help"])
+            ["dryrun", "recurse", "nobackup", "verbose", "newline=", "help"])
     except getopt.error as msg:
         usage(msg)
         return
@@ -81,6 +84,11 @@
             makebackup = False
         elif o in ('-v', '--verbose'):
             verbose = True
+        elif o in ('--newline',):
+            if not a.upper() in ('CRLF', 'LF'):
+                usage()
+                return
+            spec_newline = dict(CRLF='\r\n', LF='\n')[a.upper()]
         elif o in ('-h', '--help'):
             usage()
             return
@@ -118,9 +126,9 @@
         errprint("%s: I/O Error: %s" % (file, str(msg)))
         return
 
-    newline = r.newlines
+    newline = spec_newline if spec_newline else r.newlines
     if isinstance(newline, tuple):
-        errprint("%s: mixed newlines detected; cannot process file" % file)
+        errprint("%s: mixed newlines detected; cannot continue without --newline" % file)
         return
 
     if r.run():

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list