changeset 40234:3fc2ef49959c

relnotes: port to Python 3 The big annoyance here was having to feed textwrap unicodes instead of bytes, but it all seems to work. Differential Revision: https://phab.mercurial-scm.org/D5053
author Augie Fackler <augie@google.com>
date Sat, 13 Oct 2018 03:27:21 -0400
parents 6309128ff61f
children a7cdd81f191b
files hgext/releasenotes.py
diffstat 1 files changed, 21 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/releasenotes.py	Sat Oct 13 11:01:38 2018 +0200
+++ b/hgext/releasenotes.py	Sat Oct 13 03:27:21 2018 -0400
@@ -59,6 +59,20 @@
 
 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 = {}
@@ -444,7 +458,7 @@
             lines.append('-' * len(title))
             lines.append('')
 
-            wrapper = textwrap.TextWrapper(width=78)
+            wrapper = byteswrapper(width=78)
             for i, para in enumerate(paragraphs):
                 if i:
                     lines.append('')
@@ -466,14 +480,14 @@
             lines.append('')
 
         for paragraphs in nontitled:
-            wrapper = textwrap.TextWrapper(initial_indent='* ',
-                                           subsequent_indent='  ',
-                                           width=78)
+            wrapper = byteswrapper(initial_indent='* ',
+                                   subsequent_indent='  ',
+                                   width=78)
             lines.extend(wrapper.wrap(' '.join(paragraphs[0])))
 
-            wrapper = textwrap.TextWrapper(initial_indent='  ',
-                                           subsequent_indent='  ',
-                                           width=78)
+            wrapper = byteswrapper(initial_indent='  ',
+                                   subsequent_indent='  ',
+                                   width=78)
             for para in paragraphs[1:]:
                 lines.append('')
                 lines.extend(wrapper.wrap(' '.join(para)))