help.py
201 lines
| 7.2 KiB
| text/x-python
|
PythonLexer
/ mercurial / help.py
|
r3795 | # help.py - help data for mercurial | ||
# | ||||
# Copyright 2006 Matt Mackall <mpm@selenic.com> | ||||
# | ||||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r3795 | |||
|
r9539 | from i18n import gettext, _ | ||
|
r16710 | import itertools, sys, os | ||
|
r16126 | import extensions, revset, fileset, templatekw, templatefilters, filemerge | ||
|
r16781 | import encoding, util, minirst | ||
|
r8863 | |||
|
r14316 | def listexts(header, exts, indent=1): | ||
|
r8879 | '''return a text listing of the given extensions''' | ||
if not exts: | ||||
return '' | ||||
|
r14316 | maxlength = max(len(e) for e in exts) | ||
|
r8879 | result = '\n%s\n\n' % header | ||
for name, desc in sorted(exts.iteritems()): | ||||
|
r10364 | result += '%s%-*s %s\n' % (' ' * indent, maxlength + 2, | ||
':%s:' % name, desc) | ||||
|
r8864 | return result | ||
|
r8879 | def extshelp(): | ||
|
r9539 | doc = loaddoc('extensions')() | ||
|
r14316 | doc += listexts(_('enabled extensions:'), extensions.enabled()) | ||
doc += listexts(_('disabled extensions:'), extensions.disabled()) | ||||
|
r8863 | return doc | ||
|
r7013 | |||
|
r16781 | def optrst(options, verbose): | ||
data = [] | ||||
multioccur = False | ||||
for option in options: | ||||
if len(option) == 5: | ||||
shortopt, longopt, default, desc, optlabel = option | ||||
else: | ||||
shortopt, longopt, default, desc = option | ||||
optlabel = _("VALUE") # default label | ||||
if _("DEPRECATED") in desc and not verbose: | ||||
continue | ||||
so = '' | ||||
if shortopt: | ||||
so = '-' + shortopt | ||||
lo = '--' + longopt | ||||
if default: | ||||
desc += _(" (default: %s)") % default | ||||
if isinstance(default, list): | ||||
lo += " %s [+]" % optlabel | ||||
multioccur = True | ||||
elif (default is not None) and not isinstance(default, bool): | ||||
lo += " %s" % optlabel | ||||
data.append((so, lo, desc)) | ||||
rst = minirst.maketable(data, 1) | ||||
if multioccur: | ||||
|
r16815 | rst.append(_("\n[+] marked option can be specified multiple times\n")) | ||
|
r16781 | |||
|
r16815 | return ''.join(rst) | ||
|
r16781 | |||
|
r16710 | def topicmatch(kw): | ||
"""Return help topics matching kw. | ||||
Returns {'section': [(name, summary), ...], ...} where section is | ||||
one of topics, commands, extensions, or extensioncommands. | ||||
""" | ||||
kw = encoding.lower(kw) | ||||
def lowercontains(container): | ||||
|
r16845 | return kw in encoding.lower(container) # translated in helptable | ||
|
r16710 | results = {'topics': [], | ||
'commands': [], | ||||
'extensions': [], | ||||
'extensioncommands': [], | ||||
} | ||||
for names, header, doc in helptable: | ||||
if (sum(map(lowercontains, names)) | ||||
or lowercontains(header) | ||||
or lowercontains(doc())): | ||||
results['topics'].append((names[0], header)) | ||||
import commands # avoid cycle | ||||
for cmd, entry in commands.table.iteritems(): | ||||
if cmd.startswith('debug'): | ||||
continue | ||||
if len(entry) == 3: | ||||
summary = entry[2] | ||||
else: | ||||
summary = '' | ||||
|
r16845 | # translate docs *before* searching there | ||
docs = _(getattr(entry[0], '__doc__', None)) or '' | ||||
|
r16710 | if kw in cmd or lowercontains(summary) or lowercontains(docs): | ||
|
r16845 | doclines = docs.splitlines() | ||
|
r16710 | if doclines: | ||
summary = doclines[0] | ||||
cmdname = cmd.split('|')[0].lstrip('^') | ||||
results['commands'].append((cmdname, summary)) | ||||
for name, docs in itertools.chain( | ||||
extensions.enabled().iteritems(), | ||||
extensions.disabled().iteritems()): | ||||
# extensions.load ignores the UI argument | ||||
mod = extensions.load(None, name, '') | ||||
if lowercontains(name) or lowercontains(docs): | ||||
|
r16845 | # extension docs are already translated | ||
results['extensions'].append((name, docs.splitlines()[0])) | ||||
|
r16710 | for cmd, entry in getattr(mod, 'cmdtable', {}).iteritems(): | ||
|
r16711 | if kw in cmd or (len(entry) > 2 and lowercontains(entry[2])): | ||
|
r16710 | cmdname = cmd.split('|')[0].lstrip('^') | ||
results['extensioncommands'].append( | ||||
(cmdname, _(getattr(cmd, '__doc__', '')))) | ||||
return results | ||||
|
r9539 | def loaddoc(topic): | ||
"""Return a delayed loader for help/topic.txt.""" | ||||
|
r3798 | |||
|
r9539 | def loader(): | ||
|
r14941 | if util.mainfrozen(): | ||
|
r9539 | module = sys.executable | ||
else: | ||||
module = __file__ | ||||
base = os.path.dirname(module) | ||||
|
r7293 | |||
|
r9539 | for dir in ('.', '..'): | ||
docdir = os.path.join(base, dir, 'help') | ||||
if os.path.isdir(docdir): | ||||
break | ||||
|
r7677 | |||
|
r9539 | path = os.path.join(docdir, topic + ".txt") | ||
|
r14168 | doc = gettext(util.readfile(path)) | ||
|
r12820 | for rewriter in helphooks.get(topic, []): | ||
doc = rewriter(topic, doc) | ||||
return doc | ||||
|
r9539 | return loader | ||
|
r7677 | |||
|
r13888 | helptable = sorted([ | ||
|
r12145 | (["config", "hgrc"], _("Configuration Files"), loaddoc('config')), | ||
|
r9539 | (["dates"], _("Date Formats"), loaddoc('dates')), | ||
(["patterns"], _("File Name Patterns"), loaddoc('patterns')), | ||||
|
r10282 | (['environment', 'env'], _('Environment Variables'), | ||
loaddoc('environment')), | ||||
(['revs', 'revisions'], _('Specifying Single Revisions'), | ||||
loaddoc('revisions')), | ||||
(['mrevs', 'multirevs'], _('Specifying Multiple Revisions'), | ||||
loaddoc('multirevs')), | ||||
|
r12818 | (['revset', 'revsets'], _("Specifying Revision Sets"), loaddoc('revsets')), | ||
|
r14686 | (['fileset', 'filesets'], _("Specifying File Sets"), loaddoc('filesets')), | ||
|
r9539 | (['diffs'], _('Diff Formats'), loaddoc('diffs')), | ||
|
r12771 | (['merge-tools'], _('Merge Tools'), loaddoc('merge-tools')), | ||
|
r16568 | (['templating', 'templates', 'template', 'style'], _('Template Usage'), | ||
|
r10282 | loaddoc('templates')), | ||
|
r9539 | (['urls'], _('URL Paths'), loaddoc('urls')), | ||
|
r16547 | (["extensions"], _("Using Additional Features"), extshelp), | ||
|
r14044 | (["subrepo", "subrepos"], _("Subrepositories"), loaddoc('subrepos')), | ||
(["hgweb"], _("Configuring hgweb"), loaddoc('hgweb')), | ||||
(["glossary"], _("Glossary"), loaddoc('glossary')), | ||||
|
r16547 | (["hgignore", "ignore"], _("Syntax for Mercurial Ignore Files"), | ||
|
r14044 | loaddoc('hgignore')), | ||
|
r15996 | (["phases"], _("Working with Phases"), loaddoc('phases')), | ||
|
r13888 | ]) | ||
|
r12820 | |||
# Map topics to lists of callable taking the current topic help and | ||||
# returning the updated version | ||||
|
r14318 | helphooks = {} | ||
|
r12820 | |||
def addtopichook(topic, rewriter): | ||||
helphooks.setdefault(topic, []).append(rewriter) | ||||
|
r13593 | |||
def makeitemsdoc(topic, doc, marker, items): | ||||
"""Extract docstring from the items key to function mapping, build a | ||||
.single documentation block and use it to overwrite the marker in doc | ||||
""" | ||||
entries = [] | ||||
for name in sorted(items): | ||||
text = (items[name].__doc__ or '').rstrip() | ||||
if not text: | ||||
continue | ||||
text = gettext(text) | ||||
lines = text.splitlines() | ||||
|
r16250 | doclines = [(lines[0])] | ||
for l in lines[1:]: | ||||
# Stop once we find some Python doctest | ||||
if l.strip().startswith('>>>'): | ||||
break | ||||
doclines.append(' ' + l.strip()) | ||||
entries.append('\n'.join(doclines)) | ||||
|
r13593 | entries = '\n\n'.join(entries) | ||
return doc.replace(marker, entries) | ||||
|
r14318 | |||
def addtopicsymbols(topic, marker, symbols): | ||||
def add(topic, doc): | ||||
return makeitemsdoc(topic, doc, marker, symbols) | ||||
addtopichook(topic, add) | ||||
|
r14686 | addtopicsymbols('filesets', '.. predicatesmarker', fileset.symbols) | ||
|
r16126 | addtopicsymbols('merge-tools', '.. internaltoolsmarker', filemerge.internals) | ||
|
r14318 | addtopicsymbols('revsets', '.. predicatesmarker', revset.symbols) | ||
addtopicsymbols('templates', '.. keywordsmarker', templatekw.keywords) | ||||
addtopicsymbols('templates', '.. filtersmarker', templatefilters.filters) | ||||