3
generate the rst files for the examples by iterating over the pylab examples
5
# This code was developed from the Matplotlib gen_rst.py module
6
# and is distributed with the same license as Matplotlib
13
#rootdir = '../../examples'
17
def out_of_date(original, derived):
19
Returns True if derivative is out-of-date wrt original,
20
both of which are full file paths.
22
TODO: this check isn't adequate in some cases. Eg, if we discover
23
a bug when building the examples, the original and derived
24
will be unchanged but we still want to fource a rebuild. We can
25
manually remove from _static, but we may need another solution
27
return (not os.path.exists(derived) or
28
os.stat(derived).st_mtime < os.stat(original).st_mtime)
30
def main(exampledir,sourcedir):
32
noplot_regex = re.compile(r"#\s*-\*-\s*noplot\s*-\*-")
35
for root, subFolders, files in os.walk(exampledir):
37
if ( fname.startswith('.') or fname.startswith('#') or fname.startswith('_') or
38
fname.find('.svn')>=0 or not fname.endswith('.py') ):
41
fullpath = os.path.join(root,fname)
42
contents = file(fullpath).read()
44
relpath = os.path.split(root)[-1]
45
datad.setdefault(relpath, []).append((fullpath, fname, contents))
47
subdirs = datad.keys()
49
output_dir=os.path.join(sourcedir,'examples')
50
if not os.path.exists(output_dir):
51
os.makedirs(output_dir)
52
fhindex = file(os.path.join(sourcedir,'examples','index.rst'), 'w')
70
for subdir in subdirs:
71
output_dir= os.path.join(sourcedir,'examples',subdir)
72
if not os.path.exists(output_dir):
73
os.makedirs(output_dir)
75
static_dir = os.path.join(sourcedir, 'static', 'examples')
76
if not os.path.exists(static_dir):
77
os.makedirs(static_dir)
80
subdirIndexFile = os.path.join(subdir, 'index.rst')
81
fhsubdirIndex = file(os.path.join(output_dir,'index.rst'), 'w')
82
fhindex.write(' %s\n\n'%subdirIndexFile)
83
#thumbdir = '../_static/plot_directive/mpl_examples/%s/thumbnails/'%subdir
84
#for thumbname in glob.glob(os.path.join(thumbdir,'*.png')):
85
# fhindex.write(' %s\n'%thumbname)
87
fhsubdirIndex.write("""\
88
.. _%s-examples-index:
91
##############################################
93
##############################################
103
"""%(subdir, subdir.title()))
109
#parts = os.path.split(static_dir)
110
#thumb_dir = ('../'*(len(parts)-1)) + os.path.join(static_dir, 'thumbnails')
112
for fullpath, fname, contents in data:
113
basename, ext = os.path.splitext(fname)
114
static_file = os.path.join(static_dir, fname)
115
#thumbfile = os.path.join(thumb_dir, '%s.png'%basename)
116
#print ' static_dir=%s, basename=%s, fullpath=%s, fname=%s, thumb_dir=%s, thumbfile=%s'%(static_dir, basename, fullpath, fname, thumb_dir, thumbfile)
118
rstfile = '%s.rst'%basename
119
outfile = os.path.join(output_dir, rstfile)
121
fhsubdirIndex.write(' %s\n'%rstfile)
123
if (not out_of_date(fullpath, static_file) and
124
not out_of_date(fullpath, outfile)):
127
print '%s/%s'%(subdir,fname)
129
fhstatic = file(static_file, 'w')
130
fhstatic.write(contents)
133
fh = file(outfile, 'w')
134
fh.write('.. _%s-%s:\n\n'%(subdir, basename))
135
base=fname.partition('.')[0]
136
title = '%s'%(base.replace('_',' ').title())
139
#title = '<img src=%s> %s example code: %s'%(thumbfile, subdir, fname)
142
fh.write(title + '\n')
143
fh.write('='*len(title) + '\n\n')
146
png=os.path.join(static_dir,pngname)
147
linkname = os.path.join('..', '..', 'static', 'examples')
148
if os.path.exists(png):
149
fh.write('.. image:: %s \n\n'%os.path.join(linkname,pngname))
150
linkname = os.path.join('..', '..', '_static', 'examples')
151
fh.write("[`source code <%s>`_]\n\n::\n\n" % os.path.join(linkname,fname))
153
# indent the contents
154
contents = '\n'.join([' %s'%row.rstrip() for row in contents.split('\n')])
157
# fh.write('\n\nKeywords: python, matplotlib, pylab, example, codex (see :ref:`how-to-search-examples`)')
160
fhsubdirIndex.close()
164
if __name__ == '__main__':
166
main(sys.argv[1],sys.argv[2])