Mercurial > hg-stable
changeset 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 | c3e4d3c1d48b |
children | d0c0013f8713 |
files | mercurial/commands.py mercurial/extensions.py mercurial/help.py |
diffstat | 3 files changed, 79 insertions(+), 82 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/commands.py Sun Jun 21 16:27:07 2009 +0200 +++ b/mercurial/commands.py Sun Jun 21 16:32:00 2009 +0200 @@ -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))
--- 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
--- a/mercurial/help.py Sun Jun 21 16:27:07 2009 +0200 +++ b/mercurial/help.py Sun Jun 21 16:32:00 2009 +0200 @@ -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