Skip to content

Commit f0ad505

Browse files
authored
Merge pull request #93 from funkyfuture/sorted_sections
An option to sort sections
2 parents eca0cd3 + ea71375 commit f0ad505

File tree

7 files changed

+48
-16
lines changed

7 files changed

+48
-16
lines changed

autodocsumm/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,15 @@ def get_grouped_documenters(self, all_members=False):
270270
if not use_sections or section in use_sections:
271271
documenters.setdefault(section, []).append(e)
272272
self.options.update(options_save)
273-
return documenters
273+
274+
sort_option = self.env.config.autodocsumm_section_sorter
275+
if sort_option is True:
276+
return {k: documenters[k] for k in sorted(documenters)}
277+
elif callable(sort_option):
278+
return {k: documenters[k] for k in sorted(documenters, key=sort_option)}
279+
else:
280+
return documenters
281+
274282

275283
def add_autosummary(self, relative_ref_paths=False):
276284
"""Add the autosammary table of this documenter.
@@ -656,6 +664,7 @@ def setup(app):
656664
app.add_config_value('autodata_content', 'class', True)
657665
app.add_config_value('document_data', True, True)
658666
app.add_config_value('not_document_data', [], True)
667+
app.add_config_value('autodocsumm_section_sorter', None, True)
659668
return {'version': sphinx.__display_version__, 'parallel_read_safe': True}
660669

661670

docs/conf.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# -*- coding: utf-8 -*-
2-
#
31
# autodocsumm documentation build configuration file, created by
42
# sphinx-quickstart on Mon Jul 20 18:01:33 2015.
53
#

docs/conf_settings.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,27 @@ Configuration values and events
135135
To exclude the string representation of specific data objects. You may
136136
provide a list of fully qualified object names (e.g. in the form of
137137
``'zipfile.ZipFile'``) or ``True`` or ``False``
138+
139+
140+
.. confval:: autodocsumm_section_sorter
141+
142+
When ``True`` the summarizing sections will be sorted alphanumerically by
143+
their section title. Alternatively a callable can be set that is passed to
144+
:func:`sorted` as key argument to sort the the summary sections by their
145+
name.
146+
The default value is ``None``.
147+
Example usage with a tuple that defines an arbitrary order:
148+
149+
.. code-block:: python
150+
151+
sections_order = ("Public methods", "Attributes", "Private methods")
152+
autodocsumm_section_sorter = lambda title: sections_order.index(title)
153+
154+
An example for cases that only ensures that "Constructors" are always listed
155+
first and "Attributes" while not failing when encountering undefined section
156+
weights:
157+
158+
.. code-block:: python
159+
160+
section_weights = {"Attributes": 100, "Constructors": -100}
161+
autodocsumm_section_sorter = lambda title: sections_weights.get(title, 0)

tests/test-root/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
import sys
32
import sphinx
43
sys.path.insert(0, '.')
@@ -24,6 +23,8 @@
2423

2524
autodata_content = 'both'
2625

26+
autodocsumm_section_sorter = True
27+
2728

2829
def member_filter(app, what, name, obj, skip, options):
2930
import dummy

tests/test-root/dummy.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
"""
32
Module for testing the autodocsumm
43

tests/test-root/dummy_title.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
"""
32
Module for testing the autodocsumm
43
----------------------------------

tests/test_autodocsumm.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
See the License for the specific language governing permissions and
1818
limitations under the License.
1919
"""
20-
import os.path as osp
20+
from pathlib import Path
2121
import re
2222
import bs4
2323
import pytest
@@ -37,19 +37,16 @@ def in_between(full, sub, s0, *others):
3737

3838

3939
def get_html(app, fname):
40-
with open(osp.join(str(app.outdir), fname)) as f:
41-
return f.read()
40+
return (Path(app.outdir) / fname).read_text()
41+
42+
43+
def get_soup(app, fname):
44+
return bs4.BeautifulSoup(get_html(app, fname), 'html.parser')
4245

4346

4447
def in_autosummary(what, html) -> bool:
4548
soup = bs4.BeautifulSoup(html, "html.parser")
46-
autosummaries = soup("table")
47-
found = False
48-
for tag in autosummaries:
49-
if tag.find_all("span", string=what):
50-
found = True
51-
break
52-
return found
49+
return any(tag.find_all("span", string=what) for tag in soup("table"))
5350

5451

5552
class TestAutosummaryDocumenter:
@@ -377,6 +374,11 @@ def test_module_submodule(self, app):
377374
assert re.findall(r'<td>.*href="#dummy_submodule\.submodule2'
378375
r'\.SubmoduleClass2\.func2".*</td>', html)
379376

377+
def test_sorted_sections(self, app):
378+
soup = get_soup(app, 'test_autoclasssumm_some_sections.html')
379+
sections = soup.select("p strong")
380+
assert [s.string[:-1] for s in sections] == ["Attributes", "DummySection"]
381+
380382

381383
class TestAutoDocSummDirective:
382384
"""Test case for the :class:`autodocsumm.AutoDocSummDirective`."""

0 commit comments

Comments
 (0)