[PATCH 2 of 3] help: moved function from gendoc to help

Erik Zielke ez at aragost.com
Fri Oct 22 08:39:23 UTC 2010


# HG changeset patch
# User Erik Zielke <ez at aragost.com>
# Date 1287735682 -7200
# Node ID e4bbb6b47f03239bab597977144c9c6680561959
# Parent  cb28fd6467c4985fddb91c298ac9886830edf201
help: moved function from gendoc to help

Moved functions helpful in printing a single command from gendoc to
help, to enable getting the restructured text for a single command
elsewhere then when generating documentation.

diff -r cb28fd6467c4 -r e4bbb6b47f03 doc/gendoc.py
--- a/doc/gendoc.py	Fri Oct 22 10:21:20 2010 +0200
+++ b/doc/gendoc.py	Fri Oct 22 10:21:22 2010 +0200
@@ -1,4 +1,4 @@
-import os, sys, textwrap
+import os, sys
 # import from the live mercurial repo
 sys.path.insert(0, "..")
 # fall back to pure modules if required C extensions are not available
@@ -7,61 +7,10 @@
 from mercurial import encoding
 from mercurial.commands import table, globalopts
 from mercurial.i18n import _
+from mercurial import help as helpmod
 from mercurial.help import helptable
 from mercurial import extensions
 
-def get_desc(docstr):
-    if not docstr:
-        return "", ""
-    # sanitize
-    docstr = docstr.strip("\n")
-    docstr = docstr.rstrip()
-    shortdesc = docstr.splitlines()[0].strip()
-
-    i = docstr.find("\n")
-    if i != -1:
-        desc = docstr[i + 2:]
-    else:
-        desc = shortdesc
-
-    desc = textwrap.dedent(desc)
-
-    return (shortdesc, desc)
-
-def get_opts(opts):
-    for opt in opts:
-        if len(opt) == 5:
-            shortopt, longopt, default, desc, optlabel = opt
-        else:
-            shortopt, longopt, default, desc = opt
-        allopts = []
-        if shortopt:
-            allopts.append("-%s" % shortopt)
-        if longopt:
-            allopts.append("--%s" % longopt)
-        desc += default and _(" (default: %s)") % default or ""
-        yield(", ".join(allopts), desc)
-
-def get_cmd(cmd, cmdtable):
-    d = {}
-    attr = cmdtable[cmd]
-    cmds = cmd.lstrip("^").split("|")
-
-    d['cmd'] = cmds[0]
-    d['aliases'] = cmd.split("|")[1:]
-    d['desc'] = get_desc(attr[0].__doc__)
-    d['opts'] = list(get_opts(attr[1]))
-
-    s = 'hg ' + cmds[0]
-    if len(attr) > 2:
-        if not attr[2].startswith('hg'):
-            s += ' ' + attr[2]
-        else:
-            s = attr[2]
-    d['synopsis'] = s.strip()
-
-    return d
-
 def section(ui, s):
     ui.write("%s\n%s\n\n" % (s, "-" * encoding.colwidth(s)))
 
@@ -78,7 +27,7 @@
 def show_doc(ui):
     # print options
     section(ui, _("Options"))
-    for optstr, desc in get_opts(globalopts):
+    for optstr, desc in helpmod.get_opts(globalopts):
         ui.write("%s\n%s\n\n" % (optstr, desc))
 
     # print cmds
@@ -116,20 +65,14 @@
             commandsprinter(ui, cmdtable, subsubsubsection)
 
 def commandsprinter(ui, cmdtable, sectionfunc):
-    h = {}
-    for c, attr in cmdtable.items():
-        f = c.split("|")[0]
-        f = f.lstrip("^")
-        h[f] = c
-    cmds = h.keys()
-    cmds.sort()
+    h, cmds = helpmod.cleancommands(cmdtable)
 
     for f in cmds:
         if f.startswith("debug"):
             continue
-        d = get_cmd(h[f], cmdtable)
+        d = helpmod.get_cmd(h[f], cmdtable)
         sectionfunc(ui, d['cmd'])
-        commandprinter(ui, d)
+        helpmod.commandprinter(ui, d)
         # synopsis
 
 def commandprinter(ui, cmd):
diff -r cb28fd6467c4 -r e4bbb6b47f03 mercurial/help.py
--- a/mercurial/help.py	Fri Oct 22 10:21:20 2010 +0200
+++ b/mercurial/help.py	Fri Oct 22 10:21:22 2010 +0200
@@ -6,7 +6,7 @@
 # GNU General Public License version 2 or any later version.
 
 from i18n import gettext, _
-import sys, os
+import sys, os, textwrap
 import extensions
 
 
@@ -82,6 +82,93 @@
         return gettext(open(path).read())
     return loader
 
+def commandprinter(ui, cmd):
+    ui.write('')
+    ui.write("``%s``\n" % cmd['synopsis'].replace("hg ","", 1))
+    ui.write("\n")
+    # description
+    ui.write("%s\n\n" % cmd['desc'][1])
+    # options
+    opt_output = list(cmd['opts'])
+    if opt_output:
+        opts_len = max([len(line[0]) for line in opt_output])
+        ui.write(_("options:\n\n"))
+        for optstr, desc in opt_output:
+            if desc:
+                s = "%-*s  %s" % (opts_len, optstr, desc)
+            else:
+                s = optstr
+            ui.write("%s\n" % s)
+        ui.write("\n")
+    # aliases
+    if cmd['aliases']:
+        ui.write(_("aliases: %s\n\n") % " ".join(cmd['aliases']))
+
+def get_cmd(cmd, cmdtable):
+    """Returns a dictionary with values for the given cmd from the given cmdtable"""
+    d = {}
+    attr = cmdtable[cmd]
+    cmds = cmd.lstrip("^").split("|")
+
+    d['cmd'] = cmds[0]
+    d['aliases'] = cmd.split("|")[1:]
+    d['desc'] = get_desc(attr[0].__doc__)
+    d['opts'] = list(get_opts(attr[1]))
+
+    s = 'hg ' + cmds[0]
+    if len(attr) > 2:
+        if not attr[2].startswith('hg'):
+            s += ' ' + attr[2]
+        else:
+            s = attr[2]
+    d['synopsis'] = s.strip()
+
+    return d
+
+def get_desc(docstr):
+    """Returns a tuple with short and long description, based on the given docstr"""
+    if not docstr:
+        return "", ""
+    # sanitize
+    docstr = docstr.strip("\n")
+    docstr = docstr.rstrip()
+    shortdesc = docstr.splitlines()[0].strip()
+
+    i = docstr.find("\n")
+    if i != -1:
+        desc = docstr[i + 2:]
+    else:
+        desc = shortdesc
+
+    desc = textwrap.dedent(desc)
+
+    return (shortdesc, desc)
+
+def get_opts(opts):
+    for opt in opts:
+        if len(opt) == 5:
+            shortopt, longopt, default, desc, optlabel = opt
+        else:
+            shortopt, longopt, default, desc = opt
+        allopts = []
+        if shortopt:
+            allopts.append("-%s" % shortopt)
+        if longopt:
+            allopts.append("--%s" % longopt)
+        desc += default and _(" (default: %s)") % default or ""
+        yield(", ".join(allopts), desc)
+
+
+def cleancommands(cmdtable):
+    h = {}
+    for c, attr in cmdtable.items():
+        f = c.split("|")[0]
+        f = f.lstrip("^")
+        h[f] = c
+    cmds = h.keys()
+    cmds.sort()
+    return h, cmds
+
 helptable = [
     (["config", "hgrc"], _("Configuration Files"), loaddoc('config')),
     (["dates"], _("Date Formats"), loaddoc('dates')),



More information about the Mercurial-devel mailing list