diff mercurial/extensions.py @ 8871:20a25042fadc

extensions: move extensions listing functions from mercurial.help Includes some small fix-ups to comments in enabled() and disabled().
author Cédric Duval <cedricduval@free.fr>
date Sun, 21 Jun 2009 16:32:00 +0200
parents 46293a0c7e9f
children d0c0013f8713
line wrap: on
line diff
--- a/mercurial/extensions.py	Sun Jun 21 16:27:07 2009 +0200
+++ b/mercurial/extensions.py	Sun Jun 21 16:32:00 2009 +0200
@@ -5,9 +5,9 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2, incorporated herein by reference.
 
-import imp, os
-import util, cmdutil
-from i18n import _
+import imp, os, sys
+import util, cmdutil, help
+from i18n import _, gettext
 
 _extensions = {}
 _order = []
@@ -117,3 +117,75 @@
     origfn = getattr(container, funcname)
     setattr(container, funcname, wrap)
     return origfn
+
+def pathdirs():
+    '''convert sys.path into a list of absolute, existing, unique paths
+    (taken from pydoc)'''
+    dirs = []
+    normdirs = []
+    for dir in sys.path:
+        dir = os.path.abspath(dir or '.')
+        normdir = os.path.normcase(dir)
+        if normdir not in normdirs and os.path.isdir(dir):
+            dirs.append(dir)
+            normdirs.append(normdir)
+    return dirs
+
+def disabled():
+    '''find disabled extensions from hgext
+    returns a dict of {name: desc}, and the max name length'''
+    exts = {}
+    maxlength = 0
+    for dir in filter(os.path.isdir,
+                      (os.path.join(pd, 'hgext') for pd in pathdirs())):
+        for e in os.listdir(dir):
+            if e.endswith('.py'):
+                name = e.rsplit('.', 1)[0]
+                path = os.path.join(dir, e)
+            else:
+                name = e
+                path = os.path.join(dir, e, '__init__.py')
+
+            if name in exts or name == '__init__' or not os.path.exists(path):
+                continue
+
+            try:
+                find(name)
+            except KeyError:
+                pass
+            else:
+                continue # enabled extension
+
+            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