1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
"""
generate the rst files for the examples by iterating over the pylab examples
"""
import os, glob
import os
import re
import sys
fileList = []
def out_of_date(original, derived):
"""
Returns True if derivative is out-of-date wrt original,
both of which are full file paths.
TODO: this check isn't adequate in some cases. Eg, if we discover
a bug when building the examples, the original and derived will be
unchanged but we still want to force a rebuild.
"""
return (not os.path.exists(derived) or
os.stat(derived).st_mtime < os.stat(original).st_mtime)
noplot_regex = re.compile(r"#\s*-\*-\s*noplot\s*-\*-")
def generate_example_rst(app):
rootdir = os.path.join(app.builder.srcdir, 'mpl_examples')
exampledir = os.path.join(app.builder.srcdir, 'examples')
if not os.path.exists(exampledir):
os.makedirs(exampledir)
datad = {}
for root, subFolders, files in os.walk(rootdir):
for fname in files:
if ( fname.startswith('.') or fname.startswith('#') or fname.startswith('_') or
fname.find('.svn')>=0 or not fname.endswith('.py') ):
continue
fullpath = os.path.join(root,fname)
contents = file(fullpath).read()
# indent
relpath = os.path.split(root)[-1]
datad.setdefault(relpath, []).append((fullpath, fname, contents))
subdirs = datad.keys()
subdirs.sort()
fhindex = file(os.path.join(exampledir, 'index.rst'), 'w')
fhindex.write("""\
.. _examples-index:
####################
Matplotlib Examples
####################
.. htmlonly::
:Release: |version|
:Date: |today|
.. toctree::
:maxdepth: 2
""")
for subdir in subdirs:
rstdir = os.path.join(exampledir, subdir)
if not os.path.exists(rstdir):
os.makedirs(rstdir)
outputdir = os.path.join(app.builder.outdir, 'examples')
if not os.path.exists(outputdir):
os.makedirs(outputdir)
outputdir = os.path.join(outputdir, subdir)
if not os.path.exists(outputdir):
os.makedirs(outputdir)
subdirIndexFile = os.path.join(rstdir, 'index.rst')
fhsubdirIndex = file(subdirIndexFile, 'w')
fhindex.write(' %s/index.rst\n\n'%subdir)
fhsubdirIndex.write("""\
.. _%s-examples-index:
##############################################
%s Examples
##############################################
.. htmlonly::
:Release: |version|
:Date: |today|
.. toctree::
:maxdepth: 1
"""%(subdir, subdir))
sys.stdout.write(subdir + ", ")
sys.stdout.flush()
data = datad[subdir]
data.sort()
for fullpath, fname, contents in data:
basename, ext = os.path.splitext(fname)
outputfile = os.path.join(outputdir, fname)
#thumbfile = os.path.join(thumb_dir, '%s.png'%basename)
#print ' static_dir=%s, basename=%s, fullpath=%s, fname=%s, thumb_dir=%s, thumbfile=%s'%(static_dir, basename, fullpath, fname, thumb_dir, thumbfile)
rstfile = '%s.rst'%basename
outrstfile = os.path.join(rstdir, rstfile)
fhsubdirIndex.write(' %s\n'%rstfile)
if not out_of_date(fullpath, outrstfile):
continue
fh = file(outrstfile, 'w')
fh.write('.. _%s-%s:\n\n'%(subdir, basename))
title = '%s example code: %s'%(subdir, fname)
#title = '<img src=%s> %s example code: %s'%(thumbfile, subdir, fname)
fh.write(title + '\n')
fh.write('='*len(title) + '\n\n')
do_plot = (subdir in ('api',
'pylab_examples',
'units',
'mplot3d',
'axes_grid',
) and
not noplot_regex.search(contents))
if do_plot:
fh.write("\n\n.. plot:: %s\n\n::\n\n" % fullpath)
else:
fh.write("[`source code <%s>`_]\n\n::\n\n" % fname)
fhstatic = file(outputfile, 'w')
fhstatic.write(contents)
fhstatic.close()
# indent the contents
contents = '\n'.join([' %s'%row.rstrip() for row in contents.split('\n')])
fh.write(contents)
fh.write('\n\nKeywords: python, matplotlib, pylab, example, codex (see :ref:`how-to-search-examples`)')
fh.close()
fhsubdirIndex.close()
fhindex.close()
print
def setup(app):
app.connect('builder-inited', generate_example_rst)
|