def generate_file_rst(fname, target_dir, src_dir, plot_gallery):
base_image_name = os.path.splitext(fname)[<NUM_LIT:0>]<EOL>image_fname = '<STR_LIT>' % base_image_name<EOL>root_image_fname = '<STR_LIT>' % base_image_name<EOL>root_fig_num = <NUM_LIT:1><EOL>this_template = rst_template<EOL>last_dir = os.path.split(src_dir)[-<NUM_LIT:1>]<EOL>if last_dir == '<STR_LIT:.>' or last_dir == '<STR_LIT>':<EOL><INDENT>last_dir = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>last_dir += '<STR_LIT:_>'<EOL><DEDENT>short_fname = last_dir + fname<EOL>src_file = os.path.join(src_dir, fname)<EOL>example_file = os.path.join(target_dir, fname)<EOL>shutil.copyfile(src_file, example_file)<EOL>figure_list = []<EOL>image_dir = os.path.join(target_dir, '<STR_LIT>')<EOL>thumb_dir = os.path.join(image_dir, '<STR_LIT>')<EOL>if not os.path.exists(image_dir):<EOL><INDENT>os.makedirs(image_dir)<EOL><DEDENT>if not os.path.exists(thumb_dir):<EOL><INDENT>os.makedirs(thumb_dir)<EOL><DEDENT>image_path = os.path.join(image_dir, image_fname)<EOL>root_image_path = os.path.join(image_dir, root_image_fname)<EOL>stdout_path = os.path.join(image_dir,<EOL>'<STR_LIT>' % base_image_name)<EOL>time_path = os.path.join(image_dir,<EOL>'<STR_LIT>' % base_image_name)<EOL>thumb_file = os.path.join(thumb_dir, fname[:-<NUM_LIT:3>] + '<STR_LIT>')<EOL>time_elapsed = <NUM_LIT:0><EOL>if plot_gallery and fname.startswith('<STR_LIT>'):<EOL><INDENT>first_image_file = image_path % <NUM_LIT:1><EOL>first_root_image_file = root_image_path % <NUM_LIT:1><EOL>if os.path.exists(stdout_path):<EOL><INDENT>stdout = open(stdout_path).read()<EOL><DEDENT>else:<EOL><INDENT>stdout = '<STR_LIT>'<EOL><DEDENT>if os.path.exists(time_path):<EOL><INDENT>time_elapsed = float(open(time_path).read())<EOL><DEDENT>if (not os.path.exists(first_image_file) or<EOL>not os.path.exists(first_root_image_file) or<EOL>os.stat(first_image_file).st_mtime <=<EOL>os.stat(src_file).st_mtime):<EOL><INDENT>print('<STR_LIT>' % fname)<EOL>t0 = time()<EOL>import matplotlib.pyplot as plt<EOL>plt.close('<STR_LIT:all>')<EOL>cwd = os.getcwd()<EOL>try:<EOL><INDENT>orig_stdout = sys.stdout<EOL>os.chdir(os.path.dirname(src_file))<EOL>my_buffer = StringIO()<EOL>my_stdout = Tee(sys.stdout, my_buffer)<EOL>sys.stdout = my_stdout<EOL>my_globals = {'<STR_LIT>': plt}<EOL>execfile(os.path.basename(src_file), my_globals)<EOL>time_elapsed = time() - t0<EOL>sys.stdout = orig_stdout<EOL>my_stdout = my_buffer.getvalue()<EOL>if '<STR_LIT>' in my_globals:<EOL><INDENT>my_stdout = my_stdout.replace(<EOL>my_globals['<STR_LIT>'],<EOL>'<STR_LIT>')<EOL><DEDENT>my_stdout = my_stdout.strip()<EOL>if my_stdout:<EOL><INDENT>stdout = '<STR_LIT>' % (<EOL>'<STR_LIT>'.join(my_stdout.split('<STR_LIT:\n>')))<EOL><DEDENT>open(stdout_path, '<STR_LIT:w>').write(stdout)<EOL>open(time_path, '<STR_LIT:w>').write('<STR_LIT>' % time_elapsed)<EOL>os.chdir(cwd)<EOL>for fig_num in (fig_mngr.num for fig_mngr in<EOL>matplotlib._pylab_helpers.Gcf.get_all_fig_managers()):<EOL><INDENT>plt.figure(fig_num)<EOL>plt.savefig(image_path % fig_num)<EOL>figure_list.append(image_fname % fig_num)<EOL><DEDENT>for canvas in ROOT.gROOT.GetListOfCanvases():<EOL><INDENT>maybe_root_filename = os.path.join(os.path.dirname(src_file), canvas.name)<EOL>if os.path.isfile(maybe_root_filename):<EOL><INDENT>os.rename(maybe_root_filename, os.path.join(image_dir, canvas.name))<EOL>figure_list.append(canvas.name)<EOL>canvas.Close()<EOL><DEDENT>else:<EOL><INDENT>canvas.SaveAs(root_image_path % root_fig_num)<EOL>canvas.Close()<EOL>figure_list.append(root_image_fname % root_fig_num)<EOL>root_fig_num += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>except:<EOL><INDENT>print(<NUM_LIT> * '<STR_LIT:_>')<EOL>print('<STR_LIT>' % fname)<EOL>traceback.print_exc()<EOL>print(<NUM_LIT> * '<STR_LIT:_>')<EOL><DEDENT>finally:<EOL><INDENT>os.chdir(cwd)<EOL>sys.stdout = orig_stdout<EOL><DEDENT>print("<STR_LIT>" % time_elapsed)<EOL><DEDENT>else:<EOL><INDENT>figure_list = [f[len(image_dir):]<EOL>for f in glob.glob(image_path % '<STR_LIT>')]<EOL><DEDENT>this_template = plot_rst_template<EOL>from matplotlib import image<EOL>if os.path.exists(first_image_file):<EOL><INDENT>image.thumbnail(first_image_file, thumb_file, <NUM_LIT>)<EOL><DEDENT>elif os.path.exists(first_root_image_file):<EOL><INDENT>image.thumbnail(first_root_image_file, thumb_file, <NUM_LIT>)<EOL><DEDENT><DEDENT>if not os.path.exists(thumb_file):<EOL><INDENT>shutil.copy('<STR_LIT>', thumb_file)<EOL><DEDENT>docstring, short_desc, end_row = extract_docstring(example_file)<EOL>if len(figure_list) == <NUM_LIT:1>:<EOL><INDENT>figure_name = figure_list[<NUM_LIT:0>]<EOL>image_list = SINGLE_IMAGE % figure_name.lstrip('<STR_LIT:/>')<EOL><DEDENT>else:<EOL><INDENT>image_list = HLIST_HEADER<EOL>for figure_name in figure_list:<EOL><INDENT>image_list += HLIST_IMAGE_TEMPLATE % figure_name.lstrip('<STR_LIT:/>')<EOL><DEDENT><DEDENT>f = open(os.path.join(target_dir, fname[:-<NUM_LIT:2>] + '<STR_LIT>'), '<STR_LIT:w>')<EOL>f.write(this_template % locals())<EOL>f.flush()<EOL>
Generate the rst file for a given example.