pip/docs/pipext.py

109 lines
3.3 KiB
Python
Raw Permalink Normal View History

2013-01-18 22:25:15 +01:00
"""pip sphinx extensions"""
import optparse
import sys
from textwrap import dedent
2013-01-18 22:25:15 +01:00
from docutils import nodes
from docutils.parsers import rst
from docutils.statemachine import ViewList
from pip._internal import cmdoptions
from pip._internal.commands import commands_dict as commands
2013-01-18 22:25:15 +01:00
class PipCommandUsage(rst.Directive):
required_arguments = 1
2013-01-18 22:25:15 +01:00
def run(self):
cmd = commands[self.arguments[0]]
usage = dedent(cmd.usage.replace('%prog', 'pip')).strip()
2013-01-18 22:25:15 +01:00
node = nodes.literal_block(usage, usage)
return [node]
2013-01-18 22:25:15 +01:00
class PipCommandDescription(rst.Directive):
required_arguments = 1
2013-01-18 22:25:15 +01:00
def run(self):
node = nodes.paragraph()
node.document = self.state.document
desc = ViewList()
description = dedent(commands[self.arguments[0]].__doc__)
2013-01-18 22:25:15 +01:00
for line in description.split('\n'):
desc.append(line, "")
self.state.nested_parse(desc, 0, node)
return [node]
2013-01-18 22:25:15 +01:00
class PipOptions(rst.Directive):
def _format_option(self, option, cmd_name=None):
if cmd_name:
bookmark_line = ".. _`%s_%s`:" % (cmd_name, option._long_opts[0])
else:
bookmark_line = ".. _`%s`:" % option._long_opts[0]
2014-02-12 06:55:43 +01:00
line = ".. option:: "
2013-01-18 22:25:15 +01:00
if option._short_opts:
line += option._short_opts[0]
if option._short_opts and option._long_opts:
line += ", %s" % option._long_opts[0]
elif option._long_opts:
line += option._long_opts[0]
if option.takes_value():
metavar = option.metavar or option.dest.lower()
line += " <%s>" % metavar.lower()
2014-03-26 23:24:19 +01:00
# fix defaults
2013-01-18 22:25:15 +01:00
opt_help = option.help.replace('%default', str(option.default))
2014-03-26 23:24:19 +01:00
# fix paths with sys.prefix
2013-01-18 22:25:15 +01:00
opt_help = opt_help.replace(sys.prefix, "<sys.prefix>")
return [bookmark_line, "", line, "", " %s" % opt_help, ""]
def _format_options(self, options, cmd_name=None):
for option in options:
if option.help == optparse.SUPPRESS_HELP:
continue
for line in self._format_option(option, cmd_name):
self.view_list.append(line, "")
def run(self):
node = nodes.paragraph()
node.document = self.state.document
self.view_list = ViewList()
self.process_options()
self.state.nested_parse(self.view_list, 0, node)
return [node]
2013-01-18 22:25:15 +01:00
class PipGeneralOptions(PipOptions):
def process_options(self):
self._format_options(
[o() for o in cmdoptions.general_group['options']]
)
2013-01-18 22:25:15 +01:00
2013-01-18 22:25:15 +01:00
class PipIndexOptions(PipOptions):
def process_options(self):
self._format_options(
[o() for o in cmdoptions.index_group['options']]
)
2013-01-18 22:25:15 +01:00
2013-01-18 22:25:15 +01:00
class PipCommandOptions(PipOptions):
required_arguments = 1
2013-01-18 22:25:15 +01:00
def process_options(self):
cmd = commands[self.arguments[0]]()
self._format_options(
cmd.parser.option_groups[0].option_list,
cmd_name=cmd.name,
)
2013-01-18 22:25:15 +01:00
2013-01-18 22:25:15 +01:00
def setup(app):
app.add_directive('pip-command-usage', PipCommandUsage)
app.add_directive('pip-command-description', PipCommandDescription)
app.add_directive('pip-command-options', PipCommandOptions)
app.add_directive('pip-general-options', PipGeneralOptions)
app.add_directive('pip-index-options', PipIndexOptions)