Skip to content

Commit 8d8dc67

Browse files
committed
enh: refined example building so that one can download the code
1 parent 159628f commit 8d8dc67

File tree

9 files changed

+192
-187
lines changed

9 files changed

+192
-187
lines changed

doc/Makefile

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ help:
2626
@echo " sf_satra copy html files to sourceforge (satra only)"
2727

2828
clean:
29-
-rm -rf _build/* *~ api/generated documentation.zip
29+
-rm -rf _build/* *~ api/generated users/examples documentation.zip
3030

3131
htmlonly:
3232
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) _build/html
@@ -45,25 +45,8 @@ html: examples2rst api htmlonly
4545
stty sane
4646

4747
examples2rst:
48-
python ../tools/ex2rst \
49-
--project Nipype \
50-
--outdir $(CURDIR)/users/examples \
51-
../examples/freesurfer_tutorial.py \
52-
../examples/fsl_tutorial2.py \
53-
../examples/fsl_feeds_tutorial.py \
54-
../examples/fsl_resting_compcorr.py \
55-
../examples/spm_tutorial.py \
56-
../examples/spm_face_tutorial.py \
57-
../examples/spm_auditory_tutorial.py \
58-
../examples/fsl_dti_tutorial.py \
59-
../examples/camino_dti_tutorial.py \
60-
../examples/connectivity_tutorial.py \
61-
../examples/dtk_dti_tutorial.py \
62-
../examples/dtk_odf_tutorial.py \
63-
../examples/spm_tutorial2.py \
64-
../examples/spm_dartel_tutorial.py \
65-
../examples/dartmouth_workshop_2010.py \
66-
../examples/nipy_tutorial.py
48+
mkdir -p users/examples
49+
../tools/make_examples.py --no-exec
6750
@echo "examples2rst finished."
6851

6952
latex: api

doc/users/parallel_processing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ To prevent prevent parallel execution type::
7373
workflow.run(plugin='Linear')
7474

7575
Using the pipeline engine with SGE/OGE/PBS
76-
--------------------------------------
76+
------------------------------------------
7777

7878
In order to use nipype with SGE_/OGE_ (not tested) or PBS_ you simply need to
7979
call::

examples/frontiers_paper/smoothing_comparison.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
2+
# vi: set ft=python sts=4 ts=4 sw=4 et:
13
"""
24
=============================
35
Smoothing comparison workflow

examples/frontiers_paper/workflow_from_scratch.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
2+
# vi: set ft=python sts=4 ts=4 sw=4 et:
13
"""
24
=====================
35
Workflow from scratch

examples/slicer_tutorial.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# vi: set ft=python sts=4 ts=4 sw=4 et:
33
"""
44
Using slicer for analysis
5-
=======================
5+
=========================
66
77
The slicer_tutorial.py
88
@@ -14,6 +14,7 @@
1414
python slicer_tutorial.py
1515
1616
"""
17+
1718
raise RuntimeWarning, 'Slicer not fully implmented'
1819
from nipype.interfaces.slicer import SlicerCommandLine
1920

examples/tbss_tutorial.py

Lines changed: 0 additions & 103 deletions
This file was deleted.

tools/ex2rst

Lines changed: 59 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
#!/usr/bin/env python
2-
"""Helper to automagically generate ReST versions of examples.
3-
4-
The MIT License
5-
6-
Copyright (c) 2006-2009 Michael Hanke
7-
2007-2009 Yaroslav Halchenko
8-
9-
Permission is hereby granted, free of charge, to any person obtaining a copy
10-
of this software and associated documentation files (the "Software"), to deal
11-
in the Software without restriction, including without limitation the rights
12-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13-
copies of the Software, and to permit persons to whom the Software is
14-
furnished to do so, subject to the following conditions:
15-
16-
The above copyright notice and this permission notice shall be included in
17-
all copies or substantial portions of the Software.
18-
19-
"""
2+
#
3+
# Note: this file is copied (possibly with minor modifications) from the
4+
# sources of the PyMVPA project - http://pymvpa.org. It remains licensed as
5+
# the rest of PyMVPA (MIT license as of October 2010).
6+
#
7+
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
8+
#
9+
# See COPYING file distributed along with the PyMVPA package for the
10+
# copyright and license terms.
11+
#
12+
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
13+
14+
"""Helper to automagically generate ReST versions of examples"""
2015

2116
__docformat__ = 'restructuredtext'
2217

@@ -28,6 +23,31 @@ import glob
2823
from optparse import OptionParser
2924

3025

26+
def auto_image(line):
27+
"""Automatically replace generic image markers with ones that have full
28+
size (width/height) info, plus a :target: link to the original png, to be
29+
used in the html docs.
30+
"""
31+
img_re = re.compile(r'(\s*)\.\. image::\s*(.*)$')
32+
m = img_re.match(line)
33+
if m is None:
34+
# Not an image declaration, leave the line alone and return unmodified
35+
return line
36+
37+
# Match means it's an image spec, we rewrite it with extra tags
38+
ini_space = m.group(1)
39+
lines = [line,
40+
ini_space + ' :width: 500\n',
41+
#ini_space + ' :height: 350\n'
42+
]
43+
fspec = m.group(2)
44+
if fspec.endswith('.*'):
45+
fspec = fspec.replace('.*', '.png')
46+
fspec = fspec.replace('fig/', '../_images/')
47+
lines.append(ini_space + (' :target: %s\n' % fspec) )
48+
lines.append('\n')
49+
return ''.join(lines)
50+
3151
def exfile2rst(filename):
3252
"""Open a Python script and convert it into an ReST string.
3353
"""
@@ -42,8 +62,6 @@ def exfile2rst(filename):
4262
indocs = False
4363
doc2code = False
4464
code2doc = False
45-
interactiveblock = False
46-
preserved_indent = False
4765
# an empty line found in the example enables the check for a potentially
4866
# indented docstring starting on the next line (as an attempt to exclude
4967
# function or class docstrings)
@@ -67,7 +85,6 @@ def exfile2rst(filename):
6785
cleanline = line[:line.find('#')].lstrip()
6886
indent_level = len(line) - len(cleanline) - 1
6987
cleanline = cleanline.rstrip()
70-
interactiveblock = False
7188
else:
7289
cleanline = line[:line.find('#')].rstrip()
7390

@@ -92,19 +109,16 @@ def exfile2rst(filename):
92109
proc_line = cleanline[3:-3]
93110
else:
94111
# must be start of multiline block
95-
interactiveblock = False
96112
indocs = True
97113
code2doc = True
98114
# rescue what is left on the line
99115
proc_line = cleanline[3:] # strip """
100-
first_codeline_in_block = False
101116
else:
102117
# we are already in the docs
103118
# handle doc end
104119
if cleanline.endswith('"""') or cleanline.endswith("'''"):
105120
indocs = False
106121
doc2code = True
107-
preserved_indent = False
108122
# rescue what is left on the line
109123
proc_line = cleanline[:-3]
110124
# reset the indentation
@@ -122,40 +136,18 @@ def exfile2rst(filename):
122136
code2doc = False
123137
s += '\n'
124138

139+
proc_line = auto_image(proc_line)
140+
125141
if proc_line:
126142
s += proc_line.rstrip() + '\n'
127143

128144
else:
129-
# we are in a code block
130-
if line.startswith("if cfg.getboolean('examples', 'interactive'"):
131-
interactiveblock = True
132-
elif not interactiveblock:
133-
# we exclude the code that is used to disable the interactive
134-
# plots.
135-
if doc2code:
136-
doc2code = False
137-
first_codeline_in_block = True
138-
# the spaces at the end are crucial to get intentation of
139-
# indented code block -- otherwise sphinx removes common
140-
# whitespace
141-
s += '\n::\n'
142-
143-
# has to be code
144-
leading_whitespace = len(line) > 2 \
145-
and len(line) > len(line.lstrip())
146-
line = line.rstrip()
147-
# anything left?
148-
if line:
149-
if first_codeline_in_block and leading_whitespace \
150-
and not preserved_indent:
151-
s += ' >>%s\n' % line
152-
preserved_indent = True
153-
else:
154-
s += ' %s\n' % line
155-
156-
first_codeline_in_block = False
157-
else:
158-
s += '\n'
145+
if doc2code:
146+
doc2code = False
147+
s += '\n::\n'
148+
149+
# has to be code
150+
s += ' %s' % line
159151

160152
xfile.close()
161153

@@ -179,15 +171,20 @@ def exfile2rstfile(filename, opts):
179171

180172
# write converted ReST
181173
dfile.write(exfile2rst(filename))
182-
183-
# add links
184-
dfile.write('.. include:: ../../links_names.txt\n\n')
185174

186175
if opts.sourceref:
187176
# write post example see also box
188-
dfile.write("\n.. seealso::\n The full source code of this example is "
189-
"included in the %s source distribution (`%s`).\n"
190-
% (opts.project, filename))
177+
msg = """
178+
179+
.. admonition:: Example source code
180+
181+
You can download :download:`the full source code of this example <%s>`.
182+
This same script is also included in the %s source distribution under the
183+
:file:`examples` directory.
184+
185+
""" % (filename, opts.project)
186+
187+
dfile.write(msg)
191188

192189
dfile.close()
193190

@@ -215,7 +212,7 @@ the respective indentation is removed in the ReST output.
215212
216213
The parser algorithm automatically excludes file headers and starts with the
217214
first (module-level) docstring instead.
218-
""" )
215+
""" ) #'
219216

220217
# define options
221218
parser.add_option('--verbose', action='store_true', dest='verbose',
@@ -269,7 +266,7 @@ Name of the project that contains the examples. This name is used in the
269266
print('Ignoring duplicate parse targets.')
270267

271268
if not os.path.exists(opts.outdir):
272-
os.mkdir(opts.outdir)
269+
os.mkdir(outdir)
273270

274271
# finally process all examples
275272
for t in toparse:

0 commit comments

Comments
 (0)