extensions: refactor disabled()
authorBrodie Rao <me+hg@dackz.net>
Sun, 07 Feb 2010 11:32:08 +0100
changeset 10363 c07974215b3d
parent 10362 2e3ec7ef5349
child 10364 de1e7099d100
extensions: refactor disabled()
mercurial/extensions.py
--- a/mercurial/extensions.py	Sun Feb 07 14:06:05 2010 +0100
+++ b/mercurial/extensions.py	Sun Feb 07 11:32:08 2010 +0100
@@ -131,22 +131,17 @@
     setattr(container, funcname, wrap)
     return origfn
 
-def disabled():
-    '''find disabled extensions from hgext
-    returns a dict of {name: desc}, and the max name length'''
-
+def _disabledpaths():
+    '''find paths of disabled extensions. returns a dict of {name: path}'''
     import hgext
     extpath = os.path.dirname(os.path.abspath(hgext.__file__))
-
     try: # might not be a filesystem path
         files = os.listdir(extpath)
     except OSError:
-        return None, 0
+        return {}
 
     exts = {}
-    maxlength = 0
     for e in files:
-
         if e.endswith('.py'):
             name = e.rsplit('.', 1)[0]
             path = os.path.join(extpath, e)
@@ -155,23 +150,42 @@
             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
+        exts[name] = path
+    return exts
 
-        if name in exts or name in _order or name == '__init__':
+def _disabledhelp(path):
+    '''retrieve help synopsis of a disabled extension (without importing)'''
+    try:
+        file = open(path)
+    except IOError:
+        return
+    else:
+        doc = help.moduledoc(file)
+        file.close()
+
+    if doc: # extracting localized synopsis
+        return gettext(doc).splitlines()[0]
+    else:
+        return _('(no help text available)')
+
+def disabled():
+    '''find disabled extensions from hgext
+    returns a dict of {name: desc}, and the max name length'''
+
+    paths = _disabledpaths()
+    if not paths:
+        return None, 0
+
+    exts = {}
+    maxlength = 0
+    for name, path in paths.iteritems():
+        doc = _disabledhelp(path)
+        if not doc:
             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)')
-
+        exts[name] = doc
         if len(name) > maxlength:
             maxlength = len(name)