Mercurial > hg
comparison doc/check-seclevel.py @ 32544:e9f456183402
doc: port check-seclevel.py to be Python 2/3 portable
author | Augie Fackler <raf@durin42.com> |
---|---|
date | Sun, 28 May 2017 15:51:26 -0400 |
parents | d83ca854fa21 |
children | 9c6473d2038b |
comparison
equal
deleted
inserted
replaced
32543:cf7c88986e9f | 32544:e9f456183402 |
---|---|
21 ) | 21 ) |
22 | 22 |
23 table = commands.table | 23 table = commands.table |
24 helptable = help.helptable | 24 helptable = help.helptable |
25 | 25 |
26 level2mark = ['"', '=', '-', '.', '#'] | 26 level2mark = [b'"', b'=', b'-', b'.', b'#'] |
27 reservedmarks = ['"'] | 27 reservedmarks = [b'"'] |
28 | 28 |
29 mark2level = {} | 29 mark2level = {} |
30 for m, l in zip(level2mark, xrange(len(level2mark))): | 30 for m, l in zip(level2mark, range(len(level2mark))): |
31 if m not in reservedmarks: | 31 if m not in reservedmarks: |
32 mark2level[m] = l | 32 mark2level[m] = l |
33 | 33 |
34 initlevel_topic = 0 | 34 initlevel_topic = 0 |
35 initlevel_cmd = 1 | 35 initlevel_cmd = 1 |
36 initlevel_ext = 1 | 36 initlevel_ext = 1 |
37 initlevel_ext_cmd = 3 | 37 initlevel_ext_cmd = 3 |
38 | 38 |
39 def showavailables(ui, initlevel): | 39 def showavailables(ui, initlevel): |
40 ui.warn((' available marks and order of them in this help: %s\n') % | 40 avail = (' available marks and order of them in this help: %s\n') % ( |
41 (', '.join(['%r' % (m * 4) for m in level2mark[initlevel + 1:]]))) | 41 ', '.join(['%r' % (m * 4) for m in level2mark[initlevel + 1:]])) |
42 ui.warn(avail.encode('utf-8')) | |
42 | 43 |
43 def checkseclevel(ui, doc, name, initlevel): | 44 def checkseclevel(ui, doc, name, initlevel): |
44 ui.note(('checking "%s"\n') % name) | 45 ui.note(('checking "%s"\n') % name) |
46 if not isinstance(doc, bytes): | |
47 doc = doc.encode('utf-8') | |
45 blocks, pruned = minirst.parse(doc, 0, ['verbose']) | 48 blocks, pruned = minirst.parse(doc, 0, ['verbose']) |
46 errorcnt = 0 | 49 errorcnt = 0 |
47 curlevel = initlevel | 50 curlevel = initlevel |
48 for block in blocks: | 51 for block in blocks: |
49 if block['type'] != 'section': | 52 if block[b'type'] != b'section': |
50 continue | 53 continue |
51 mark = block['underline'] | 54 mark = block[b'underline'] |
52 title = block['lines'][0] | 55 title = block[b'lines'][0] |
53 if (mark not in mark2level) or (mark2level[mark] <= initlevel): | 56 if (mark not in mark2level) or (mark2level[mark] <= initlevel): |
54 ui.warn(('invalid section mark %r for "%s" of %s\n') % | 57 ui.warn((('invalid section mark %r for "%s" of %s\n') % |
55 (mark * 4, title, name)) | 58 (mark * 4, title, name)).encode('utf-8')) |
56 showavailables(ui, initlevel) | 59 showavailables(ui, initlevel) |
57 errorcnt += 1 | 60 errorcnt += 1 |
58 continue | 61 continue |
59 nextlevel = mark2level[mark] | 62 nextlevel = mark2level[mark] |
60 if curlevel < nextlevel and curlevel + 1 != nextlevel: | 63 if curlevel < nextlevel and curlevel + 1 != nextlevel: |
70 return errorcnt | 73 return errorcnt |
71 | 74 |
72 def checkcmdtable(ui, cmdtable, namefmt, initlevel): | 75 def checkcmdtable(ui, cmdtable, namefmt, initlevel): |
73 errorcnt = 0 | 76 errorcnt = 0 |
74 for k, entry in cmdtable.items(): | 77 for k, entry in cmdtable.items(): |
75 name = k.split("|")[0].lstrip("^") | 78 name = k.split(b"|")[0].lstrip(b"^") |
76 if not entry[0].__doc__: | 79 if not entry[0].__doc__: |
77 ui.note(('skip checking %s: no help document\n') % | 80 ui.note(('skip checking %s: no help document\n') % |
78 (namefmt % name)) | 81 (namefmt % name)) |
79 continue | 82 continue |
80 errorcnt += checkseclevel(ui, entry[0].__doc__, | 83 errorcnt += checkseclevel(ui, entry[0].__doc__, |
91 '%s help topic' % names[0], | 94 '%s help topic' % names[0], |
92 initlevel_topic) | 95 initlevel_topic) |
93 | 96 |
94 errorcnt += checkcmdtable(ui, table, '%s command', initlevel_cmd) | 97 errorcnt += checkcmdtable(ui, table, '%s command', initlevel_cmd) |
95 | 98 |
96 for name in sorted(extensions.enabled().keys() + | 99 for name in sorted(list(extensions.enabled()) + |
97 extensions.disabled().keys()): | 100 list(extensions.disabled())): |
98 mod = extensions.load(ui, name, None) | 101 mod = extensions.load(ui, name, None) |
99 if not mod.__doc__: | 102 if not mod.__doc__: |
100 ui.note(('skip checking %s extension: no help document\n') % name) | 103 ui.note(('skip checking %s extension: no help document\n') % name) |
101 continue | 104 continue |
102 errorcnt += checkseclevel(ui, mod.__doc__, | 105 errorcnt += checkseclevel(ui, mod.__doc__, |
104 initlevel_ext) | 107 initlevel_ext) |
105 | 108 |
106 cmdtable = getattr(mod, 'cmdtable', None) | 109 cmdtable = getattr(mod, 'cmdtable', None) |
107 if cmdtable: | 110 if cmdtable: |
108 errorcnt += checkcmdtable(ui, cmdtable, | 111 errorcnt += checkcmdtable(ui, cmdtable, |
109 '%s command of ' + name + ' extension', | 112 '%%s command of %s extension' % name, |
110 initlevel_ext_cmd) | 113 initlevel_ext_cmd) |
111 return errorcnt | 114 return errorcnt |
112 | 115 |
113 def checkfile(ui, filename, initlevel): | 116 def checkfile(ui, filename, initlevel): |
114 if filename == '-': | 117 if filename == '-': |