]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Mark `wraptext` deprecated; use `TextWrapper` directly in `write_po` (#1140)
authorAarni Koskela <akx@iki.fi>
Sun, 20 Oct 2024 06:52:07 +0000 (09:52 +0300)
committerGitHub <noreply@github.com>
Sun, 20 Oct 2024 06:52:07 +0000 (06:52 +0000)
* Mark `wraptext` deprecated; use `TextWrapper` directly in `write_po`

Co-authored-by: Tomas R. <tomas.roun8@gmail.com>
babel/messages/pofile.py
babel/util.py

index fa5c859cdd49bf476d94a1d30efede5f5483ae03..b7a524051d7b8e033e4f6e7baef089aa08169af7 100644 (file)
@@ -17,7 +17,7 @@ from typing import TYPE_CHECKING
 
 from babel.core import Locale
 from babel.messages.catalog import Catalog, Message
-from babel.util import _cmp, wraptext
+from babel.util import TextWrapper, _cmp
 
 if TYPE_CHECKING:
     from typing import IO, AnyStr
@@ -637,8 +637,11 @@ def generate_po(
     # provide the same behaviour
     comment_width = width if width and width > 0 else 76
 
+    comment_wrapper = TextWrapper(width=comment_width)
+    header_wrapper = TextWrapper(width=width, subsequent_indent="# ")
+
     def _format_comment(comment, prefix=''):
-        for line in wraptext(comment, comment_width):
+        for line in comment_wrapper.wrap(comment):
             yield f"#{prefix} {line.strip()}\n"
 
     def _format_message(message, prefix=''):
@@ -668,8 +671,7 @@ def generate_po(
             if width and width > 0:
                 lines = []
                 for line in comment_header.splitlines():
-                    lines += wraptext(line, width=width,
-                                      subsequent_indent='# ')
+                    lines += header_wrapper.wrap(line)
                 comment_header = '\n'.join(lines)
             yield f"{comment_header}\n"
 
index 605695b1fafbfde80200d78bae47452b6678dd2c..db82d7361457bd4669368d344f773ac55f709026 100644 (file)
@@ -15,6 +15,7 @@ import datetime
 import os
 import re
 import textwrap
+import warnings
 from collections.abc import Generator, Iterable
 from typing import IO, Any, TypeVar
 
@@ -217,6 +218,12 @@ def wraptext(text: str, width: int = 70, initial_indent: str = '', subsequent_in
     :param subsequent_indent: string that will be prepended to all lines save
                               the first of wrapped output
     """
+    warnings.warn(
+        "`babel.util.wraptext` is deprecated and will be removed in a future version of Babel. "
+        "If you need this functionality, use the `babel.util.TextWrapper` class directly.",
+        DeprecationWarning,
+        stacklevel=2,
+    )
     wrapper = TextWrapper(width=width, initial_indent=initial_indent,
                           subsequent_indent=subsequent_indent,
                           break_long_words=False)