<s> """<STR_LIT>""" <EOL> from __future__ import division , print_function , absolute_import <EOL> from time import time <EOL> import ast <EOL> import hashlib <EOL> import os <EOL> import re <EOL> import shutil <EOL> import subprocess <EOL> import sys <EOL> import traceback <EOL> import warnings <EOL> from textwrap import dedent <EOL> try : <EOL> from textwrap import indent <EOL> except ImportError : <EOL> def indent ( text , prefix , predicate = None ) : <EOL> """<STR_LIT>""" <EOL> if predicate is None : <EOL> def predicate ( line ) : <EOL> return line . strip ( ) <EOL> def prefixed_lines ( ) : <EOL> for line in text . splitlines ( True ) : <EOL> yield ( prefix + line if predicate ( line ) else line ) <EOL> return '<STR_LIT>' . join ( prefixed_lines ( ) ) <EOL> try : <EOL> from StringIO import StringIO <EOL> except ImportError : <EOL> from io import StringIO <EOL> try : <EOL> import matplotlib <EOL> matplotlib . use ( '<STR_LIT>' ) <EOL> import matplotlib . pyplot as plt <EOL> except ImportError : <EOL> pass <EOL> from . import glr_path_static <EOL> from . backreferences import write_backreferences , _thumbnail_div <EOL> from . notebook import Notebook <EOL> try : <EOL> basestring <EOL> except NameError : <EOL> basestring = str <EOL> class Tee ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , file1 , file2 ) : <EOL> self . file1 = file1 <EOL> self . file2 = file2 <EOL> def write ( self , data ) : <EOL> self . file1 . write ( data ) <EOL> self . file2 . write ( data ) <EOL> def flush ( self ) : <EOL> self . file1 . flush ( ) <EOL> self . file2 . flush ( ) <EOL> CODE_DOWNLOAD = """<STR_LIT>""" <EOL> HLIST_HEADER = """<STR_LIT>""" <EOL> HLIST_IMAGE_TEMPLATE = """<STR_LIT>""" <EOL> SINGLE_IMAGE = """<STR_LIT>""" <EOL> CODE_OUTPUT = """<STR_LIT>""" <EOL> def get_docstring_and_rest ( filename ) : <EOL> """<STR_LIT>""" <EOL> with open ( filename ) as f : <EOL> content = f . read ( ) <EOL> node = ast . parse ( content ) <EOL> if not isinstance ( node , ast . Module ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( node . __class__ . __name__ ) ) <EOL> if node . body and isinstance ( node . body [ <NUM_LIT:0> ] , ast . Expr ) and isinstance ( node . body [ <NUM_LIT:0> ] . value , ast . Str ) : <EOL> docstring_node = node . body [ <NUM_LIT:0> ] <EOL> docstring = docstring_node . value . s <EOL> rest = content . split ( '<STR_LIT:\n>' , docstring_node . lineno ) [ - <NUM_LIT:1> ] <EOL> return docstring , rest <EOL> else : <EOL> raise ValueError ( ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> . format ( filename ) ) <EOL> def split_code_and_text_blocks ( source_file ) : <EOL> """<STR_LIT>""" <EOL> docstring , rest_of_content = get_docstring_and_rest ( source_file ) <EOL> blocks = [ ( '<STR_LIT:text>' , docstring ) ] <EOL> pattern = re . compile ( <EOL> r'<STR_LIT>' , <EOL> flags = re . M ) <EOL> pos_so_far = <NUM_LIT:0> <EOL> for match in re . finditer ( pattern , rest_of_content ) : <EOL> match_start_pos , match_end_pos = match . span ( ) <EOL> code_block_content = rest_of_content [ pos_so_far : match_start_pos ] <EOL> text_content = match . group ( '<STR_LIT>' ) <EOL> sub_pat = re . compile ( '<STR_LIT>' , flags = re . M ) <EOL> text_block_content = dedent ( re . sub ( sub_pat , '<STR_LIT>' , text_content ) ) <EOL> if code_block_content . strip ( ) : <EOL> blocks . append ( ( '<STR_LIT:code>' , code_block_content ) ) <EOL> if text_block_content . strip ( ) : <EOL> blocks . append ( ( '<STR_LIT:text>' , text_block_content ) ) <EOL> pos_so_far = match_end_pos <EOL> remaining_content = rest_of_content [ pos_so_far : ] <EOL> if remaining_content . strip ( ) : <EOL> blocks . append ( ( '<STR_LIT:code>' , remaining_content ) ) <EOL> return blocks <EOL> def codestr2rst ( codestr , lang = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> code_directive = "<STR_LIT>" . format ( lang ) <EOL> indented_block = indent ( codestr , '<STR_LIT:U+0020>' * <NUM_LIT:4> ) <EOL> return code_directive + indented_block <EOL> def text2string ( content ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return ast . literal_eval ( content ) + '<STR_LIT:\n>' <EOL> except Exception : <EOL> return content <EOL> def extract_intro ( filename ) : <EOL> """<STR_LIT>""" <EOL> docstring , _ = get_docstring_and_rest ( filename ) <EOL> paragraphs = docstring . lstrip ( ) . split ( '<STR_LIT>' ) <EOL> if len ( paragraphs ) > <NUM_LIT:1> : <EOL> first_paragraph = re . sub ( '<STR_LIT:\n>' , '<STR_LIT:U+0020>' , paragraphs [ <NUM_LIT:1> ] ) <EOL> first_paragraph = ( first_paragraph [ : <NUM_LIT> ] + '<STR_LIT>' <EOL> if len ( first_paragraph ) > <NUM_LIT> else first_paragraph ) <EOL> else : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( filename ) ) <EOL> return first_paragraph <EOL> def get_md5sum ( src_file ) : <EOL> """<STR_LIT>""" <EOL> with open ( src_file , '<STR_LIT:r>' ) as src_data : <EOL> src_content = src_data . read ( ) <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> : <EOL> src_content = src_content . encode ( '<STR_LIT:utf-8>' ) <EOL> src_md5 = hashlib . md5 ( src_content ) . hexdigest ( ) <EOL> return src_md5 <EOL> def check_md5sum_change ( src_file ) : <EOL> """<STR_LIT>""" <EOL> src_md5 = get_md5sum ( src_file ) <EOL> src_md5_file = src_file + '<STR_LIT>' <EOL> src_file_changed = True <EOL> if os . path . exists ( src_md5_file ) : <EOL> with open ( src_md5_file , '<STR_LIT:r>' ) as file_checksum : <EOL> ref_md5 = file_checksum . read ( ) <EOL> if src_md5 == ref_md5 : <EOL> src_file_changed = False <EOL> if src_file_changed : <EOL> with open ( src_md5_file , '<STR_LIT:w>' ) as file_checksum : <EOL> file_checksum . write ( src_md5 ) <EOL> return src_file_changed <EOL> def _plots_are_current ( src_file , image_file ) : <EOL> """<STR_LIT>""" <EOL> first_image_file = image_file . format ( <NUM_LIT:1> ) <EOL> has_image = os . path . exists ( first_image_file ) <EOL> src_file_changed = check_md5sum_change ( src_file ) <EOL> return has_image and not src_file_changed <EOL> def save_figures ( image_path , fig_count , gallery_conf ) : <EOL> """<STR_LIT>""" <EOL> figure_list = [ ] <EOL> fig_managers = matplotlib . _pylab_helpers . Gcf . get_all_fig_managers ( ) <EOL> for fig_mngr in fig_managers : <EOL> fig = plt . figure ( fig_mngr . num ) <EOL> kwargs = { } <EOL> to_rgba = matplotlib . colors . colorConverter . to_rgba <EOL> for attr in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> fig_attr = getattr ( fig , '<STR_LIT>' + attr ) ( ) <EOL> default_attr = matplotlib . rcParams [ '<STR_LIT>' + attr ] <EOL> if to_rgba ( fig_attr ) != to_rgba ( default_attr ) : <EOL> kwargs [ attr ] = fig_attr <EOL> current_fig = image_path . format ( fig_count + fig_mngr . num ) <EOL> fig . savefig ( current_fig , ** kwargs ) <EOL> figure_list . append ( current_fig ) <EOL> if gallery_conf . get ( '<STR_LIT>' , False ) : <EOL> from mayavi import mlab <EOL> e = mlab . get_engine ( ) <EOL> last_matplotlib_fig_num = len ( figure_list ) <EOL> total_fig_num = last_matplotlib_fig_num + len ( e . scenes ) <EOL> mayavi_fig_nums = range ( last_matplotlib_fig_num , total_fig_num ) <EOL> for scene , mayavi_fig_num in zip ( e . scenes , mayavi_fig_nums ) : <EOL> current_fig = image_path . format ( mayavi_fig_num ) <EOL> mlab . savefig ( current_fig , figure = scene ) <EOL> scale_image ( current_fig , current_fig , <NUM_LIT> , <NUM_LIT> ) <EOL> figure_list . append ( current_fig ) <EOL> mlab . close ( all = True ) <EOL> return figure_list <EOL> def scale_image ( in_fname , out_fname , max_width , max_height ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> from PIL import Image <EOL> except ImportError : <EOL> import Image <EOL> img = Image . open ( in_fname ) <EOL> width_in , height_in = img . size <EOL> scale_w = max_width / float ( width_in ) <EOL> scale_h = max_height / float ( height_in ) <EOL> if height_in * scale_w <= max_height : <EOL> scale = scale_w <EOL> else : <EOL> scale = scale_h <EOL> if scale >= <NUM_LIT:1.0> and in_fname == out_fname : <EOL> return <EOL> width_sc = int ( round ( scale * width_in ) ) <EOL> height_sc = int ( round ( scale * height_in ) ) <EOL> img . thumbnail ( ( width_sc , height_sc ) , Image . ANTIALIAS ) <EOL> thumb = Image . new ( '<STR_LIT>' , ( max_width , max_height ) , ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) ) <EOL> pos_insert = ( ( max_width - width_sc ) // <NUM_LIT:2> , ( max_height - height_sc ) // <NUM_LIT:2> ) <EOL> thumb . paste ( img , pos_insert ) <EOL> thumb . save ( out_fname ) <EOL> if os . environ . get ( '<STR_LIT>' , False ) : <EOL> try : <EOL> subprocess . call ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , out_fname ] ) <EOL> except Exception : <EOL> warnings . warn ( '<STR_LIT>' ) <EOL> def save_thumbnail ( image_path , base_image_name , gallery_conf ) : <EOL> """<STR_LIT>""" <EOL> first_image_file = image_path . format ( <NUM_LIT:1> ) <EOL> thumb_dir = os . path . join ( os . path . dirname ( first_image_file ) , '<STR_LIT>' ) <EOL> if not os . path . exists ( thumb_dir ) : <EOL> os . makedirs ( thumb_dir ) <EOL> thumb_file = os . path . join ( thumb_dir , <EOL> '<STR_LIT>' % base_image_name ) <EOL> if os . path . exists ( first_image_file ) : <EOL> scale_image ( first_image_file , thumb_file , <NUM_LIT> , <NUM_LIT> ) <EOL> elif not os . path . exists ( thumb_file ) : <EOL> default_thumb_file = os . path . join ( glr_path_static ( ) , '<STR_LIT>' ) <EOL> default_thumb_file = gallery_conf . get ( "<STR_LIT>" , <EOL> default_thumb_file ) <EOL> scale_image ( default_thumb_file , thumb_file , <NUM_LIT:200> , <NUM_LIT> ) <EOL> def generate_dir_rst ( src_dir , target_dir , gallery_conf , seen_backrefs ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( os . path . join ( src_dir , '<STR_LIT>' ) ) : <EOL> print ( <NUM_LIT> * '<STR_LIT:_>' ) <EOL> print ( '<STR_LIT>' % <EOL> src_dir ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( <NUM_LIT> * '<STR_LIT:_>' ) <EOL> return "<STR_LIT>" <EOL> fhindex = open ( os . path . join ( src_dir , '<STR_LIT>' ) ) . read ( ) <EOL> if not os . path . exists ( target_dir ) : <EOL> os . makedirs ( target_dir ) <EOL> sorted_listdir = [ fname for fname in sorted ( os . listdir ( src_dir ) ) <EOL> if fname . endswith ( '<STR_LIT>' ) ] <EOL> entries_text = [ ] <EOL> for fname in sorted_listdir : <EOL> amount_of_code = generate_file_rst ( fname , target_dir , src_dir , <EOL> gallery_conf ) <EOL> new_fname = os . path . join ( src_dir , fname ) <EOL> intro = extract_intro ( new_fname ) <EOL> write_backreferences ( seen_backrefs , gallery_conf , <EOL> target_dir , fname , intro ) <EOL> this_entry = _thumbnail_div ( target_dir , fname , intro ) + """<STR_LIT>""" % ( target_dir , fname [ : - <NUM_LIT:3> ] ) <EOL> entries_text . append ( ( amount_of_code , this_entry ) ) <EOL> entries_text . sort ( ) <EOL> for _ , entry_text in entries_text : <EOL> fhindex += entry_text <EOL> fhindex += """<STR_LIT>""" <EOL> return fhindex <EOL> def execute_script ( code_block , example_globals , image_path , fig_count , <EOL> src_file , gallery_conf ) : <EOL> """<STR_LIT>""" <EOL> time_elapsed = <NUM_LIT:0> <EOL> stdout = '<STR_LIT>' <EOL> print ( '<STR_LIT>' % src_file ) <EOL> plt . close ( '<STR_LIT:all>' ) <EOL> cwd = os . getcwd ( ) <EOL> orig_stdout = sys . stdout <EOL> try : <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> t_start = time ( ) <EOL> exec ( code_block , example_globals ) <EOL> time_elapsed = time ( ) - t_start <EOL> sys . stdout = orig_stdout <EOL> my_stdout = my_buffer . getvalue ( ) . strip ( ) . expandtabs ( ) <EOL> if my_stdout : <EOL> stdout = CODE_OUTPUT . format ( indent ( my_stdout , '<STR_LIT:U+0020>' * <NUM_LIT:4> ) ) <EOL> os . chdir ( cwd ) <EOL> figure_list = save_figures ( image_path , fig_count , gallery_conf ) <EOL> image_list = "<STR_LIT>" <EOL> if len ( figure_list ) == <NUM_LIT:1> : <EOL> figure_name = figure_list [ <NUM_LIT:0> ] <EOL> image_list = SINGLE_IMAGE % figure_name . lstrip ( '<STR_LIT:/>' ) <EOL> elif len ( figure_list ) > <NUM_LIT:1> : <EOL> image_list = HLIST_HEADER <EOL> for figure_name in figure_list : <EOL> image_list += HLIST_IMAGE_TEMPLATE % figure_name . lstrip ( '<STR_LIT:/>' ) <EOL> except Exception : <EOL> formatted_exception = traceback . format_exc ( ) <EOL> print ( <NUM_LIT> * '<STR_LIT:_>' ) <EOL> print ( '<STR_LIT>' % src_file ) <EOL> print ( formatted_exception ) <EOL> print ( <NUM_LIT> * '<STR_LIT:_>' ) <EOL> figure_list = [ ] <EOL> image_list = codestr2rst ( formatted_exception , lang = '<STR_LIT>' ) <EOL> broken_img = os . path . join ( glr_path_static ( ) , '<STR_LIT>' ) <EOL> shutil . copyfile ( broken_img , os . path . join ( cwd , image_path . format ( <NUM_LIT:1> ) ) ) <EOL> fig_count += <NUM_LIT:1> <EOL> if gallery_conf [ '<STR_LIT>' ] : <EOL> raise <EOL> finally : <EOL> os . chdir ( cwd ) <EOL> sys . stdout = orig_stdout <EOL> print ( "<STR_LIT>" % time_elapsed ) <EOL> code_output = "<STR_LIT>" . format ( image_list , stdout ) <EOL> return code_output , time_elapsed , fig_count + len ( figure_list ) <EOL> def generate_file_rst ( fname , target_dir , src_dir , gallery_conf ) : <EOL> """<STR_LIT>""" <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> image_dir = os . path . join ( target_dir , '<STR_LIT>' ) <EOL> if not os . path . exists ( image_dir ) : <EOL> os . makedirs ( image_dir ) <EOL> base_image_name = os . path . splitext ( fname ) [ <NUM_LIT:0> ] <EOL> image_fname = '<STR_LIT>' + base_image_name + '<STR_LIT>' <EOL> image_path = os . path . join ( image_dir , image_fname ) <EOL> script_blocks = split_code_and_text_blocks ( example_file ) <EOL> amount_of_code = sum ( [ len ( bcontent ) <EOL> for blabel , bcontent in script_blocks <EOL> if blabel == '<STR_LIT:code>' ] ) <EOL> if _plots_are_current ( example_file , image_path ) : <EOL> return amount_of_code <EOL> time_elapsed = <NUM_LIT:0> <EOL> ref_fname = example_file . replace ( os . path . sep , '<STR_LIT:_>' ) <EOL> example_rst = """<STR_LIT>""" . format ( ref_fname ) <EOL> example_nb = Notebook ( fname , target_dir ) <EOL> filename_pattern = gallery_conf . get ( '<STR_LIT>' ) <EOL> if re . search ( filename_pattern , src_file ) and gallery_conf [ '<STR_LIT>' ] : <EOL> example_globals = { '<STR_LIT>' : '<STR_LIT>' } <EOL> fig_count = <NUM_LIT:0> <EOL> is_example_notebook_like = len ( script_blocks ) > <NUM_LIT:2> <EOL> for blabel , bcontent in script_blocks : <EOL> if blabel == '<STR_LIT:code>' : <EOL> code_output , rtime , fig_count = execute_script ( bcontent , <EOL> example_globals , <EOL> image_path , <EOL> fig_count , <EOL> src_file , <EOL> gallery_conf ) <EOL> time_elapsed += rtime <EOL> example_nb . add_code_cell ( bcontent ) <EOL> if is_example_notebook_like : <EOL> example_rst += codestr2rst ( bcontent ) + '<STR_LIT:\n>' <EOL> example_rst += code_output <EOL> else : <EOL> example_rst += code_output <EOL> example_rst += codestr2rst ( bcontent ) + '<STR_LIT:\n>' <EOL> else : <EOL> example_rst += text2string ( bcontent ) + '<STR_LIT:\n>' <EOL> example_nb . add_markdown_cell ( text2string ( bcontent ) ) <EOL> else : <EOL> for blabel , bcontent in script_blocks : <EOL> if blabel == '<STR_LIT:code>' : <EOL> example_rst += codestr2rst ( bcontent ) + '<STR_LIT:\n>' <EOL> example_nb . add_code_cell ( bcontent ) <EOL> else : <EOL> example_rst += bcontent + '<STR_LIT:\n>' <EOL> example_nb . add_markdown_cell ( text2string ( bcontent ) ) <EOL> save_thumbnail ( image_path , base_image_name , gallery_conf ) <EOL> time_m , time_s = divmod ( time_elapsed , <NUM_LIT> ) <EOL> example_nb . save_file ( ) <EOL> with open ( os . path . join ( target_dir , base_image_name + '<STR_LIT>' ) , '<STR_LIT:w>' ) as f : <EOL> example_rst += CODE_DOWNLOAD . format ( time_m , time_s , fname , <EOL> example_nb . file_name ) <EOL> f . write ( example_rst ) <EOL> return amount_of_code </s>
