[PATCH 1 of 4] extensions: functions for a list of enabled and disabled extensions
Cédric Duval
cedricduval at free.fr
Sun Jun 21 09:40:08 CDT 2009
# HG changeset patch
# User Cédric Duval <cedricduval at free.fr>
# Date 1245594909 -7200
# Node ID 69c98c3a44e313d8dd767239f603d27ae2e2079c
# Parent d244ee52ac30c4f8b557140616f992d8726780e8
extensions: functions for a list of enabled and disabled extensions
Moving those functions from help.py to extensions.py as enabled()
and disabled(), where they belong more. pathdirs() comes as well.
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1484,7 +1484,7 @@
ui.write(' %-*s %s\n' % (m, f, h[f]))
if name != 'shortlist':
- exts, maxlength = help.enabledextensions()
+ exts, maxlength = extensions.enabled()
ui.write(help.extensionslisting(_('enabled extensions:'),
exts, maxlength))
diff --git a/mercurial/extensions.py b/mercurial/extensions.py
--- a/mercurial/extensions.py
+++ b/mercurial/extensions.py
@@ -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,80 @@
origfn = getattr(container, funcname)
setattr(container, funcname, wrap)
return origfn
+
+def pathdirs():
+ '''Convert sys.path into a list of absolute, existing, unique paths.
+
+ Function borrowed 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 the extensions shipped with Mercurial but not enabled
+
+ Returns a dict of extensions names and descriptions, 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 max name length'''
+ exts = {}
+ maxlength = 0
+
+ if not enabled:
+ return None, None
+
+ 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
diff --git a/mercurial/help.py b/mercurial/help.py
--- a/mercurial/help.py
+++ b/mercurial/help.py
@@ -5,24 +5,10 @@
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2, incorporated herein by reference.
-import os, sys
-from i18n import _, gettext
+from i18n import _
import extensions
-# borrowed from pydoc
-def pathdirs():
- '''Convert sys.path into a list of absolute, existing, unique paths.'''
- 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
-
# loosely inspired by pydoc.source_synopsis()
# rewritten to handle ''' as well as """
# and to return the whole text instead of just the synopsis
@@ -53,67 +39,6 @@
return ''.join(result)
-def additionalextensions():
- '''Find the extensions shipped with Mercurial but not enabled
-
- Returns extensions names and descriptions, 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:
- extensions.find(name)
- except KeyError:
- pass
- else:
- continue # enabled extension
-
- try:
- file = open(path)
- except IOError:
- continue
- else:
- doc = 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 enabledextensions():
- '''Return the list of enabled extensions, and max name length'''
- enabled = list(extensions.extensions())
- exts = {}
- maxlength = 0
-
- if enabled:
- exthelps = []
- for ename, ext in enabled:
- 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
-
def extensionslisting(header, exts, maxlength):
'''Return a text listing of the given extensions'''
result = ''
@@ -160,10 +85,10 @@
hgext.baz = !
''')
- exts, maxlength = enabledextensions()
+ exts, maxlength = extensions.enabled()
doc += extensionslisting(_('enabled extensions:'), exts, maxlength)
- exts, maxlength = additionalextensions()
+ exts, maxlength = extensions.disabled()
doc += extensionslisting(_('non-enabled extensions:'), exts, maxlength)
return doc
More information about the Mercurial-devel
mailing list