Mercurial > hg
comparison doc/gendoc.py @ 43076:2372284d9457
formatting: blacken the codebase
This is using my patch to black
(https://github.com/psf/black/pull/826) so we don't un-wrap collection
literals.
Done with:
hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S
# skip-blame mass-reformatting only
# no-check-commit reformats foo_bar functions
Differential Revision: https://phab.mercurial-scm.org/D6971
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 06 Oct 2019 09:45:02 -0400 |
parents | a42cc325b682 |
children | 313e3a279828 |
comparison
equal
deleted
inserted
replaced
43075:57875cf423c9 | 43076:2372284d9457 |
---|---|
10 import sys | 10 import sys |
11 import textwrap | 11 import textwrap |
12 | 12 |
13 try: | 13 try: |
14 import msvcrt | 14 import msvcrt |
15 | |
15 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) | 16 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) |
16 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) | 17 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) |
17 except ImportError: | 18 except ImportError: |
18 pass | 19 pass |
19 | 20 |
20 # This script is executed during installs and may not have C extensions | 21 # This script is executed during installs and may not have C extensions |
21 # available. Relax C module requirements. | 22 # available. Relax C module requirements. |
22 os.environ[r'HGMODULEPOLICY'] = r'allow' | 23 os.environ[r'HGMODULEPOLICY'] = r'allow' |
23 # import from the live mercurial repo | 24 # import from the live mercurial repo |
24 sys.path.insert(0, r"..") | 25 sys.path.insert(0, r"..") |
25 from mercurial import demandimport; demandimport.enable() | 26 from mercurial import demandimport |
27 | |
28 demandimport.enable() | |
26 # Load util so that the locale path is set by i18n.setdatapath() before | 29 # Load util so that the locale path is set by i18n.setdatapath() before |
27 # calling _(). | 30 # calling _(). |
28 from mercurial import util | 31 from mercurial import util |
32 | |
29 util.datapath | 33 util.datapath |
30 from mercurial import ( | 34 from mercurial import ( |
31 commands, | 35 commands, |
32 encoding, | 36 encoding, |
33 extensions, | 37 extensions, |
44 table = commands.table | 48 table = commands.table |
45 globalopts = commands.globalopts | 49 globalopts = commands.globalopts |
46 helptable = help.helptable | 50 helptable = help.helptable |
47 loaddoc = help.loaddoc | 51 loaddoc = help.loaddoc |
48 | 52 |
53 | |
49 def get_desc(docstr): | 54 def get_desc(docstr): |
50 if not docstr: | 55 if not docstr: |
51 return b"", b"" | 56 return b"", b"" |
52 # sanitize | 57 # sanitize |
53 docstr = docstr.strip(b"\n") | 58 docstr = docstr.strip(b"\n") |
54 docstr = docstr.rstrip() | 59 docstr = docstr.rstrip() |
55 shortdesc = docstr.splitlines()[0].strip() | 60 shortdesc = docstr.splitlines()[0].strip() |
56 | 61 |
57 i = docstr.find(b"\n") | 62 i = docstr.find(b"\n") |
58 if i != -1: | 63 if i != -1: |
59 desc = docstr[i + 2:] | 64 desc = docstr[i + 2 :] |
60 else: | 65 else: |
61 desc = shortdesc | 66 desc = shortdesc |
62 | 67 |
63 desc = textwrap.dedent(desc.decode('latin1')).encode('latin1') | 68 desc = textwrap.dedent(desc.decode('latin1')).encode('latin1') |
64 | 69 |
65 return (shortdesc, desc) | 70 return (shortdesc, desc) |
71 | |
66 | 72 |
67 def get_opts(opts): | 73 def get_opts(opts): |
68 for opt in opts: | 74 for opt in opts: |
69 if len(opt) == 5: | 75 if len(opt) == 5: |
70 shortopt, longopt, default, desc, optlabel = opt | 76 shortopt, longopt, default, desc, optlabel = opt |
84 # only remove line breaks and indentation | 90 # only remove line breaks and indentation |
85 desc = b' '.join(l.lstrip() for l in desc.split(b'\n')) | 91 desc = b' '.join(l.lstrip() for l in desc.split(b'\n')) |
86 desc += default and _(b" (default: %s)") % bytes(default) or b"" | 92 desc += default and _(b" (default: %s)") % bytes(default) or b"" |
87 yield (b", ".join(allopts), desc) | 93 yield (b", ".join(allopts), desc) |
88 | 94 |
95 | |
89 def get_cmd(cmd, cmdtable): | 96 def get_cmd(cmd, cmdtable): |
90 d = {} | 97 d = {} |
91 attr = cmdtable[cmd] | 98 attr = cmdtable[cmd] |
92 cmds = cmd.lstrip(b"^").split(b"|") | 99 cmds = cmd.lstrip(b"^").split(b"|") |
93 | 100 |
103 else: | 110 else: |
104 s = attr[2] | 111 s = attr[2] |
105 d[b'synopsis'] = s.strip() | 112 d[b'synopsis'] = s.strip() |
106 | 113 |
107 return d | 114 return d |
115 | |
108 | 116 |
109 def showdoc(ui): | 117 def showdoc(ui): |
110 # print options | 118 # print options |
111 ui.write(minirst.section(_(b"Options"))) | 119 ui.write(minirst.section(_(b"Options"))) |
112 multioccur = False | 120 multioccur = False |
125 # print help topics | 133 # print help topics |
126 # The config help topic is included in the hgrc.5 man page. | 134 # The config help topic is included in the hgrc.5 man page. |
127 helpprinter(ui, helptable, minirst.section, exclude=[b'config']) | 135 helpprinter(ui, helptable, minirst.section, exclude=[b'config']) |
128 | 136 |
129 ui.write(minirst.section(_(b"Extensions"))) | 137 ui.write(minirst.section(_(b"Extensions"))) |
130 ui.write(_(b"This section contains help for extensions that are " | 138 ui.write( |
131 b"distributed together with Mercurial. Help for other " | 139 _( |
132 b"extensions is available in the help system.")) | 140 b"This section contains help for extensions that are " |
133 ui.write((b"\n\n" | 141 b"distributed together with Mercurial. Help for other " |
134 b".. contents::\n" | 142 b"extensions is available in the help system." |
135 b" :class: htmlonly\n" | 143 ) |
136 b" :local:\n" | 144 ) |
137 b" :depth: 1\n\n")) | 145 ui.write( |
146 ( | |
147 b"\n\n" | |
148 b".. contents::\n" | |
149 b" :class: htmlonly\n" | |
150 b" :local:\n" | |
151 b" :depth: 1\n\n" | |
152 ) | |
153 ) | |
138 | 154 |
139 for extensionname in sorted(allextensionnames()): | 155 for extensionname in sorted(allextensionnames()): |
140 mod = extensions.load(ui, extensionname, None) | 156 mod = extensions.load(ui, extensionname, None) |
141 ui.write(minirst.subsection(extensionname)) | 157 ui.write(minirst.subsection(extensionname)) |
142 ui.write(b"%s\n\n" % gettext(pycompat.getdoc(mod))) | 158 ui.write(b"%s\n\n" % gettext(pycompat.getdoc(mod))) |
143 cmdtable = getattr(mod, 'cmdtable', None) | 159 cmdtable = getattr(mod, 'cmdtable', None) |
144 if cmdtable: | 160 if cmdtable: |
145 ui.write(minirst.subsubsection(_(b'Commands'))) | 161 ui.write(minirst.subsubsection(_(b'Commands'))) |
146 commandprinter(ui, cmdtable, minirst.subsubsubsection, | 162 commandprinter( |
147 minirst.subsubsubsubsection) | 163 ui, |
164 cmdtable, | |
165 minirst.subsubsubsection, | |
166 minirst.subsubsubsubsection, | |
167 ) | |
168 | |
148 | 169 |
149 def showtopic(ui, topic): | 170 def showtopic(ui, topic): |
150 extrahelptable = [ | 171 extrahelptable = [ |
151 ([b"common"], b'', loaddoc(b'common'), help.TOPIC_CATEGORY_MISC), | 172 ([b"common"], b'', loaddoc(b'common'), help.TOPIC_CATEGORY_MISC), |
152 ([b"hg.1"], b'', loaddoc(b'hg.1'), help.TOPIC_CATEGORY_CONFIG), | 173 ([b"hg.1"], b'', loaddoc(b'hg.1'), help.TOPIC_CATEGORY_CONFIG), |
153 ([b"hg-ssh.8"], b'', loaddoc(b'hg-ssh.8'), help.TOPIC_CATEGORY_CONFIG), | 174 ([b"hg-ssh.8"], b'', loaddoc(b'hg-ssh.8'), help.TOPIC_CATEGORY_CONFIG), |
154 ([b"hgignore.5"], b'', loaddoc(b'hgignore.5'), | 175 ( |
155 help.TOPIC_CATEGORY_CONFIG), | 176 [b"hgignore.5"], |
177 b'', | |
178 loaddoc(b'hgignore.5'), | |
179 help.TOPIC_CATEGORY_CONFIG, | |
180 ), | |
156 ([b"hgrc.5"], b'', loaddoc(b'hgrc.5'), help.TOPIC_CATEGORY_CONFIG), | 181 ([b"hgrc.5"], b'', loaddoc(b'hgrc.5'), help.TOPIC_CATEGORY_CONFIG), |
157 ([b"hgignore.5.gendoc"], b'', loaddoc(b'hgignore'), | 182 ( |
158 help.TOPIC_CATEGORY_CONFIG), | 183 [b"hgignore.5.gendoc"], |
159 ([b"hgrc.5.gendoc"], b'', loaddoc(b'config'), | 184 b'', |
160 help.TOPIC_CATEGORY_CONFIG), | 185 loaddoc(b'hgignore'), |
186 help.TOPIC_CATEGORY_CONFIG, | |
187 ), | |
188 ( | |
189 [b"hgrc.5.gendoc"], | |
190 b'', | |
191 loaddoc(b'config'), | |
192 help.TOPIC_CATEGORY_CONFIG, | |
193 ), | |
161 ] | 194 ] |
162 helpprinter(ui, helptable + extrahelptable, None, include=[topic]) | 195 helpprinter(ui, helptable + extrahelptable, None, include=[topic]) |
196 | |
163 | 197 |
164 def helpprinter(ui, helptable, sectionfunc, include=[], exclude=[]): | 198 def helpprinter(ui, helptable, sectionfunc, include=[], exclude=[]): |
165 for h in helptable: | 199 for h in helptable: |
166 names, sec, doc = h[0:3] | 200 names, sec, doc = h[0:3] |
167 if exclude and names[0] in exclude: | 201 if exclude and names[0] in exclude: |
176 if callable(doc): | 210 if callable(doc): |
177 doc = doc(ui) | 211 doc = doc(ui) |
178 ui.write(doc) | 212 ui.write(doc) |
179 ui.write(b"\n") | 213 ui.write(b"\n") |
180 | 214 |
215 | |
181 def commandprinter(ui, cmdtable, sectionfunc, subsectionfunc): | 216 def commandprinter(ui, cmdtable, sectionfunc, subsectionfunc): |
182 """Render restructuredtext describing a list of commands and their | 217 """Render restructuredtext describing a list of commands and their |
183 documentations, grouped by command category. | 218 documentations, grouped by command category. |
184 | 219 |
185 Args: | 220 Args: |
220 # If a command category wasn't registered, the command won't get | 255 # If a command category wasn't registered, the command won't get |
221 # rendered below, so we raise an AssertionError. | 256 # rendered below, so we raise an AssertionError. |
222 if helpcategory(cmd) not in cmdsbycategory: | 257 if helpcategory(cmd) not in cmdsbycategory: |
223 raise AssertionError( | 258 raise AssertionError( |
224 "The following command did not register its (category) in " | 259 "The following command did not register its (category) in " |
225 "help.CATEGORY_ORDER: %s (%s)" % (cmd, helpcategory(cmd))) | 260 "help.CATEGORY_ORDER: %s (%s)" % (cmd, helpcategory(cmd)) |
261 ) | |
226 cmdsbycategory[helpcategory(cmd)].append(cmd) | 262 cmdsbycategory[helpcategory(cmd)].append(cmd) |
227 | 263 |
228 # Print the help for each command. We present the commands grouped by | 264 # Print the help for each command. We present the commands grouped by |
229 # category, and we use help.CATEGORY_ORDER as a guide for a helpful order | 265 # category, and we use help.CATEGORY_ORDER as a guide for a helpful order |
230 # in which to present the categories. | 266 # in which to present the categories. |
268 s = optstr | 304 s = optstr |
269 ui.write(b"%s\n" % s) | 305 ui.write(b"%s\n" % s) |
270 if optstr.endswith(b"[+]>"): | 306 if optstr.endswith(b"[+]>"): |
271 multioccur = True | 307 multioccur = True |
272 if multioccur: | 308 if multioccur: |
273 ui.write(_(b"\n[+] marked option can be specified" | 309 ui.write( |
274 b" multiple times\n")) | 310 _( |
311 b"\n[+] marked option can be specified" | |
312 b" multiple times\n" | |
313 ) | |
314 ) | |
275 ui.write(b"\n") | 315 ui.write(b"\n") |
276 # aliases | 316 # aliases |
277 if d[b'aliases']: | 317 if d[b'aliases']: |
278 ui.write(_(b" aliases: %s\n\n") % b" ".join(d[b'aliases'])) | 318 ui.write(_(b" aliases: %s\n\n") % b" ".join(d[b'aliases'])) |
279 | 319 |
320 | |
280 def allextensionnames(): | 321 def allextensionnames(): |
281 return set(extensions.enabled().keys()) | set(extensions.disabled().keys()) | 322 return set(extensions.enabled().keys()) | set(extensions.disabled().keys()) |
323 | |
282 | 324 |
283 if __name__ == "__main__": | 325 if __name__ == "__main__": |
284 doc = b'hg.1.gendoc' | 326 doc = b'hg.1.gendoc' |
285 if len(sys.argv) > 1: | 327 if len(sys.argv) > 1: |
286 doc = encoding.strtolocal(sys.argv[1]) | 328 doc = encoding.strtolocal(sys.argv[1]) |