Skip to content

Commit

Permalink
Ensure sort is done also in automodsumm file writing
Browse files Browse the repository at this point in the history
Signed-off-by: nstarman <nstarman@users.noreply.github.com>

# Conflicts:
#	sphinx_automodapi/automodsumm.py
  • Loading branch information
mhvk authored and nstarman committed Apr 20, 2024
1 parent 0afd92a commit 2b01690
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
15 changes: 12 additions & 3 deletions sphinx_automodapi/automodsumm.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
in the generated documentation. The flags ``:inherited-members:`` or
``:no-inherited-members:`` allows overrriding this global setting.
* ``:sort:``
If the module contains ``__all__``, sort the module's objects
alphabetically (if ``__all__`` is not present, the objects are found
using `dir`, which always gives a sorted list).
This extension also adds three sphinx configuration options:
* ``automodsumm_writereprocessed``
Expand Down Expand Up @@ -381,12 +387,12 @@ def automodsumm_to_autosummary_lines(fn, app):
opssecs, remainders)):
allindent = i1 + (' ' if i2 is None else i2)

# filter out functions-only, classes-only, and ariables-only
# filter out functions-only, classes-only, variables-only, and sort
# options if present.
oplines = ops.split('\n')
toskip = []
allowedpkgnms = []
funcsonly = clssonly = varsonly = False
funcsonly = clssonly = varsonly = sort = False
for i, ln in reversed(list(enumerate(oplines))):
if ':functions-only:' in ln:
funcsonly = True
Expand All @@ -403,6 +409,9 @@ def automodsumm_to_autosummary_lines(fn, app):
if ':allowed-package-names:' in ln:
allowedpkgnms.extend(_str_list_converter(ln.replace(':allowed-package-names:', '')))
del oplines[i]
if ':sort:' in ln:
sort = True
del oplines[i]
if [funcsonly, clssonly, varsonly].count(True) > 1:
msg = ('Defined more than one of functions-only, classes-only, '
'and variables-only. Skipping this directive.')
Expand All @@ -420,7 +429,7 @@ def automodsumm_to_autosummary_lines(fn, app):
newlines.extend(oplines)

ols = True if len(allowedpkgnms) == 0 else allowedpkgnms
for nm, fqn, obj in zip(*find_mod_objs(modnm, onlylocals=ols)):
for nm, fqn, obj in zip(*find_mod_objs(modnm, onlylocals=ols, sort=sort)):
if nm in toskip:
continue
if funcsonly and not inspect.isroutine(obj):
Expand Down
14 changes: 13 additions & 1 deletion sphinx_automodapi/tests/test_automodsumm.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ def test_ams_cython(tmpdir, cython_testpackage): # noqa

# =============================================================================

CLASS_RST = """
:orphan:
.. currentmodule:: {mod}
.. autoclass:: {cls}
""".strip()

sorted_str = """
Before
Expand All @@ -229,7 +237,11 @@ def test_sort(tmpdir):
with open(tmpdir.join("index.rst").strpath, "w") as f:
f.write(sorted_str)

write_api_files_to_tmpdir(tmpdir)
apidir = tmpdir.mkdir('api')
mod = 'sphinx_automodapi.tests.example_module.classes'
for cls in "Spam", "Egg":
with open(apidir.join(f'{mod}.{cls}.rst').strpath, 'w') as f:
f.write(CLASS_RST.format(mod=mod, cls=cls))

run_sphinx_in_tmpdir(tmpdir)

Expand Down

0 comments on commit 2b01690

Please sign in to comment.