forked from mikehulluk/morphforge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMF_create_example_docs.py
More file actions
210 lines (127 loc) · 4.97 KB
/
Copy pathMF_create_example_docs.py
File metadata and controls
210 lines (127 loc) · 4.97 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import os
import shutil
import shlex
import subprocess
import glob
import re
import itertools
from glob import glob as Glob
from os.path import join as Join
from Cheetah.Template import Template
import mreorg
from morphforge.core.mgrs.locmgr import LocMgr
root = os.path.normpath( os.path.join( LocMgr.get_root_path(), "..") )
examples_src_dir = os.path.join(root, "src/morphforgeexamples/")
#"/home/michael/hw/morphforge/doc"
doc_src_dir = os.path.normpath( os.path.join(root, "doc") )
examples_dst_dir = os.path.join(root, "doc/srcs_generated_examples")
examples_dst_dir_images = os.path.join(root, "doc/srcs_generated_examples/images/")
examples_build_dir = os.path.join( LocMgr.get_tmp_path(), "mf_doc_build")
examples_build_dir_image_out = os.path.join( examples_build_dir, "images/")
dirs = ['morphology', 'singlecell_simulation', 'multicell_simulation', 'advanced_examples']#, 'assorted' ]
example_subdirs = [ d for d in os.listdir(examples_src_dir) if d.startswith("""exset""") ]
dirs = sorted(example_subdirs)
example_srcs = list( itertools.chain( *[ sorted(Glob( Join(examples_src_dir, dir) + "/*.py") ) for dir in dirs] ) )
def clear_directory(d):
if os.path.exists(d):
shutil.rmtree(d)
os.mkdir(d)
def parse_src_file(filename, docstring):
d = open(filename, 'r').read()
# Remove copyright notice:
d = re.split("""[#]\s?[-]+""", d)[-1]
# Remove the docstring:
if docstring is not None:
raw_docstring = r'''"""\s*%s\s*"""''' % re.escape(docstring).strip()
d = re.sub(raw_docstring, '', d, re.MULTILINE)
return d
rstTmpl = """
$title
$titleunderline
$docstring
Code
~~~~
.. code-block:: python
$code
#if $figures
Figures
~~~~~~~~
#for $im in $figures
.. figure:: $im
:width: 3in
:figwidth: 4in
Download :download:`Figure <$im>`
#end for
#end if
Output
~~~~~~
.. code-block:: bash
$output
"""
def make_rst_output(index, examples_filename, src_code, output_images, docstring, output):
name_short = os.path.split(examples_filename)[1]
name_short = os.path.splitext(name_short)[0]
# Copy the image files accross:
im_names = []
for im in output_images:
im_newName = os.path.join(examples_dst_dir_images, "%s_%s"%(name_short, os.path.split(im)[-1]))
shutil.copyfile(im, im_newName)
im_newName_short = im_newName.replace(doc_src_dir, "") #/home/michael/hw/morphforge/doc", "")
im_names.append(im_newName_short)
title = [ l.strip() for l in docstring.split(".")[0].split("\n") if l.strip() ] [0] if docstring else None
title = title or '<Missing Docstring>'
# Prefix the title:
title = "%d. "%(index+1) +title
# Create the rst:
context = {
'title':title,
'titleunderline':"="*len(title),
'docstring': docstring,
'code' : "\n".join( ["\t"+l for l in src_code.split("\n")] ),
'figures':im_names,
'output' : "\n".join( ["\t"+l for l in output.split("\n")] ),
}
s = Template(rstTmpl, context).respond()
op_rst_filename = os.path.join( examples_dst_dir, name_short+".rst")
with open(op_rst_filename, 'w') as fOut:
fOut.write(s)
saveCodeTmpl = """
import matplotlib
import pylab
for fig_num, fig_mgr in matplotlib._pylab_helpers.Gcf.figs.iteritems():
matplotlib._pylab_helpers.Gcf.set_active(fig_mgr)
pylab.savefig("{{OUTDIR}}out%d.png" % fig_num, facecolor='lightgrey')
"""
def run_example(index, filename):
print 'Running Example:', filename
newFilename = os.path.join( examples_build_dir, os.path.split(filename)[1] )
# Clear the directory
clear_directory(examples_build_dir)
clear_directory(examples_build_dir_image_out)
#Create the python file to run:
with open(newFilename, 'w') as fOut:
fOut.write( open(filename).read() )
saveCode = saveCodeTmpl.replace("{{OUTDIR}}", examples_build_dir_image_out)
fOut.write(saveCode)
# run the file, and capture the output:
# Turn off plotting:
env = os.environ.copy()
env['MREORG_BATCHRUN'] = "True"
args = shlex.split("""python %s"""%newFilename)
#print 'Launching child process', args
result = subprocess.check_output(args, stderr=subprocess.STDOUT, env=env)
# Split the output to get at the docstring:
output = result
docstring = mreorg.utils.extract_docstring_from_fileobj( open(filename))
# Get the images:
images = glob.glob(examples_build_dir_image_out + "/*")
# Clean up the source code:
src_code = parse_src_file(filename, docstring)
# Create the Output RST File:
make_rst_output( index=index, examples_filename=filename, src_code=src_code, output_images=images, docstring=docstring, output=output)
# Start with an empty directory:
clear_directory(examples_dst_dir)
clear_directory(examples_dst_dir_images)
for index, fName in enumerate(example_srcs):
fName_full = os.path.join(examples_src_dir, fName)
run_example(index, fName_full)