summaryrefslogtreecommitdiff
path: root/Lib/textwrap.py
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2013-08-21 13:26:34 +0200
committerChristian Heimes <christian@cheimes.de>2013-08-21 13:26:34 +0200
commitd1e599e1c9f6aa68e2974fb2746d5bb5b49f35e3 (patch)
treeec860a7832a8b48b4962deededc6034688a0739d /Lib/textwrap.py
parent5ac4b5df2c236c92a1c9bf79d15f0b0bf88b9904 (diff)
parent8d78b37b4eae9ab6191aacd9b885f32d134a70f6 (diff)
downloadcpython-d1e599e1c9f6aa68e2974fb2746d5bb5b49f35e3.tar.gz
Issue #18747: Re-seed OpenSSL's pseudo-random number generator after fork.
A pthread_atfork() child handler is used to seeded the PRNG with pid, time and some stack data.
Diffstat (limited to 'Lib/textwrap.py')
-rw-r--r--Lib/textwrap.py53
1 files changed, 51 insertions, 2 deletions
diff --git a/Lib/textwrap.py b/Lib/textwrap.py
index 7024d4d245..27ebc16e16 100644
--- a/Lib/textwrap.py
+++ b/Lib/textwrap.py
@@ -19,6 +19,8 @@ __all__ = ['TextWrapper', 'wrap', 'fill', 'dedent', 'indent']
# since 0xa0 is not in range(128).
_whitespace = '\t\n\x0b\x0c\r '
+_default_placeholder = ' [...]'
+
class TextWrapper:
"""
Object for wrapping/filling text. The public interface consists of
@@ -277,6 +279,9 @@ class TextWrapper:
return lines
+ def _split_chunks(self, text):
+ text = self._munge_whitespace(text)
+ return self._split(text)
# -- Public interface ----------------------------------------------
@@ -289,8 +294,7 @@ class TextWrapper:
and all other whitespace characters (including newline) are
converted to space.
"""
- text = self._munge_whitespace(text)
- chunks = self._split(text)
+ chunks = self._split_chunks(text)
if self.fix_sentence_endings:
self._fix_sentence_endings(chunks)
return self._wrap_chunks(chunks)
@@ -304,6 +308,36 @@ class TextWrapper:
"""
return "\n".join(self.wrap(text))
+ def shorten(self, text, *, placeholder=_default_placeholder):
+ """shorten(text: str) -> str
+
+ Collapse and truncate the given text to fit in 'self.width' columns.
+ """
+ max_length = self.width
+ if max_length < len(placeholder.strip()):
+ raise ValueError("placeholder too large for max width")
+ sep = ' '
+ sep_len = len(sep)
+ parts = []
+ cur_len = 0
+ chunks = self._split_chunks(text)
+ for chunk in chunks:
+ if not chunk.strip():
+ continue
+ chunk_len = len(chunk) + sep_len if parts else len(chunk)
+ if cur_len + chunk_len > max_length:
+ break
+ parts.append(chunk)
+ cur_len += chunk_len
+ else:
+ # No truncation necessary
+ return sep.join(parts)
+ max_truncated_length = max_length - len(placeholder)
+ while parts and cur_len > max_truncated_length:
+ last = parts.pop()
+ cur_len -= len(last) + sep_len
+ return (sep.join(parts) + placeholder).strip()
+
# -- Convenience interface ---------------------------------------------
@@ -332,6 +366,21 @@ def fill(text, width=70, **kwargs):
w = TextWrapper(width=width, **kwargs)
return w.fill(text)
+def shorten(text, width, *, placeholder=_default_placeholder, **kwargs):
+ """Collapse and truncate the given text to fit in the given width.
+
+ The text first has its whitespace collapsed. If it then fits in
+ the *width*, it is returned as is. Otherwise, as many words
+ as possible are joined and then the placeholder is appended::
+
+ >>> textwrap.shorten("Hello world!", width=12)
+ 'Hello world!'
+ >>> textwrap.shorten("Hello world!", width=11)
+ 'Hello [...]'
+ """
+ w = TextWrapper(width=width, **kwargs)
+ return w.shorten(text, placeholder=placeholder)
+
# -- Loosely related functionality -------------------------------------