[Doc] Include summaries of each example in index of Python examples

This commit is contained in:
Ray Speth 2016-10-14 11:29:44 -04:00
parent 5af1dfd29c
commit a35a0cb510
5 changed files with 42 additions and 22 deletions

1
.gitignore vendored
View file

@ -37,6 +37,7 @@ config.log
coverage/
coverage.info
doc/sphinx/cython/examples
doc/sphinx/cython/examples.rst
doc/sphinx/matlab/examples/
doc/sphinx/matlab/tutorials/
doc/sphinx/matlab/code-docs/

View file

@ -70,6 +70,7 @@ if 'clean' in COMMAND_LINE_TARGETS:
removeDirectory('doc/sphinx/matlab/tutorials')
removeDirectory('doc/sphinx/matlab/code-docs')
removeDirectory('doc/sphinx/cython/examples')
removeFile('doc/sphinx/cython/examples.rst')
removeDirectory('interfaces/cython/Cantera.egg-info')
removeDirectory('interfaces/python_minimal/Cantera_minimal_.egg-info')
for name in os.listdir('interfaces/cython/cantera/data/'):

View file

@ -1,4 +1,5 @@
from buildutils import *
import ast
Import('env', 'build', 'install')
@ -7,6 +8,23 @@ localenv = env.Clone()
from collections import namedtuple
Page = namedtuple('Page', ['name', 'title', 'objects'])
def extract_python_docstring(pyfile, summary_only=True):
""" Returns the docstring from a Python script """
with open(pyfile) as f:
mod = ast.parse(f.read())
doc = ''
for node in mod.body:
if isinstance(node, ast.Expr) and isinstance(node.value, ast.Str):
doc = node.value.s
doc = doc.strip()
if summary_only:
doc = doc.split('\n\n')[0]
return doc
# Set up functions to pseudo-autodoc the MATLAB toolbox
def extract_matlab_docstring(mfile, level):
"""
@ -112,15 +130,27 @@ if localenv['sphinx_docs']:
# Python examples: Create individual documentation pages with the source
# for each example
example_root = Dir('#interfaces/cython/cantera/examples').abspath
indexenv = env.Clone()
for subdir in subdirs(example_root):
summaries = []
for f in mglob(env, pjoin(example_root, subdir), 'py'):
docname = 'examples/{0}_{1}'.format(subdir, f.name[:-3])
summaries.append(':doc:`{0} <{1}>`:'.format(f.name, docname))
summaries.append(extract_python_docstring(f.abspath))
summaries.append('')
tmpenv = env.Clone()
tmpenv['script_name'] = f.name
tmpenv['script_path'] = '../../../../interfaces/cython/cantera/examples/%s/%s' % (subdir, f.name)
b = tmpenv.SubstFile('#doc/sphinx/cython/examples/%s_%s.rst' % (subdir, f.name[:-3]),
b = tmpenv.SubstFile('#doc/sphinx/cython/{0}.rst'.format(docname),
'#doc/sphinx/cython/example-script.rst.in')
build(b)
localenv.Depends(sphinxdocs, b)
indexenv['python_{0}_examples'.format(subdir)] = '\n'.join(summaries)
b = indexenv.SubstFile('#doc/sphinx/cython/examples.rst',
'#doc/sphinx/cython/examples.rst.in')
build(b)
localenv.Depends(sphinxdocs, b)
# Create a list of MATLAB classes to document. This uses the NamedTuple
# structure defined at the top of the file. The @Data and @Utilities

View file

@ -1,3 +1,5 @@
:orphan:
.. _py-example-@script_name@:
@script_name@

View file

@ -14,49 +14,35 @@ installation directory. To determine the location of this directory, run the fol
Thermodynamics
--------------
.. toctree::
:glob:
examples/thermo*
@python_thermo_examples@
Kinetics
--------
.. toctree::
:glob:
examples/kinetics*
@python_kinetics_examples@
Transport
---------
.. toctree::
:glob:
examples/transport*
@python_transport_examples@
Reactor Networks
----------------
.. toctree::
:glob:
examples/reactors*
@python_reactors_examples@
One-dimensional Flames
----------------------
.. toctree::
:glob:
examples/onedim*
@python_onedim_examples@
Multiphase Mixtures
-------------------
.. toctree::
:glob:
examples/multiphase*
@python_multiphase_examples@
Surface Chemistry
-----------------
.. toctree::
:glob:
examples/surface_chemistry*
@python_surface_chemistry_examples@