Skip to content

Instantly share code, notes, and snippets.

@tsibley
Created July 10, 2023 17:38
Show Gist options
  • Save tsibley/f8c5ada0dd257cc122799792b9cbe33d to your computer and use it in GitHub Desktop.
Save tsibley/f8c5ada0dd257cc122799792b9cbe33d to your computer and use it in GitHub Desktop.
diff --git a/.venv/lib/python3.8/site-packages/sphinxcontrib/autoprogram.py b/autoprogram.py
index 635ea82..f63d2d3 100644
--- a/.venv/lib/python3.8/site-packages/sphinxcontrib/autoprogram.py
+++ b/autoprogram.py
@@ -25,8 +25,7 @@ from docutils import nodes
from docutils.parsers.rst import Directive
from docutils.parsers.rst.directives import unchanged
from docutils.statemachine import StringList, ViewList
-from six import exec_
-from six.moves import builtins, reduce
+from functools import reduce
from sphinx.domains import std
from sphinx.util.nodes import nested_parse_with_titles
@@ -68,13 +67,17 @@ def scan_programs(
if groups:
yield command, [], parser
+ for group in parser._action_groups:
+ option_groups = list(scan_options(group._group_actions))
+ if option_groups:
+ yield command, option_groups, group
+ else:
+ option_groups = []
for group in parser._action_groups:
options = list(scan_options(group._group_actions))
if options:
- yield command, options, group
- else:
- options = list(scan_options(parser._actions))
- yield command, options, parser
+ option_groups.append((group.title, group.description, options))
+ yield command, option_groups, parser
if parser._subparsers:
choices: Iterable[Tuple[Any, Any]] = ()
@@ -125,7 +128,7 @@ def format_option(arg) -> Tuple[List[str], str]:
metavar = arg.metavar or arg.dest
if not isinstance(metavar, tuple):
metavar = (metavar,)
- value = "<{0}>".format("> <".join(metavar).lower())
+ value = " ".join(metavar)
names = [
"{0} {1}".format(option_string, value) for option_string in arg.option_strings
@@ -154,7 +157,7 @@ def import_object(import_name: str):
with open(f[0]) as fobj:
codestring = fobj.read()
foo = imp.new_module("foo")
- exec_(codestring, foo.__dict__)
+ exec(codestring, foo.__dict__)
sys.modules["foo"] = foo
mod = __import__("foo")
@@ -163,7 +166,7 @@ def import_object(import_name: str):
raise
mod = reduce(getattr, module_name.split(".")[1:], mod)
- globals_: Dict[str, Any] = builtins # type: ignore[assignment]
+ globals_: Dict[str, Any] = __builtins__ # type: ignore[assignment]
if not isinstance(globals_, dict):
globals_ = globals_.__dict__
return eval(expr, globals_, mod.__dict__)
@@ -218,7 +221,7 @@ class AutoprogramDirective(Directive):
if prog and parser.prog.startswith(original_prog):
parser.prog = parser.prog.replace(original_prog, prog, 1)
- for commands, options, group_or_parser in scan_programs(
+ for commands, option_groups, group_or_parser in scan_programs(
parser, maxdepth=maxdepth, groups=groups
):
if isinstance(group_or_parser, argparse._ArgumentGroup):
@@ -241,7 +244,7 @@ class AutoprogramDirective(Directive):
for line in render_rst(
title,
- options,
+ option_groups,
is_program=is_program,
is_subgroup=is_subgroup,
description=description,
@@ -264,7 +267,7 @@ class AutoprogramDirective(Directive):
def render_rst(
title: str,
- options,
+ option_groups,
is_program: bool,
is_subgroup: bool,
description: Optional[str],
@@ -295,10 +298,6 @@ def render_rst(
yield ("!" if is_subgroup else "?") * len(title)
yield ""
- for line in inspect.cleandoc(description or "").splitlines():
- yield line
- yield ""
-
if usage is None:
pass
elif usage_codeblock:
@@ -311,12 +310,25 @@ def render_rst(
yield ""
- for option_strings, help_ in options:
- yield ".. option:: {0}".format(", ".join(option_strings))
+ for line in inspect.cleandoc(description or "").splitlines():
+ yield line
+ yield ""
+
+ for group_title, group_description, options in option_groups:
+ yield group_title[0].title() + group_title[1:]
+ yield "#" * len(group_title)
yield ""
- yield " " + help_.replace("\n", " \n")
+
+ for line in inspect.cleandoc(group_description or "").splitlines():
+ yield line
yield ""
+ for option_strings, help_ in options:
+ yield ".. option:: {0}".format(", ".join(option_strings))
+ yield ""
+ yield " " + help_.replace("\n", " \n")
+ yield ""
+
for line in (epilog or "").splitlines():
yield line or ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment