CVS: tmda/TMDA ChangeLog,1.311,1.312 Util.py,1.113,1.114

"Jason R. Mastaler" <[email protected]> Tue, 17 Feb 2004 13:12:38 -0800
Newsgroups gmane.mail.spam.tmda.cvs
Message-ID <[email protected]>
Update of /cvsroot/tmda/tmda/TMDA
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21103/TMDA

Modified Files:
	ChangeLog Util.py 
Log Message:
Use Python's new textwrap module in Util.wraptext() instead
of our own implementation (stolen from Mailman).


Index: ChangeLog
===================================================================
RCS file: /cvsroot/tmda/tmda/TMDA/ChangeLog,v
retrieving revision 1.311
retrieving revision 1.312
diff -u -r1.311 -r1.312
--- ChangeLog	6 Feb 2004 01:21:14 -0000	1.311
+++ ChangeLog	17 Feb 2004 21:12:35 -0000	1.312
@@ -1,3 +1,7 @@
+2004-02-17  Jason R. Mastaler  <[email protected]>
+
+	* Util.py (wraptext): Utilize the new builtin textwrap module.
+	
 2004-02-05  Jason R. Mastaler  <[email protected]>
 
 	* Defaults.py (_path_vars): New dict to hold variables that

Index: Util.py
===================================================================
RCS file: /cvsroot/tmda/tmda/TMDA/Util.py,v
retrieving revision 1.113
retrieving revision 1.114
diff -u -r1.113 -r1.114
--- Util.py	25 Jan 2004 18:30:11 -0000	1.113
+++ Util.py	17 Feb 2004 21:12:35 -0000	1.114
@@ -23,7 +23,6 @@
 
 
 from cStringIO import StringIO
-from string import whitespace as WHITESPACE
 import cPickle
 import email
 import email.Utils
@@ -37,17 +36,15 @@
 import stat
 import sys
 import tempfile
+import textwrap
 import time
 
 import Errors
 
 
-DOT = '.'
-EMPTYSTRING = ''
 MODE_EXEC = 01
 MODE_READ = 04
 MODE_WRITE = 02
-NL = '\n'
 POSIX_NAME_MAX = 255                    # maximum length of a file name
 
 
@@ -799,83 +796,10 @@
                         return 1
 
 
-def wraptext(text, column=70, honor_leading_ws=1):
-    """Wrap and fill the text to the specified column.
-
-    Wrapping is always in effect, although if it is not possible to
-    wrap a line (because some word is longer than `column' characters)
-    the line is broken at the next available whitespace boundary.
-    Paragraphs are also always filled, unless honor_leading_ws is true
-    and the line begins with whitespace.
-
-    Includes code from Mailman
-    <URL:http://www.gnu.org/software/mailman/mailman.html>
-    Copyright (C) 1998,1999,2000,2001 by the Free Software Foundation, Inc.,
-    and licensed under the GNU General Public License version 2.
-    """
-    wrapped = ''
-    # first split the text into paragraphs, defined as a blank line
-    paras = re.split('\n\n', text)
-    for para in paras:
-        # fill
-        lines = []
-        fillprev = 0
-        for line in para.split(NL):
-            if not line:
-                lines.append(line)
-                continue
-            if honor_leading_ws and line[0] in WHITESPACE:
-                fillthis = 0
-            else:
-                fillthis = 1
-            if fillprev and fillthis:
-                # if the previous line should be filled, then just append a
-                # single space, and the rest of the current line
-                lines[-1] = lines[-1].rstrip() + ' ' + line
-            else:
-                # no fill, i.e. retain newline
-                lines.append(line)
-            fillprev = fillthis
-        # wrap each line
-        for text in lines:
-            while text:
-                if len(text) <= column:
-                    line = text
-                    text = ''
-                else:
-                    bol = column
-                    # find the last whitespace character
-                    while bol > 0 and text[bol] not in WHITESPACE:
-                        bol = bol - 1
-                    # now find the last non-whitespace character
-                    eol = bol
-                    while eol > 0 and text[eol] in WHITESPACE:
-                        eol = eol - 1
-                    # watch out for text that's longer than the column width
-                    if eol == 0:
-                        # break on whitespace after column
-                        eol = column
-                        while eol < len(text) and \
-                              text[eol] not in WHITESPACE:
-                            eol = eol + 1
-                        bol = eol
-                        while bol < len(text) and \
-                              text[bol] in WHITESPACE:
-                            bol = bol + 1
-                        bol = bol - 1
-                    line = text[:eol+1] + '\n'
-                    # find the next non-whitespace character
-                    bol = bol + 1
-                    while bol < len(text) and text[bol] in WHITESPACE:
-                        bol = bol + 1
-                    text = text[bol:]
-                wrapped = wrapped + line
-            wrapped = wrapped + '\n'
-            # end while text
-        wrapped = wrapped + '\n'
-        # end for text in lines
-    # the last two newlines are bogus
-    return wrapped[:-2]
+def wraptext(text, column=70):
+    """Wrap and fill the text to the specified column width."""
+    wrapper = textwrap.TextWrapper(width=column, break_long_words=False)
+    return wrapper.fill(text)
 
 
 def maketext(templatefile, vardict):