Patchwork releasenotes: use stringutil.wrap() instead of handcrafted TextWrapper wrapper

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 13, 2018, 10:44 a.m.
Message ID <a8b42b6d9d733fb128d0.1539427478@mimosa>
Download mbox | patch
Permalink /patch/35879/
State Accepted
Headers show

Comments

Yuya Nishihara - Oct. 13, 2018, 10:44 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1539426583 -7200
#      Sat Oct 13 12:29:43 2018 +0200
# Node ID a8b42b6d9d733fb128d09b6a484481a517441698
# Parent  379dd054614bdf645712ee51ac172c8b08ab3dc2
releasenotes: use stringutil.wrap() instead of handcrafted TextWrapper wrapper

It's silly to splitlines() a joined string, but we don't care the performance
here.
Pulkit Goyal - Oct. 13, 2018, 11:44 a.m.
On Sat, Oct 13, 2018 at 2:15 PM Yuya Nishihara <yuya@tcha.org> wrote:

> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1539426583 -7200
> #      Sat Oct 13 12:29:43 2018 +0200
> # Node ID a8b42b6d9d733fb128d09b6a484481a517441698
> # Parent  379dd054614bdf645712ee51ac172c8b08ab3dc2
> releasenotes: use stringutil.wrap() instead of handcrafted TextWrapper
> wrapper
>
> It's silly to splitlines() a joined string, but we don't care the
> performance
> here.
>

Queued this, many thanks!

Patch

diff --git a/hgext/releasenotes.py b/hgext/releasenotes.py
--- a/hgext/releasenotes.py
+++ b/hgext/releasenotes.py
@@ -16,7 +16,6 @@  from __future__ import absolute_import
 import difflib
 import errno
 import re
-import textwrap
 
 from mercurial.i18n import _
 from mercurial import (
@@ -29,6 +28,9 @@  from mercurial import (
     scmutil,
     util,
 )
+from mercurial.utils import (
+    stringutil,
+)
 
 cmdtable = {}
 command = registrar.command(cmdtable)
@@ -58,20 +60,6 @@  RE_ISSUE = br'\bissue ?[0-9]{4,6}(?![0-9
 
 BULLET_SECTION = _('Other Changes')
 
-if pycompat.ispy3:
-    class byteswrapper(object):
-        def __init__(self, **kwargs):
-            for k in kwargs:
-                v = kwargs[k]
-                if not isinstance(v, str) and isinstance(v, bytes):
-                    kwargs[k] = v.decode('utf8')
-            self._tw = textwrap.TextWrapper(**kwargs)
-        def wrap(self, data):
-            return [
-                l.encode('utf8') for l in self._tw.wrap(data.decode('utf8'))]
-else:
-    byteswrapper = textwrap.TextWrapper
-
 class parsedreleasenotes(object):
     def __init__(self):
         self.sections = {}
@@ -457,11 +445,11 @@  def serializenotes(sections, notes):
             lines.append('-' * len(title))
             lines.append('')
 
-            wrapper = byteswrapper(width=78)
             for i, para in enumerate(paragraphs):
                 if i:
                     lines.append('')
-                lines.extend(wrapper.wrap(' '.join(para)))
+                lines.extend(stringutil.wrap(' '.join(para),
+                                             width=78).splitlines())
 
             lines.append('')
 
@@ -479,17 +467,17 @@  def serializenotes(sections, notes):
             lines.append('')
 
         for paragraphs in nontitled:
-            wrapper = byteswrapper(initial_indent='* ',
-                                   subsequent_indent='  ',
-                                   width=78)
-            lines.extend(wrapper.wrap(' '.join(paragraphs[0])))
+            lines.extend(stringutil.wrap(' '.join(paragraphs[0]),
+                                         width=78,
+                                         initindent='* ',
+                                         hangindent='  ').splitlines())
 
-            wrapper = byteswrapper(initial_indent='  ',
-                                   subsequent_indent='  ',
-                                   width=78)
             for para in paragraphs[1:]:
                 lines.append('')
-                lines.extend(wrapper.wrap(' '.join(para)))
+                lines.extend(stringutil.wrap(' '.join(para),
+                                             width=78,
+                                             initindent='  ',
+                                             hangindent='  ').splitlines())
 
             lines.append('')