[Python-checkins] distutils2: Replacing wrap_text function with Python builtin textwrap.wrap
tarek.ziade
python-checkins at python.org
Sun Mar 13 19:45:15 CET 2011
http://hg.python.org/distutils2/rev/41f54eecbfa3
changeset: 1100:41f54eecbfa3
user: Arc Riley <arcriley at gmail.com>
date: Sat Nov 20 15:38:35 2010 -0500
summary:
Replacing wrap_text function with Python builtin textwrap.wrap
files:
distutils2/fancy_getopt.py
diff --git a/distutils2/fancy_getopt.py b/distutils2/fancy_getopt.py
--- a/distutils2/fancy_getopt.py
+++ b/distutils2/fancy_getopt.py
@@ -13,6 +13,7 @@
import string
import re
import getopt
+import textwrap
from distutils2.errors import DistutilsGetoptError, DistutilsArgError
# Much like command_re in distutils.core, this is close to but not quite
@@ -349,7 +350,7 @@
for option in self.option_table:
long, short, help = option[:3]
- text = wrap_text(help, text_width)
+ text = textwrap.wrap(help, text_width)
if long[-1] == '=':
long = long[0:-1]
@@ -394,67 +395,6 @@
return parser.getopt(args, object)
-WS_TRANS = string.maketrans(string.whitespace, ' ' * len(string.whitespace))
-
-def wrap_text (text, width):
- """wrap_text(text : string, width : int) -> [string]
-
- Split 'text' into multiple lines of no more than 'width' characters
- each, and return the list of strings that results.
- """
-
- if text is None:
- return []
- if len(text) <= width:
- return [text]
-
- text = string.expandtabs(text)
- text = string.translate(text, WS_TRANS)
- chunks = re.split(r'( +|-+)', text)
- chunks = filter(None, chunks) # ' - ' results in empty strings
- lines = []
-
- while chunks:
-
- cur_line = [] # list of chunks (to-be-joined)
- cur_len = 0 # length of current line
-
- while chunks:
- l = len(chunks[0])
- if cur_len + l <= width: # can squeeze (at least) this chunk in
- cur_line.append(chunks[0])
- del chunks[0]
- cur_len = cur_len + l
- else: # this line is full
- # drop last chunk if all space
- if cur_line and cur_line[-1][0] == ' ':
- del cur_line[-1]
- break
-
- if chunks: # any chunks left to process?
-
- # if the current line is still empty, then we had a single
- # chunk that's too big too fit on a line -- so we break
- # down and break it up at the line width
- if cur_len == 0:
- cur_line.append(chunks[0][0:width])
- chunks[0] = chunks[0][width:]
-
- # all-whitespace chunks at the end of a line can be discarded
- # (and we know from the re.split above that if a chunk has
- # *any* whitespace, it is *all* whitespace)
- if chunks[0][0] == ' ':
- del chunks[0]
-
- # and store this line in the list-of-all-lines -- as a single
- # string, of course!
- lines.append(string.join(cur_line, ''))
-
- # while chunks
-
- return lines
-
-
class OptionDummy(object):
"""Dummy class just used as a place to hold command-line option
values as instance attributes."""
--
Repository URL: http://hg.python.org/distutils2
More information about the Python-checkins
mailing list