##// END OF EJS Templates
check-seclevel: restore use of callable() since it was readded in Python 3.2
Augie Fackler -
r21792:e15c991f default
parent child Browse files
Show More
@@ -1,170 +1,169 b''
1 1 #!/usr/bin/env python
2 2 #
3 3 # checkseclevel - checking section title levels in each online help documents
4 4
5 5 import sys, os
6 6 import optparse
7 7
8 8 # import from the live mercurial repo
9 9 sys.path.insert(0, "..")
10 10 # fall back to pure modules if required C extensions are not available
11 11 sys.path.append(os.path.join('..', 'mercurial', 'pure'))
12 12 from mercurial import demandimport; demandimport.enable()
13 13 from mercurial.commands import table
14 14 from mercurial.help import helptable
15 15 from mercurial import extensions
16 16 from mercurial import minirst
17 from mercurial import util
18 17
19 18 _verbose = False
20 19
21 20 def verbose(msg):
22 21 if _verbose:
23 22 print msg
24 23
25 24 def error(msg):
26 25 sys.stderr.write('%s\n' % msg)
27 26
28 27 level2mark = ['"', '=', '-', '.', '#']
29 28 reservedmarks = ['"']
30 29
31 30 mark2level = {}
32 31 for m, l in zip(level2mark, xrange(len(level2mark))):
33 32 if m not in reservedmarks:
34 33 mark2level[m] = l
35 34
36 35 initlevel_topic = 0
37 36 initlevel_cmd = 1
38 37 initlevel_ext = 1
39 38 initlevel_ext_cmd = 3
40 39
41 40 def showavailables(initlevel):
42 41 error(' available marks and order of them in this help: %s' %
43 42 (', '.join(['%r' % (m * 4) for m in level2mark[initlevel + 1:]])))
44 43
45 44 def checkseclevel(doc, name, initlevel):
46 45 verbose('checking "%s"' % name)
47 46 blocks, pruned = minirst.parse(doc, 0, ['verbose'])
48 47 errorcnt = 0
49 48 curlevel = initlevel
50 49 for block in blocks:
51 50 if block['type'] != 'section':
52 51 continue
53 52 mark = block['underline']
54 53 title = block['lines'][0]
55 54 if (mark not in mark2level) or (mark2level[mark] <= initlevel):
56 55 error('invalid section mark %r for "%s" of %s' %
57 56 (mark * 4, title, name))
58 57 showavailables(initlevel)
59 58 errorcnt += 1
60 59 continue
61 60 nextlevel = mark2level[mark]
62 61 if curlevel < nextlevel and curlevel + 1 != nextlevel:
63 62 error('gap of section level at "%s" of %s' %
64 63 (title, name))
65 64 showavailables(initlevel)
66 65 errorcnt += 1
67 66 continue
68 67 verbose('appropriate section level for "%s %s"' %
69 68 (mark * (nextlevel * 2), title))
70 69 curlevel = nextlevel
71 70
72 71 return errorcnt
73 72
74 73 def checkcmdtable(cmdtable, namefmt, initlevel):
75 74 errorcnt = 0
76 75 for k, entry in cmdtable.items():
77 76 name = k.split("|")[0].lstrip("^")
78 77 if not entry[0].__doc__:
79 78 verbose('skip checking %s: no help document' %
80 79 (namefmt % name))
81 80 continue
82 81 errorcnt += checkseclevel(entry[0].__doc__,
83 82 namefmt % name,
84 83 initlevel)
85 84 return errorcnt
86 85
87 86 def checkhghelps():
88 87 errorcnt = 0
89 88 for names, sec, doc in helptable:
90 if util.safehasattr(doc, '__call__'):
89 if callable(doc):
91 90 doc = doc()
92 91 errorcnt += checkseclevel(doc,
93 92 '%s help topic' % names[0],
94 93 initlevel_topic)
95 94
96 95 errorcnt += checkcmdtable(table, '%s command', initlevel_cmd)
97 96
98 97 for name in sorted(extensions.enabled().keys() +
99 98 extensions.disabled().keys()):
100 99 mod = extensions.load(None, name, None)
101 100 if not mod.__doc__:
102 101 verbose('skip checking %s extension: no help document' % name)
103 102 continue
104 103 errorcnt += checkseclevel(mod.__doc__,
105 104 '%s extension' % name,
106 105 initlevel_ext)
107 106
108 107 cmdtable = getattr(mod, 'cmdtable', None)
109 108 if cmdtable:
110 109 errorcnt += checkcmdtable(cmdtable,
111 110 '%s command of ' + name + ' extension',
112 111 initlevel_ext_cmd)
113 112 return errorcnt
114 113
115 114 def checkfile(filename, initlevel):
116 115 if filename == '-':
117 116 filename = 'stdin'
118 117 doc = sys.stdin.read()
119 118 else:
120 119 fp = open(filename)
121 120 try:
122 121 doc = fp.read()
123 122 finally:
124 123 fp.close()
125 124
126 125 verbose('checking input from %s with initlevel %d' %
127 126 (filename, initlevel))
128 127 return checkseclevel(doc, 'input from %s' % filename, initlevel)
129 128
130 129 if __name__ == "__main__":
131 130 optparser = optparse.OptionParser("""%prog [options]
132 131
133 132 This checks all help documents of Mercurial (topics, commands,
134 133 extensions and commands of them), if no file is specified by --file
135 134 option.
136 135 """)
137 136 optparser.add_option("-v", "--verbose",
138 137 help="enable additional output",
139 138 action="store_true")
140 139 optparser.add_option("-f", "--file",
141 140 help="filename to read in (or '-' for stdin)",
142 141 action="store", default="")
143 142
144 143 optparser.add_option("-t", "--topic",
145 144 help="parse file as help topic",
146 145 action="store_const", dest="initlevel", const=0)
147 146 optparser.add_option("-c", "--command",
148 147 help="parse file as help of core command",
149 148 action="store_const", dest="initlevel", const=1)
150 149 optparser.add_option("-e", "--extension",
151 150 help="parse file as help of extension",
152 151 action="store_const", dest="initlevel", const=1)
153 152 optparser.add_option("-C", "--extension-command",
154 153 help="parse file as help of extension command",
155 154 action="store_const", dest="initlevel", const=3)
156 155
157 156 optparser.add_option("-l", "--initlevel",
158 157 help="set initial section level manually",
159 158 action="store", type="int", default=0)
160 159
161 160 (options, args) = optparser.parse_args()
162 161
163 162 _verbose = options.verbose
164 163
165 164 if options.file:
166 165 if checkfile(options.file, options.initlevel):
167 166 sys.exit(1)
168 167 else:
169 168 if checkhghelps():
170 169 sys.exit(1)
General Comments 0
You need to be logged in to leave comments. Login now