Patchwork [02,of,10,py3] doc: port check-seclevel.py to be Python 2/3 portable

login
register
mail settings
Submitter Augie Fackler
Date May 29, 2017, 2:32 p.m.
Message ID <4991a75faad243e7b80a.1496068344@imladris.local>
Download mbox | patch
Permalink /patch/21048/
State Accepted
Headers show

Comments

Augie Fackler - May 29, 2017, 2:32 p.m.
# HG changeset patch
# User Augie Fackler <raf@durin42.com>
# Date 1496001086 14400
#      Sun May 28 15:51:26 2017 -0400
# Node ID 4991a75faad243e7b80a51bddc5e5a3d86989b3b
# Parent  75e176c753d2c2c7eb5b5c0791bc993160fcb7b1
doc: port check-seclevel.py to be Python 2/3 portable
Yuya Nishihara - May 30, 2017, 1:02 p.m.
On Mon, 29 May 2017 10:32:24 -0400, Augie Fackler wrote:
> # HG changeset patch
> # User Augie Fackler <raf@durin42.com>
> # Date 1496001086 14400
> #      Sun May 28 15:51:26 2017 -0400
> # Node ID 4991a75faad243e7b80a51bddc5e5a3d86989b3b
> # Parent  75e176c753d2c2c7eb5b5c0791bc993160fcb7b1
> doc: port check-seclevel.py to be Python 2/3 portable

Queued 2, 3, 6-9, thanks.

Patch

diff --git a/doc/check-seclevel.py b/doc/check-seclevel.py
--- a/doc/check-seclevel.py
+++ b/doc/check-seclevel.py
@@ -23,11 +23,11 @@  from mercurial import (
 table = commands.table
 helptable = help.helptable
 
-level2mark = ['"', '=', '-', '.', '#']
-reservedmarks = ['"']
+level2mark = [b'"', b'=', b'-', b'.', b'#']
+reservedmarks = [b'"']
 
 mark2level = {}
-for m, l in zip(level2mark, xrange(len(level2mark))):
+for m, l in zip(level2mark, range(len(level2mark))):
     if m not in reservedmarks:
         mark2level[m] = l
 
@@ -37,22 +37,25 @@  initlevel_ext = 1
 initlevel_ext_cmd = 3
 
 def showavailables(ui, initlevel):
-    ui.warn(('    available marks and order of them in this help: %s\n') %
-            (', '.join(['%r' % (m * 4) for m in level2mark[initlevel + 1:]])))
+    avail = ('    available marks and order of them in this help: %s\n') % (
+        ', '.join(['%r' % (m * 4) for m in level2mark[initlevel + 1:]]))
+    ui.warn(avail.encode('utf-8'))
 
 def checkseclevel(ui, doc, name, initlevel):
     ui.note(('checking "%s"\n') % name)
+    if not isinstance(doc, bytes):
+        doc = doc.encode('utf-8')
     blocks, pruned = minirst.parse(doc, 0, ['verbose'])
     errorcnt = 0
     curlevel = initlevel
     for block in blocks:
-        if block['type'] != 'section':
+        if block[b'type'] != b'section':
             continue
-        mark = block['underline']
-        title = block['lines'][0]
+        mark = block[b'underline']
+        title = block[b'lines'][0]
         if (mark not in mark2level) or (mark2level[mark] <= initlevel):
-            ui.warn(('invalid section mark %r for "%s" of %s\n') %
-                    (mark * 4, title, name))
+            ui.warn((('invalid section mark %r for "%s" of %s\n') %
+                     (mark * 4, title, name)).encode('utf-8'))
             showavailables(ui, initlevel)
             errorcnt += 1
             continue
@@ -72,7 +75,7 @@  def checkseclevel(ui, doc, name, initlev
 def checkcmdtable(ui, cmdtable, namefmt, initlevel):
     errorcnt = 0
     for k, entry in cmdtable.items():
-        name = k.split("|")[0].lstrip("^")
+        name = k.split(b"|")[0].lstrip(b"^")
         if not entry[0].__doc__:
             ui.note(('skip checking %s: no help document\n') %
                     (namefmt % name))
@@ -93,8 +96,8 @@  def checkhghelps(ui):
 
     errorcnt += checkcmdtable(ui, table, '%s command', initlevel_cmd)
 
-    for name in sorted(extensions.enabled().keys() +
-                       extensions.disabled().keys()):
+    for name in sorted(list(extensions.enabled()) +
+                       list(extensions.disabled())):
         mod = extensions.load(ui, name, None)
         if not mod.__doc__:
             ui.note(('skip checking %s extension: no help document\n') % name)
@@ -106,7 +109,7 @@  def checkhghelps(ui):
         cmdtable = getattr(mod, 'cmdtable', None)
         if cmdtable:
             errorcnt += checkcmdtable(ui, cmdtable,
-                                      '%s command of ' + name + ' extension',
+                                      '%%s command of %s extension' % name,
                                       initlevel_ext_cmd)
     return errorcnt