Close #13857: Added textwrap.indent() function (initial patch by Ezra
Berch)
diff --git a/Lib/test/test_textwrap.py b/Lib/test/test_textwrap.py
index bbd0882..bb4a851 100644
--- a/Lib/test/test_textwrap.py
+++ b/Lib/test/test_textwrap.py
@@ -11,7 +11,7 @@
import unittest
from test import support
-from textwrap import TextWrapper, wrap, fill, dedent
+from textwrap import TextWrapper, wrap, fill, dedent, indent
class BaseTestCase(unittest.TestCase):
@@ -594,11 +594,147 @@
self.assertEqual(expect, dedent(text))
+# Test textwrap.indent
+class IndentTestCase(unittest.TestCase):
+ # The examples used for tests. If any of these change, the expected
+ # results in the various test cases must also be updated.
+ # The roundtrip cases are separate, because textwrap.dedent doesn't
+ # handle Windows line endings
+ ROUNDTRIP_CASES = (
+ # Basic test case
+ "Hi.\nThis is a test.\nTesting.",
+ # Include a blank line
+ "Hi.\nThis is a test.\n\nTesting.",
+ # Include leading and trailing blank lines
+ "\nHi.\nThis is a test.\nTesting.\n",
+ )
+ CASES = ROUNDTRIP_CASES + (
+ # Use Windows line endings
+ "Hi.\r\nThis is a test.\r\nTesting.\r\n",
+ # Pathological case
+ "\nHi.\r\nThis is a test.\n\r\nTesting.\r\n\n",
+ )
+
+ def test_indent_nomargin_default(self):
+ # indent should do nothing if 'prefix' is empty.
+ for text in self.CASES:
+ self.assertEqual(indent(text, ''), text)
+
+ def test_indent_nomargin_explicit_default(self):
+ # The same as test_indent_nomargin, but explicitly requesting
+ # the default behaviour by passing None as the predicate
+ for text in self.CASES:
+ self.assertEqual(indent(text, '', None), text)
+
+ def test_indent_nomargin_all_lines(self):
+ # The same as test_indent_nomargin, but using the optional
+ # predicate argument
+ predicate = lambda line: True
+ for text in self.CASES:
+ self.assertEqual(indent(text, '', predicate), text)
+
+ def test_indent_no_lines(self):
+ # Explicitly skip indenting any lines
+ predicate = lambda line: False
+ for text in self.CASES:
+ self.assertEqual(indent(text, ' ', predicate), text)
+
+ def test_roundtrip_spaces(self):
+ # A whitespace prefix should roundtrip with dedent
+ for text in self.ROUNDTRIP_CASES:
+ self.assertEqual(dedent(indent(text, ' ')), text)
+
+ def test_roundtrip_tabs(self):
+ # A whitespace prefix should roundtrip with dedent
+ for text in self.ROUNDTRIP_CASES:
+ self.assertEqual(dedent(indent(text, '\t\t')), text)
+
+ def test_roundtrip_mixed(self):
+ # A whitespace prefix should roundtrip with dedent
+ for text in self.ROUNDTRIP_CASES:
+ self.assertEqual(dedent(indent(text, ' \t \t ')), text)
+
+ def test_indent_default(self):
+ # Test default indenting of lines that are not whitespace only
+ prefix = ' '
+ expected = (
+ # Basic test case
+ " Hi.\n This is a test.\n Testing.",
+ # Include a blank line
+ " Hi.\n This is a test.\n\n Testing.",
+ # Include leading and trailing blank lines
+ "\n Hi.\n This is a test.\n Testing.\n",
+ # Use Windows line endings
+ " Hi.\r\n This is a test.\r\n Testing.\r\n",
+ # Pathological case
+ "\n Hi.\r\n This is a test.\n\r\n Testing.\r\n\n",
+ )
+ for text, expect in zip(self.CASES, expected):
+ self.assertEqual(indent(text, prefix), expect)
+
+ def test_indent_explicit_default(self):
+ # Test default indenting of lines that are not whitespace only
+ prefix = ' '
+ expected = (
+ # Basic test case
+ " Hi.\n This is a test.\n Testing.",
+ # Include a blank line
+ " Hi.\n This is a test.\n\n Testing.",
+ # Include leading and trailing blank lines
+ "\n Hi.\n This is a test.\n Testing.\n",
+ # Use Windows line endings
+ " Hi.\r\n This is a test.\r\n Testing.\r\n",
+ # Pathological case
+ "\n Hi.\r\n This is a test.\n\r\n Testing.\r\n\n",
+ )
+ for text, expect in zip(self.CASES, expected):
+ self.assertEqual(indent(text, prefix, None), expect)
+
+ def test_indent_all_lines(self):
+ # Add 'prefix' to all lines, including whitespace-only ones.
+ prefix = ' '
+ expected = (
+ # Basic test case
+ " Hi.\n This is a test.\n Testing.",
+ # Include a blank line
+ " Hi.\n This is a test.\n \n Testing.",
+ # Include leading and trailing blank lines
+ " \n Hi.\n This is a test.\n Testing.\n",
+ # Use Windows line endings
+ " Hi.\r\n This is a test.\r\n Testing.\r\n",
+ # Pathological case
+ " \n Hi.\r\n This is a test.\n \r\n Testing.\r\n \n",
+ )
+ predicate = lambda line: True
+ for text, expect in zip(self.CASES, expected):
+ self.assertEqual(indent(text, prefix, predicate), expect)
+
+ def test_indent_empty_lines(self):
+ # Add 'prefix' solely to whitespace-only lines.
+ prefix = ' '
+ expected = (
+ # Basic test case
+ "Hi.\nThis is a test.\nTesting.",
+ # Include a blank line
+ "Hi.\nThis is a test.\n \nTesting.",
+ # Include leading and trailing blank lines
+ " \nHi.\nThis is a test.\nTesting.\n",
+ # Use Windows line endings
+ "Hi.\r\nThis is a test.\r\nTesting.\r\n",
+ # Pathological case
+ " \nHi.\r\nThis is a test.\n \r\nTesting.\r\n \n",
+ )
+ predicate = lambda line: not line.strip()
+ for text, expect in zip(self.CASES, expected):
+ self.assertEqual(indent(text, prefix, predicate), expect)
+
+
def test_main():
support.run_unittest(WrapTestCase,
LongWordTestCase,
IndentTestCases,
- DedentTestCase)
+ DedentTestCase,
+ IndentTestCase)
if __name__ == '__main__':
test_main()
diff --git a/Lib/textwrap.py b/Lib/textwrap.py
index 66ccf2b..7024d4d 100644
--- a/Lib/textwrap.py
+++ b/Lib/textwrap.py
@@ -7,7 +7,7 @@
import re
-__all__ = ['TextWrapper', 'wrap', 'fill', 'dedent']
+__all__ = ['TextWrapper', 'wrap', 'fill', 'dedent', 'indent']
# Hardcode the recognized whitespace characters to the US-ASCII
# whitespace characters. The main reason for doing this is that in
@@ -386,6 +386,25 @@
text = re.sub(r'(?m)^' + margin, '', text)
return text
+
+def indent(text, prefix, predicate=None):
+ """Adds 'prefix' to the beginning of selected lines in 'text'.
+
+ If 'predicate' is provided, 'prefix' will only be added to the lines
+ where 'predicate(line)' is True. If 'predicate' is not provided,
+ it will default to adding 'prefix' to all non-empty lines that do not
+ consist solely of whitespace characters.
+ """
+ if predicate is None:
+ def predicate(line):
+ return line.strip()
+
+ def prefixed_lines():
+ for line in text.splitlines(True):
+ yield (prefix + line if predicate(line) else line)
+ return ''.join(prefixed_lines())
+
+
if __name__ == "__main__":
#print dedent("\tfoo\n\tbar")
#print dedent(" \thello there\n \t how are you?")