##// END OF EJS Templates
gendoc: restore use of callable() since it was readded in Python 3.2
Augie Fackler -
r21793:e0b29a0c default
parent child Browse files
Show More
@@ -1,205 +1,204 b''
1 """usage: %s DOC ...
1 """usage: %s DOC ...
2
2
3 where DOC is the name of a document
3 where DOC is the name of a document
4 """
4 """
5
5
6 import os, sys, textwrap
6 import os, sys, textwrap
7 # import from the live mercurial repo
7 # import from the live mercurial repo
8 sys.path.insert(0, "..")
8 sys.path.insert(0, "..")
9 # fall back to pure modules if required C extensions are not available
9 # fall back to pure modules if required C extensions are not available
10 sys.path.append(os.path.join('..', 'mercurial', 'pure'))
10 sys.path.append(os.path.join('..', 'mercurial', 'pure'))
11 from mercurial import demandimport; demandimport.enable()
11 from mercurial import demandimport; demandimport.enable()
12 from mercurial import minirst
12 from mercurial import minirst
13 from mercurial.commands import table, globalopts
13 from mercurial.commands import table, globalopts
14 from mercurial.i18n import gettext, _
14 from mercurial.i18n import gettext, _
15 from mercurial.help import helptable, loaddoc
15 from mercurial.help import helptable, loaddoc
16 from mercurial import extensions
16 from mercurial import extensions
17 from mercurial import util
18
17
19 def get_desc(docstr):
18 def get_desc(docstr):
20 if not docstr:
19 if not docstr:
21 return "", ""
20 return "", ""
22 # sanitize
21 # sanitize
23 docstr = docstr.strip("\n")
22 docstr = docstr.strip("\n")
24 docstr = docstr.rstrip()
23 docstr = docstr.rstrip()
25 shortdesc = docstr.splitlines()[0].strip()
24 shortdesc = docstr.splitlines()[0].strip()
26
25
27 i = docstr.find("\n")
26 i = docstr.find("\n")
28 if i != -1:
27 if i != -1:
29 desc = docstr[i + 2:]
28 desc = docstr[i + 2:]
30 else:
29 else:
31 desc = shortdesc
30 desc = shortdesc
32
31
33 desc = textwrap.dedent(desc)
32 desc = textwrap.dedent(desc)
34
33
35 return (shortdesc, desc)
34 return (shortdesc, desc)
36
35
37 def get_opts(opts):
36 def get_opts(opts):
38 for opt in opts:
37 for opt in opts:
39 if len(opt) == 5:
38 if len(opt) == 5:
40 shortopt, longopt, default, desc, optlabel = opt
39 shortopt, longopt, default, desc, optlabel = opt
41 else:
40 else:
42 shortopt, longopt, default, desc = opt
41 shortopt, longopt, default, desc = opt
43 optlabel = _("VALUE")
42 optlabel = _("VALUE")
44 allopts = []
43 allopts = []
45 if shortopt:
44 if shortopt:
46 allopts.append("-%s" % shortopt)
45 allopts.append("-%s" % shortopt)
47 if longopt:
46 if longopt:
48 allopts.append("--%s" % longopt)
47 allopts.append("--%s" % longopt)
49 if isinstance(default, list):
48 if isinstance(default, list):
50 allopts[-1] += " <%s[+]>" % optlabel
49 allopts[-1] += " <%s[+]>" % optlabel
51 elif (default is not None) and not isinstance(default, bool):
50 elif (default is not None) and not isinstance(default, bool):
52 allopts[-1] += " <%s>" % optlabel
51 allopts[-1] += " <%s>" % optlabel
53 if '\n' in desc:
52 if '\n' in desc:
54 # only remove line breaks and indentation
53 # only remove line breaks and indentation
55 desc = ' '.join(l.lstrip() for l in desc.split('\n'))
54 desc = ' '.join(l.lstrip() for l in desc.split('\n'))
56 desc += default and _(" (default: %s)") % default or ""
55 desc += default and _(" (default: %s)") % default or ""
57 yield (", ".join(allopts), desc)
56 yield (", ".join(allopts), desc)
58
57
59 def get_cmd(cmd, cmdtable):
58 def get_cmd(cmd, cmdtable):
60 d = {}
59 d = {}
61 attr = cmdtable[cmd]
60 attr = cmdtable[cmd]
62 cmds = cmd.lstrip("^").split("|")
61 cmds = cmd.lstrip("^").split("|")
63
62
64 d['cmd'] = cmds[0]
63 d['cmd'] = cmds[0]
65 d['aliases'] = cmd.split("|")[1:]
64 d['aliases'] = cmd.split("|")[1:]
66 d['desc'] = get_desc(gettext(attr[0].__doc__))
65 d['desc'] = get_desc(gettext(attr[0].__doc__))
67 d['opts'] = list(get_opts(attr[1]))
66 d['opts'] = list(get_opts(attr[1]))
68
67
69 s = 'hg ' + cmds[0]
68 s = 'hg ' + cmds[0]
70 if len(attr) > 2:
69 if len(attr) > 2:
71 if not attr[2].startswith('hg'):
70 if not attr[2].startswith('hg'):
72 s += ' ' + attr[2]
71 s += ' ' + attr[2]
73 else:
72 else:
74 s = attr[2]
73 s = attr[2]
75 d['synopsis'] = s.strip()
74 d['synopsis'] = s.strip()
76
75
77 return d
76 return d
78
77
79 def showdoc(ui):
78 def showdoc(ui):
80 # print options
79 # print options
81 ui.write(minirst.section(_("Options")))
80 ui.write(minirst.section(_("Options")))
82 multioccur = False
81 multioccur = False
83 for optstr, desc in get_opts(globalopts):
82 for optstr, desc in get_opts(globalopts):
84 ui.write("%s\n %s\n\n" % (optstr, desc))
83 ui.write("%s\n %s\n\n" % (optstr, desc))
85 if optstr.endswith("[+]>"):
84 if optstr.endswith("[+]>"):
86 multioccur = True
85 multioccur = True
87 if multioccur:
86 if multioccur:
88 ui.write(_("\n[+] marked option can be specified multiple times\n"))
87 ui.write(_("\n[+] marked option can be specified multiple times\n"))
89 ui.write("\n")
88 ui.write("\n")
90
89
91 # print cmds
90 # print cmds
92 ui.write(minirst.section(_("Commands")))
91 ui.write(minirst.section(_("Commands")))
93 commandprinter(ui, table, minirst.subsection)
92 commandprinter(ui, table, minirst.subsection)
94
93
95 # print help topics
94 # print help topics
96 # The config help topic is included in the hgrc.5 man page.
95 # The config help topic is included in the hgrc.5 man page.
97 helpprinter(ui, helptable, minirst.section, exclude=['config'])
96 helpprinter(ui, helptable, minirst.section, exclude=['config'])
98
97
99 ui.write(minirst.section(_("Extensions")))
98 ui.write(minirst.section(_("Extensions")))
100 ui.write(_("This section contains help for extensions that are "
99 ui.write(_("This section contains help for extensions that are "
101 "distributed together with Mercurial. Help for other "
100 "distributed together with Mercurial. Help for other "
102 "extensions is available in the help system."))
101 "extensions is available in the help system."))
103 ui.write("\n\n"
102 ui.write("\n\n"
104 ".. contents::\n"
103 ".. contents::\n"
105 " :class: htmlonly\n"
104 " :class: htmlonly\n"
106 " :local:\n"
105 " :local:\n"
107 " :depth: 1\n\n")
106 " :depth: 1\n\n")
108
107
109 for extensionname in sorted(allextensionnames()):
108 for extensionname in sorted(allextensionnames()):
110 mod = extensions.load(None, extensionname, None)
109 mod = extensions.load(None, extensionname, None)
111 ui.write(minirst.subsection(extensionname))
110 ui.write(minirst.subsection(extensionname))
112 ui.write("%s\n\n" % gettext(mod.__doc__))
111 ui.write("%s\n\n" % gettext(mod.__doc__))
113 cmdtable = getattr(mod, 'cmdtable', None)
112 cmdtable = getattr(mod, 'cmdtable', None)
114 if cmdtable:
113 if cmdtable:
115 ui.write(minirst.subsubsection(_('Commands')))
114 ui.write(minirst.subsubsection(_('Commands')))
116 commandprinter(ui, cmdtable, minirst.subsubsubsection)
115 commandprinter(ui, cmdtable, minirst.subsubsubsection)
117
116
118 def showtopic(ui, topic):
117 def showtopic(ui, topic):
119 extrahelptable = [
118 extrahelptable = [
120 (["common"], '', loaddoc('common')),
119 (["common"], '', loaddoc('common')),
121 (["hg.1"], '', loaddoc('hg.1')),
120 (["hg.1"], '', loaddoc('hg.1')),
122 (["hgignore.5"], '', loaddoc('hgignore.5')),
121 (["hgignore.5"], '', loaddoc('hgignore.5')),
123 (["hgrc.5"], '', loaddoc('hgrc.5')),
122 (["hgrc.5"], '', loaddoc('hgrc.5')),
124 (["hgignore.5.gendoc"], '', loaddoc('hgignore')),
123 (["hgignore.5.gendoc"], '', loaddoc('hgignore')),
125 (["hgrc.5.gendoc"], '', loaddoc('config')),
124 (["hgrc.5.gendoc"], '', loaddoc('config')),
126 ]
125 ]
127 helpprinter(ui, helptable + extrahelptable, None, include=[topic])
126 helpprinter(ui, helptable + extrahelptable, None, include=[topic])
128
127
129 def helpprinter(ui, helptable, sectionfunc, include=[], exclude=[]):
128 def helpprinter(ui, helptable, sectionfunc, include=[], exclude=[]):
130 for names, sec, doc in helptable:
129 for names, sec, doc in helptable:
131 if exclude and names[0] in exclude:
130 if exclude and names[0] in exclude:
132 continue
131 continue
133 if include and names[0] not in include:
132 if include and names[0] not in include:
134 continue
133 continue
135 for name in names:
134 for name in names:
136 ui.write(".. _%s:\n" % name)
135 ui.write(".. _%s:\n" % name)
137 ui.write("\n")
136 ui.write("\n")
138 if sectionfunc:
137 if sectionfunc:
139 ui.write(sectionfunc(sec))
138 ui.write(sectionfunc(sec))
140 if util.safehasattr(doc, '__call__'):
139 if callable(doc):
141 doc = doc()
140 doc = doc()
142 ui.write(doc)
141 ui.write(doc)
143 ui.write("\n")
142 ui.write("\n")
144
143
145 def commandprinter(ui, cmdtable, sectionfunc):
144 def commandprinter(ui, cmdtable, sectionfunc):
146 h = {}
145 h = {}
147 for c, attr in cmdtable.items():
146 for c, attr in cmdtable.items():
148 f = c.split("|")[0]
147 f = c.split("|")[0]
149 f = f.lstrip("^")
148 f = f.lstrip("^")
150 h[f] = c
149 h[f] = c
151 cmds = h.keys()
150 cmds = h.keys()
152 cmds.sort()
151 cmds.sort()
153
152
154 for f in cmds:
153 for f in cmds:
155 if f.startswith("debug"):
154 if f.startswith("debug"):
156 continue
155 continue
157 d = get_cmd(h[f], cmdtable)
156 d = get_cmd(h[f], cmdtable)
158 ui.write(sectionfunc(d['cmd']))
157 ui.write(sectionfunc(d['cmd']))
159 # short description
158 # short description
160 ui.write(d['desc'][0])
159 ui.write(d['desc'][0])
161 # synopsis
160 # synopsis
162 ui.write("::\n\n")
161 ui.write("::\n\n")
163 synopsislines = d['synopsis'].splitlines()
162 synopsislines = d['synopsis'].splitlines()
164 for line in synopsislines:
163 for line in synopsislines:
165 # some commands (such as rebase) have a multi-line
164 # some commands (such as rebase) have a multi-line
166 # synopsis
165 # synopsis
167 ui.write(" %s\n" % line)
166 ui.write(" %s\n" % line)
168 ui.write('\n')
167 ui.write('\n')
169 # description
168 # description
170 ui.write("%s\n\n" % d['desc'][1])
169 ui.write("%s\n\n" % d['desc'][1])
171 # options
170 # options
172 opt_output = list(d['opts'])
171 opt_output = list(d['opts'])
173 if opt_output:
172 if opt_output:
174 opts_len = max([len(line[0]) for line in opt_output])
173 opts_len = max([len(line[0]) for line in opt_output])
175 ui.write(_("Options:\n\n"))
174 ui.write(_("Options:\n\n"))
176 multioccur = False
175 multioccur = False
177 for optstr, desc in opt_output:
176 for optstr, desc in opt_output:
178 if desc:
177 if desc:
179 s = "%-*s %s" % (opts_len, optstr, desc)
178 s = "%-*s %s" % (opts_len, optstr, desc)
180 else:
179 else:
181 s = optstr
180 s = optstr
182 ui.write("%s\n" % s)
181 ui.write("%s\n" % s)
183 if optstr.endswith("[+]>"):
182 if optstr.endswith("[+]>"):
184 multioccur = True
183 multioccur = True
185 if multioccur:
184 if multioccur:
186 ui.write(_("\n[+] marked option can be specified"
185 ui.write(_("\n[+] marked option can be specified"
187 " multiple times\n"))
186 " multiple times\n"))
188 ui.write("\n")
187 ui.write("\n")
189 # aliases
188 # aliases
190 if d['aliases']:
189 if d['aliases']:
191 ui.write(_(" aliases: %s\n\n") % " ".join(d['aliases']))
190 ui.write(_(" aliases: %s\n\n") % " ".join(d['aliases']))
192
191
193
192
194 def allextensionnames():
193 def allextensionnames():
195 return extensions.enabled().keys() + extensions.disabled().keys()
194 return extensions.enabled().keys() + extensions.disabled().keys()
196
195
197 if __name__ == "__main__":
196 if __name__ == "__main__":
198 doc = 'hg.1.gendoc'
197 doc = 'hg.1.gendoc'
199 if len(sys.argv) > 1:
198 if len(sys.argv) > 1:
200 doc = sys.argv[1]
199 doc = sys.argv[1]
201
200
202 if doc == 'hg.1.gendoc':
201 if doc == 'hg.1.gendoc':
203 showdoc(sys.stdout)
202 showdoc(sys.stdout)
204 else:
203 else:
205 showtopic(sys.stdout, sys.argv[1])
204 showtopic(sys.stdout, sys.argv[1])
General Comments 0
You need to be logged in to leave comments. Login now