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