summaryrefslogtreecommitdiff
path: root/distutils2/fancy_getopt.py
diff options
context:
space:
mode:
authorArc Riley <arcriley@gmail.com>2010-11-20 15:38:35 -0500
committerArc Riley <arcriley@gmail.com>2010-11-20 15:38:35 -0500
commitf28f48db9c584823def5f781fedd8d37977df776 (patch)
treecdd133c0d54a5d743e6c17f135109f10e8bb519a /distutils2/fancy_getopt.py
parent2ead0a2b0c592a69e0aa7859b8f3479a2d714551 (diff)
downloaddisutils2-f28f48db9c584823def5f781fedd8d37977df776.tar.gz
Replacing wrap_text function with Python builtin textwrap.wrap
Diffstat (limited to 'distutils2/fancy_getopt.py')
-rw-r--r--distutils2/fancy_getopt.py64
1 files changed, 2 insertions, 62 deletions
diff --git a/distutils2/fancy_getopt.py b/distutils2/fancy_getopt.py
index 4ffbaa7..4cfb3ea 100644
--- a/distutils2/fancy_getopt.py
+++ b/distutils2/fancy_getopt.py
@@ -13,6 +13,7 @@ import sys
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 @@ class FancyGetopt(object):
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 @@ def fancy_getopt (options, negative_opt, object, args):
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."""