extensions: move extensions listing functions from mercurial.help

Includes some small fix-ups to comments in enabled() and disabled().
This commit is contained in:
Cédric Duval 2009-06-21 16:32:00 +02:00
parent 0d5412917a
commit 17141a61d0
3 changed files with 79 additions and 82 deletions

View File

@ -1484,7 +1484,7 @@ def help_(ui, name=None, with_version=False):
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))

View File

@ -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 @@ def wrapfunction(container, funcname, wrapper):
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

View File

@ -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 @@ def moduledoc(file):
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 @@ def topicextensions():
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