Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permit putting class members on 'stub' pages #104

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ develop-eggs
distribute-*.tar.gz

# Other
.vimsession
.cache
.tox
.*.swp
Expand Down
2 changes: 1 addition & 1 deletion sphinx_automodapi/automodapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def setup(app):
from . import automodsumm
app.setup_extension(automodsumm.__name__)

app.connect('source-read', process_automodapi)
app.connect('source-read', process_automodapi, priority=100)

app.add_config_value('automodapi_inheritance_diagram', True, True)
app.add_config_value('automodapi_toctreedirnm', 'api', True)
Expand Down
33 changes: 27 additions & 6 deletions sphinx_automodapi/automodsumm.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
actually used by sphinx, so this option is only for figuring out the
cause of sphinx warnings or other debugging. Defaults to ``False``.

* ``automodsumm_stub_pages``
Should be a bool, and if ``True``, will cause `automodsumm`_ to
generate individual stub pages for class attributes and class methods
rather than documenting all class methods and attributes on the same
page. Defaults to ``False``.

* ``automodsumm_inherited_members``
Should be a bool and if ``True``, will cause `automodsumm`_ to document
class members that are inherited from a base class. This value can be
Expand Down Expand Up @@ -266,10 +272,13 @@ def process_automodsumm_generation(app):
for sfn, lines in zip(filestosearch, liness):
suffix = os.path.splitext(sfn)[1]
if len(lines) > 0:
generate_automodsumm_docs(
new_files = generate_automodsumm_docs(
lines, sfn, app=app, builder=app.builder,
suffix=suffix, base_path=app.srcdir,
stub_pages=app.config.automodsumm_stub_pages,
inherited_members=app.config.automodsumm_inherited_members)
for f in new_files:
env.found_docs.add(env.path2doc(f))


# _automodsummrex = re.compile(r'^(\s*)\.\. automodsumm::\s*([A-Za-z0-9_.]+)\s*'
Expand Down Expand Up @@ -408,6 +417,7 @@ def automodsumm_to_autosummary_lines(fn, app):
def generate_automodsumm_docs(lines, srcfn, app=None, suffix='.rst',
base_path=None, builder=None,
template_dir=None,
stub_pages=False,
inherited_members=False):
"""
This function is adapted from
Expand All @@ -434,7 +444,13 @@ def generate_automodsumm_docs(lines, srcfn, app=None, suffix='.rst',
template_dirs = [os.path.join(os.path.dirname(__file__), 'templates'),
os.path.join(base_path, '_templates')]
if builder is not None:
# allow the user to override the templates
# allow the user to override the templates and ensure class stub
# pages read the automodsumm base.rst
# TODO: Will this override user base.rst files?
local_dir_full = os.path.join(template_dirs[0], 'autosummary_core')
templates_path = builder.config.templates_path
if local_dir_full not in templates_path:
templates_path.append(local_dir_full)
template_loader = BuiltinTemplateLoader()
template_loader.init(builder, dirs=template_dirs)
else:
Expand Down Expand Up @@ -487,11 +503,11 @@ def generate_automodsumm_docs(lines, srcfn, app=None, suffix='.rst',
fn = os.path.join(path, name + suffix)

# skip it if it exists
# always add to new_files so we can add them to env.found_docs
new_files.append(fn)
if os.path.isfile(fn):
continue

new_files.append(fn)

f = open(fn, 'w')

try:
Expand All @@ -502,8 +518,11 @@ def generate_automodsumm_docs(lines, srcfn, app=None, suffix='.rst',
template = template_env.get_template(template_name)
else:
tmplstr = 'autosummary_core/%s.rst'
tmplname = doc.objtype
if tmplname == 'class':
tmplname = 'class-stubs' if stub_pages else 'class-flat'
try:
template = template_env.get_template(tmplstr % doc.objtype)
template = template_env.get_template(tmplstr % tmplname)
except TemplateNotFound:
template = template_env.get_template(tmplstr % 'base')

Expand Down Expand Up @@ -649,6 +668,7 @@ def get_members_class(obj, typ, include_public=[],
finally:
f.close()

return new_files

def setup(app):

Expand All @@ -663,9 +683,10 @@ def setup(app):

app.add_directive('automod-diagram', Automoddiagram)
app.add_directive('automodsumm', Automodsumm)
app.connect('builder-inited', process_automodsumm_generation)
app.connect('builder-inited', process_automodsumm_generation, priority=100)

app.add_config_value('automodsumm_writereprocessed', False, True)
app.add_config_value('automodsumm_stub_pages', False, 'env')
app.add_config_value('automodsumm_inherited_members', False, 'env')

return {'parallel_read_safe': True,
Expand Down
46 changes: 46 additions & 0 deletions sphinx_automodapi/templates/autosummary_core/class-stubs.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{% if referencefile %}
.. include:: {{ referencefile }}
{% endif %}

{{ objname }}
{{ underline }}

.. currentmodule:: {{ module }}

.. autoclass:: {{ objname }}
:show-inheritance:

{% if '__init__' in methods %}
{% set caught_result = methods.remove('__init__') %}
{% endif %}

{% block attributes_summary %}
{% if attributes %}

.. rubric:: Attributes Summary

.. autosummary::
:toctree:
:template: base.rst
{% for item in attributes %}
~{{ name }}.{{ item }}
{%- endfor %}

{% endif %}
{% endblock %}

{% block methods_summary %}
{% if methods %}

.. rubric:: Methods Summary

.. autosummary::
:toctree:
:template: base.rst
{% for item in methods %}
~{{ name }}.{{ item }}
{%- endfor %}

{% endif %}
{% endblock %}