Skip to content

Commit 769ea19

Browse files
authored
Merge pull request #127 from adrn/include
Add :include: option, to do the opposite of :skip:
2 parents b68a5f3 + 1100412 commit 769ea19

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

sphinx_automodapi/automodapi.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
included in the generated documentation. This option may appear
2626
any number of times to skip multiple objects.
2727
28+
* ``:include: str``
29+
This option is the opposite of :skip: -- if specified, only the object
30+
names that match any of the names passed to :include: will be included
31+
in the generated documentation. This option may appear multiple times
32+
to include multiple objects.
33+
2834
* ``:no-main-docstr:``
2935
If present, the docstring for the module/package will not be generated.
3036
The function and class tables will still be used, however.
@@ -232,6 +238,7 @@ def automodapi_replace(sourcestr, app, dotoctree=True, docname=None,
232238

233239
# initialize default options
234240
toskip = []
241+
includes = []
235242
inhdiag = app.config.automodapi_inheritance_diagram
236243
maindocstr = True
237244
top_head = True
@@ -245,6 +252,8 @@ def automodapi_replace(sourcestr, app, dotoctree=True, docname=None,
245252
for opname, args in _automodapiargsrex.findall(spl[grp * 3 + 2]):
246253
if opname == 'skip':
247254
toskip.append(args.strip())
255+
elif opname == 'include':
256+
includes.append(args.strip())
248257
elif opname == 'inheritance-diagram':
249258
inhdiag = True
250259
elif opname == 'no-inheritance-diagram':
@@ -290,8 +299,8 @@ def automodapi_replace(sourcestr, app, dotoctree=True, docname=None,
290299
if warnings:
291300
logger.warning(msg, location)
292301

293-
ispkg, hascls, hasfuncs, hasother = _mod_info(
294-
modnm, toskip, onlylocals=onlylocals)
302+
ispkg, hascls, hasfuncs, hasother, toskip = _mod_info(
303+
modnm, toskip, includes, onlylocals=onlylocals)
295304

296305
# add automodule directive only if no-main-docstr isn't present
297306
if maindocstr:
@@ -394,16 +403,20 @@ def automodapi_replace(sourcestr, app, dotoctree=True, docname=None,
394403
return sourcestr
395404

396405

397-
def _mod_info(modname, toskip=[], onlylocals=True):
406+
def _mod_info(modname, toskip=[], include=[], onlylocals=True):
398407
"""
399408
Determines if a module is a module or a package and whether or not
400409
it has classes or functions.
401410
"""
402411

403412
hascls = hasfunc = hasother = False
404413

414+
skips = []
405415
for localnm, fqnm, obj in zip(*find_mod_objs(modname, onlylocals=onlylocals)):
406-
if localnm not in toskip:
416+
if localnm in toskip or (include and localnm not in include):
417+
skips.append(localnm)
418+
419+
else:
407420
hascls = hascls or inspect.isclass(obj)
408421
hasfunc = hasfunc or inspect.isroutine(obj)
409422
hasother = hasother or (not inspect.isclass(obj) and
@@ -418,7 +431,7 @@ def _mod_info(modname, toskip=[], onlylocals=True):
418431
ispkg = (hasattr(pkg, '__file__') and isinstance(pkg.__file__, str) and
419432
os.path.split(pkg.__file__)[1].startswith('__init__.py'))
420433

421-
return ispkg, hascls, hasfunc, hasother
434+
return ispkg, hascls, hasfunc, hasother, skips
422435

423436

424437
def process_automodapi(app, docname, source):

sphinx_automodapi/tests/test_automodapi.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,54 @@ def test_am_replacer_skip(tmpdir):
327327
assert result == am_replacer_skip_expected
328328

329329

330+
am_replacer_include_str = """
331+
This comes before
332+
333+
.. automodapi:: sphinx_automodapi.tests.example_module.functions
334+
:include: add
335+
:include: subtract
336+
337+
This comes after
338+
"""
339+
340+
am_replacer_include_expected = """
341+
This comes before
342+
343+
344+
sphinx_automodapi.tests.example_module.functions Module
345+
-------------------------------------------------------
346+
347+
.. automodule:: sphinx_automodapi.tests.example_module.functions
348+
349+
Functions
350+
^^^^^^^^^
351+
352+
.. automodsumm:: sphinx_automodapi.tests.example_module.functions
353+
:functions-only:
354+
:toctree: api
355+
:skip: multiply
356+
357+
358+
This comes after
359+
""".format(empty='')
360+
361+
362+
def test_am_replacer_include(tmpdir):
363+
"""
364+
Tests using the ":include: option in an ".. automodapi::" .
365+
"""
366+
367+
with open(tmpdir.join('index.rst').strpath, 'w') as f:
368+
f.write(am_replacer_include_str.format(options=''))
369+
370+
run_sphinx_in_tmpdir(tmpdir)
371+
372+
with open(tmpdir.join('index.rst.automodapi').strpath) as f:
373+
result = f.read()
374+
375+
assert result == am_replacer_include_expected
376+
377+
330378
am_replacer_invalidop_str = """
331379
This comes before
332380

0 commit comments

Comments
 (0)