[PATCH 2 of 2] extensions: use the preprocessed descriptions

Cédric Duval cedricduval at free.fr
Sun Jun 28 05:00:24 CDT 2009


# HG changeset patch
# User Cédric Duval <cedricduval at free.fr>
# Date 1246182817 -7200
# Node ID 8c73755c17e1f8c2414ed26a512dbb45d2902b5d
# Parent  a61ea0b22a3f6f3a0a6bb209b58a64efeed22c01
extensions: use the preprocessed descriptions

This makes use of the preprocessed descriptions (introduced in the previous
changeset) for the display of the 'extensions' topic, and fixes issue1708.

Additionally, moving moduledoc() (not needed by hg) from help.py to setup.py.

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1517,7 +1517,8 @@
                 ui.write(' %-*s   %s\n' % (m, f, util.wrap(h[f], m + 4)))
 
         if name != 'shortlist':
-            exts, maxlength = extensions.enabled()
+            exts = extensions.descriptions(enabled=True)
+            maxlength = max([ len(e) for e in exts.keys() or [''] ])
             ui.write(help.listexts(_('enabled extensions:'), exts, maxlength))
 
         if not ui.quiet:
diff --git a/mercurial/extensions.py b/mercurial/extensions.py
--- a/mercurial/extensions.py
+++ b/mercurial/extensions.py
@@ -6,7 +6,7 @@
 # GNU General Public License version 2, incorporated herein by reference.
 
 import imp, os
-import util, cmdutil, help
+import util, cmdutil, help, __extensions__
 from i18n import _, gettext
 
 _extensions = {}
@@ -118,60 +118,12 @@
     setattr(container, funcname, wrap)
     return origfn
 
-def disabled():
-    '''find disabled extensions from hgext
-    returns a dict of {name: desc}, and the max name length'''
-
-    import hgext
-    extpath = os.path.dirname(os.path.abspath(hgext.__file__))
-
-    exts = {}
-    maxlength = 0
-    for e in os.listdir(extpath):
-
-        if e.endswith('.py'):
-            name = e.rsplit('.', 1)[0]
-            path = os.path.join(extpath, e)
+def descriptions(enabled):
+    '''return a dict of {name: description} for extensions'''
+    def f(name):
+        if enabled:
+            return name in _order
         else:
-            name = e
-            path = os.path.join(extpath, e, '__init__.py')
-            if not os.path.exists(path):
-                continue
-
-        if name in exts or name in _order or name == '__init__':
-            continue
-
-        try:
-            file = open(path)
-        except IOError:
-            continue
-        else:
-            doc = help.moduledoc(file)
-            file.close()
-
-        if doc: # extracting localized synopsis
-            exts[name] = gettext(doc).splitlines()[0]
-        else:
-            exts[name] = _('(no help text available)')
-
-        if len(name) > maxlength:
-            maxlength = len(name)
-
-    return exts, maxlength
-
-def enabled():
-    '''return a dict of {name: desc} of extensions, and the max name length'''
-
-    if not enabled:
-        return {}, 0
-
-    exts = {}
-    maxlength = 0
-    exthelps = []
-    for ename, ext in extensions():
-        doc = (gettext(ext.__doc__) or _('(no help text available)'))
-        ename = ename.split('.')[-1]
-        maxlength = max(len(ename), maxlength)
-        exts[ename] = doc.splitlines(0)[0].strip()
-
-    return exts, maxlength
+            return name not in _order
+    e = [ (k, v) for k, v in __extensions__.descriptions.iteritems() if f(k) ]
+    return dict(e)
diff --git a/mercurial/help.py b/mercurial/help.py
--- a/mercurial/help.py
+++ b/mercurial/help.py
@@ -5,46 +5,17 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2, incorporated herein by reference.
 
-from i18n import _
+from i18n import _, gettext
 import extensions, util
 
 
-def moduledoc(file):
-    '''return the top-level python documentation for the given file
-
-    Loosely inspired by pydoc.source_synopsis(), but rewritten to handle \'''
-    as well as """ and to return the whole text instead of just the synopsis'''
-    result = []
-
-    line = file.readline()
-    while line[:1] == '#' or not line.strip():
-        line = file.readline()
-        if not line: break
-
-    start = line[:3]
-    if start == '"""' or start == "'''":
-        line = line[3:]
-        while line:
-            if line.rstrip().endswith(start):
-                line = line.split(start)[0]
-                if line:
-                    result.append(line)
-                break
-            elif not line:
-                return None # unmatched delimiter
-            result.append(line)
-            line = file.readline()
-    else:
-        return None
-
-    return ''.join(result)
-
 def listexts(header, exts, maxlength):
-    '''return a text listing of the given extensions'''
+    '''return a localized text listing of the given extensions'''
     if not exts:
         return ''
     result = '\n%s\n\n' % header
     for name, desc in sorted(exts.iteritems()):
+        desc = gettext(desc).splitlines()[0]
         desc = util.wrap(desc, maxlength + 4)
         result += ' %s   %s\n' % (name.ljust(maxlength), desc)
     return result
@@ -86,10 +57,12 @@
       hgext.baz = !
     ''')
 
-    exts, maxlength = extensions.enabled()
+    exts = extensions.descriptions(enabled=True)
+    maxlength = max([ len(name) for name in exts.keys() or [''] ])
     doc += listexts(_('enabled extensions:'), exts, maxlength)
 
-    exts, maxlength = extensions.disabled()
+    exts = extensions.descriptions(enabled=False)
+    maxlength = max([ len(name) for name in exts.keys() or [''] ])
     doc += listexts(_('disabled extensions:'), exts, maxlength)
 
     return doc
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -178,15 +178,37 @@
                     exts[e] = path
         return exts
 
+    def moduledoc(self, file):
+        '''return the top-level python documentation for the given file,
+        loosely inspired by pydoc.source_synopsis(), but rewritten to handle
+        \''' as well and to return the whole text (required for gettext)'''
+        result = []
+    
+        line = file.readline()
+        while line[:1] == '#' or not line.strip():
+            line = file.readline()
+            if not line: break
+    
+        start = line[:3]
+        if start == '"""' or start == "'''":
+            line = line[3:]
+            while line:
+                if line.rstrip().endswith(start):
+                    line = line.split(start)[0]
+                    if line:
+                        result.append(line)
+                    break
+                elif not line:
+                    return None # unmatched delimiter
+                result.append(line)
+                line = file.readline()
+        else:
+            return None
+    
+        return ''.join(result)
+
     def descriptions(self):
         '''return a dict of {name: description} for each extension'''
-
-        # XXX temporary kludge to allow importing Mercurial modules while the
-        # C modules can not be used - but help.moduledoc() is no longer used by
-        # hg anyway, and will be moved away from help.py in next changeset...
-        sys.path.append(os.path.join('mercurial', 'pure'))
-        from mercurial.help import moduledoc
-
         exts = {}
         for name, path in self.paths().iteritems():
             try:
@@ -194,7 +216,7 @@
             except IOError:
                 continue
             else:
-                exts[name] = moduledoc(file) or '(no help text available)'
+                exts[name] = self.moduledoc(file) or '(no help text available)'
                 file.close()
         return exts
 
diff --git a/tests/test-extension.out b/tests/test-extension.out
--- a/tests/test-extension.out
+++ b/tests/test-extension.out
@@ -29,10 +29,6 @@
  debugfoobar:
       yet another debug command
 
-enabled extensions:
-
- debugextension   only debugcommands
-
 global options:
  -R --repository      repository root directory or symbolic path name
     --cwd             change working directory



More information about the Mercurial-devel mailing list