<s> """<STR_LIT>""" <EOL> from __future__ import division , print_function <EOL> from time import time <EOL> import ast <EOL> import os <EOL> import re <EOL> import shutil <EOL> import traceback <EOL> import glob <EOL> import sys <EOL> import gzip <EOL> import posixpath <EOL> import subprocess <EOL> import warnings <EOL> from sklearn . externals import six <EOL> try : <EOL> from StringIO import StringIO <EOL> import cPickle as pickle <EOL> import urllib2 as urllib <EOL> from urllib2 import HTTPError , URLError <EOL> except ImportError : <EOL> from io import StringIO <EOL> import pickle <EOL> import urllib . request <EOL> import urllib . error <EOL> import urllib . parse <EOL> from urllib . error import HTTPError , URLError <EOL> try : <EOL> execfile <EOL> except NameError : <EOL> def execfile ( filename , global_vars = None , local_vars = None ) : <EOL> with open ( filename , encoding = '<STR_LIT:utf-8>' ) as f : <EOL> code = compile ( f . read ( ) , filename , '<STR_LIT>' ) <EOL> exec ( code , global_vars , local_vars ) <EOL> try : <EOL> basestring <EOL> except NameError : <EOL> basestring = str <EOL> import token <EOL> import tokenize <EOL> import numpy as np <EOL> try : <EOL> import matplotlib <EOL> matplotlib . use ( '<STR_LIT>' ) <EOL> except ImportError : <EOL> pass <EOL> from sklearn . externals import joblib <EOL> class Tee ( object ) : <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> def _get_data ( url ) : <EOL> """<STR_LIT>""" <EOL> if url . startswith ( '<STR_LIT>' ) : <EOL> try : <EOL> resp = urllib . urlopen ( url ) <EOL> encoding = resp . headers . dict . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> except AttributeError : <EOL> resp = urllib . request . urlopen ( url ) <EOL> encoding = resp . headers . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> data = resp . read ( ) <EOL> if encoding == '<STR_LIT>' : <EOL> pass <EOL> elif encoding == '<STR_LIT>' : <EOL> data = StringIO ( data ) <EOL> data = gzip . GzipFile ( fileobj = data ) . read ( ) <EOL> else : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> else : <EOL> with open ( url , '<STR_LIT:r>' ) as fid : <EOL> data = fid . read ( ) <EOL> fid . close ( ) <EOL> return data <EOL> mem = joblib . Memory ( cachedir = '<STR_LIT>' ) <EOL> get_data = mem . cache ( _get_data ) <EOL> def parse_sphinx_searchindex ( searchindex ) : <EOL> """<STR_LIT>""" <EOL> def _select_block ( str_in , start_tag , end_tag ) : <EOL> """<STR_LIT>""" <EOL> start_pos = str_in . find ( start_tag ) <EOL> if start_pos < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> depth = <NUM_LIT:0> <EOL> for pos in range ( start_pos , len ( str_in ) ) : <EOL> if str_in [ pos ] == start_tag : <EOL> depth += <NUM_LIT:1> <EOL> elif str_in [ pos ] == end_tag : <EOL> depth -= <NUM_LIT:1> <EOL> if depth == <NUM_LIT:0> : <EOL> break <EOL> sel = str_in [ start_pos + <NUM_LIT:1> : pos ] <EOL> return sel <EOL> def _parse_dict_recursive ( dict_str ) : <EOL> """<STR_LIT>""" <EOL> dict_out = dict ( ) <EOL> pos_last = <NUM_LIT:0> <EOL> pos = dict_str . find ( '<STR_LIT::>' ) <EOL> while pos >= <NUM_LIT:0> : <EOL> key = dict_str [ pos_last : pos ] <EOL> if dict_str [ pos + <NUM_LIT:1> ] == '<STR_LIT:[>' : <EOL> pos_tmp = dict_str . find ( '<STR_LIT:]>' , pos + <NUM_LIT:1> ) <EOL> if pos_tmp < <NUM_LIT:0> : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> value = dict_str [ pos + <NUM_LIT:2> : pos_tmp ] . split ( '<STR_LIT:U+002C>' ) <EOL> for i in range ( len ( value ) ) : <EOL> try : <EOL> value [ i ] = int ( value [ i ] ) <EOL> except ValueError : <EOL> pass <EOL> elif dict_str [ pos + <NUM_LIT:1> ] == '<STR_LIT:{>' : <EOL> subdict_str = _select_block ( dict_str [ pos : ] , '<STR_LIT:{>' , '<STR_LIT:}>' ) <EOL> value = _parse_dict_recursive ( subdict_str ) <EOL> pos_tmp = pos + len ( subdict_str ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> key = key . strip ( '<STR_LIT:">' ) <EOL> if len ( key ) > <NUM_LIT:0> : <EOL> dict_out [ key ] = value <EOL> pos_last = dict_str . find ( '<STR_LIT:U+002C>' , pos_tmp ) <EOL> if pos_last < <NUM_LIT:0> : <EOL> break <EOL> pos_last += <NUM_LIT:1> <EOL> pos = dict_str . find ( '<STR_LIT::>' , pos_last ) <EOL> return dict_out <EOL> if hasattr ( searchindex , '<STR_LIT>' ) : <EOL> searchindex = searchindex . decode ( '<STR_LIT>' ) <EOL> query = '<STR_LIT>' <EOL> pos = searchindex . find ( query ) <EOL> if pos < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> sel = _select_block ( searchindex [ pos : ] , '<STR_LIT:{>' , '<STR_LIT:}>' ) <EOL> objects = _parse_dict_recursive ( sel ) <EOL> query = '<STR_LIT>' <EOL> pos = searchindex . find ( query ) <EOL> if pos < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> filenames = searchindex [ pos + len ( query ) + <NUM_LIT:1> : ] <EOL> filenames = filenames [ : filenames . find ( '<STR_LIT:]>' ) ] <EOL> filenames = [ f . strip ( '<STR_LIT:">' ) for f in filenames . split ( '<STR_LIT:U+002C>' ) ] <EOL> return filenames , objects <EOL> class SphinxDocLinkResolver ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , doc_url , searchindex = '<STR_LIT>' , <EOL> extra_modules_test = None , relative = False ) : <EOL> self . doc_url = doc_url <EOL> self . relative = relative <EOL> self . _link_cache = { } <EOL> self . extra_modules_test = extra_modules_test <EOL> self . _page_cache = { } <EOL> if doc_url . startswith ( '<STR_LIT>' ) : <EOL> if relative : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> searchindex_url = doc_url + '<STR_LIT:/>' + searchindex <EOL> else : <EOL> searchindex_url = os . path . join ( doc_url , searchindex ) <EOL> if os . name . lower ( ) == '<STR_LIT>' and not doc_url . startswith ( '<STR_LIT>' ) : <EOL> if not relative : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . _is_windows = True <EOL> else : <EOL> self . _is_windows = False <EOL> sindex = get_data ( searchindex_url ) <EOL> filenames , objects = parse_sphinx_searchindex ( sindex ) <EOL> self . _searchindex = dict ( filenames = filenames , objects = objects ) <EOL> def _get_link ( self , cobj ) : <EOL> """<STR_LIT>""" <EOL> fname_idx = None <EOL> full_name = cobj [ '<STR_LIT>' ] + '<STR_LIT:.>' + cobj [ '<STR_LIT:name>' ] <EOL> if full_name in self . _searchindex [ '<STR_LIT>' ] : <EOL> value = self . _searchindex [ '<STR_LIT>' ] [ full_name ] <EOL> if isinstance ( value , dict ) : <EOL> value = value [ next ( iter ( value . keys ( ) ) ) ] <EOL> fname_idx = value [ <NUM_LIT:0> ] <EOL> elif cobj [ '<STR_LIT>' ] in self . _searchindex [ '<STR_LIT>' ] : <EOL> value = self . _searchindex [ '<STR_LIT>' ] [ cobj [ '<STR_LIT>' ] ] <EOL> if cobj [ '<STR_LIT:name>' ] in value . keys ( ) : <EOL> fname_idx = value [ cobj [ '<STR_LIT:name>' ] ] [ <NUM_LIT:0> ] <EOL> if fname_idx is not None : <EOL> fname = self . _searchindex [ '<STR_LIT>' ] [ fname_idx ] + '<STR_LIT>' <EOL> if self . _is_windows : <EOL> fname = fname . replace ( '<STR_LIT:/>' , '<STR_LIT:\\>' ) <EOL> link = os . path . join ( self . doc_url , fname ) <EOL> else : <EOL> link = posixpath . join ( self . doc_url , fname ) <EOL> if hasattr ( link , '<STR_LIT>' ) : <EOL> link = link . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) <EOL> if link in self . _page_cache : <EOL> html = self . _page_cache [ link ] <EOL> else : <EOL> html = get_data ( link ) <EOL> self . _page_cache [ link ] = html <EOL> comb_names = [ cobj [ '<STR_LIT>' ] + '<STR_LIT:.>' + cobj [ '<STR_LIT:name>' ] ] <EOL> if self . extra_modules_test is not None : <EOL> for mod in self . extra_modules_test : <EOL> comb_names . append ( mod + '<STR_LIT:.>' + cobj [ '<STR_LIT:name>' ] ) <EOL> url = False <EOL> if hasattr ( html , '<STR_LIT>' ) : <EOL> html = html . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) <EOL> for comb_name in comb_names : <EOL> if hasattr ( comb_name , '<STR_LIT>' ) : <EOL> comb_name = comb_name . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) <EOL> if comb_name in html : <EOL> url = link + u'<STR_LIT:#>' + comb_name <EOL> link = url <EOL> else : <EOL> link = False <EOL> return link <EOL> def resolve ( self , cobj , this_url ) : <EOL> """<STR_LIT>""" <EOL> full_name = cobj [ '<STR_LIT>' ] + '<STR_LIT:.>' + cobj [ '<STR_LIT:name>' ] <EOL> link = self . _link_cache . get ( full_name , None ) <EOL> if link is None : <EOL> link = self . _get_link ( cobj ) <EOL> self . _link_cache [ full_name ] = link <EOL> if link is False or link is None : <EOL> return None <EOL> if self . relative : <EOL> link = os . path . relpath ( link , start = this_url ) <EOL> if self . _is_windows : <EOL> link = link . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) <EOL> link = link [ <NUM_LIT:3> : ] <EOL> return link <EOL> rst_template = """<STR_LIT>""" <EOL> plot_rst_template = """<STR_LIT>""" <EOL> HLIST_HEADER = """<STR_LIT>""" <EOL> HLIST_IMAGE_TEMPLATE = """<STR_LIT>""" <EOL> SINGLE_IMAGE = """<STR_LIT>""" <EOL> carousel_thumbs = { '<STR_LIT>' : ( <NUM_LIT:1> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:3> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:1> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:1> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:1> , <NUM_LIT> ) } <EOL> def extract_docstring ( filename , ignore_heading = False ) : <EOL> """<STR_LIT>""" <EOL> if six . PY2 : <EOL> lines = open ( filename ) . readlines ( ) <EOL> else : <EOL> lines = open ( filename , encoding = '<STR_LIT:utf-8>' ) . readlines ( ) <EOL> start_row = <NUM_LIT:0> <EOL> if lines [ <NUM_LIT:0> ] . startswith ( '<STR_LIT>' ) : <EOL> lines . pop ( <NUM_LIT:0> ) <EOL> start_row = <NUM_LIT:1> <EOL> docstring = '<STR_LIT>' <EOL> first_par = '<STR_LIT>' <EOL> line_iterator = iter ( lines ) <EOL> tokens = tokenize . generate_tokens ( lambda : next ( line_iterator ) ) <EOL> for tok_type , tok_content , _ , ( erow , _ ) , _ in tokens : <EOL> tok_type = token . tok_name [ tok_type ] <EOL> if tok_type in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> continue <EOL> elif tok_type == '<STR_LIT>' : <EOL> docstring = eval ( tok_content ) <EOL> paragraphs = '<STR_LIT:\n>' . join ( <EOL> line . rstrip ( ) for line <EOL> in docstring . split ( '<STR_LIT:\n>' ) ) . split ( '<STR_LIT>' ) <EOL> if paragraphs : <EOL> if ignore_heading : <EOL> if len ( paragraphs ) > <NUM_LIT:1> : <EOL> first_par = re . sub ( '<STR_LIT:\n>' , '<STR_LIT:U+0020>' , paragraphs [ <NUM_LIT:1> ] ) <EOL> first_par = ( ( first_par [ : <NUM_LIT> ] + '<STR_LIT>' ) <EOL> if len ( first_par ) > <NUM_LIT> else first_par ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( filename ) ) <EOL> else : <EOL> first_par = paragraphs [ <NUM_LIT:0> ] <EOL> break <EOL> return docstring , first_par , erow + <NUM_LIT:1> + start_row <EOL> def generate_example_rst ( app ) : <EOL> """<STR_LIT>""" <EOL> root_dir = os . path . join ( app . builder . srcdir , '<STR_LIT>' ) <EOL> example_dir = os . path . abspath ( os . path . join ( app . builder . srcdir , '<STR_LIT:..>' , <EOL> '<STR_LIT>' ) ) <EOL> generated_dir = os . path . abspath ( os . path . join ( app . builder . srcdir , <EOL> '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> try : <EOL> plot_gallery = eval ( app . builder . config . plot_gallery ) <EOL> except TypeError : <EOL> plot_gallery = bool ( app . builder . config . plot_gallery ) <EOL> if not os . path . exists ( example_dir ) : <EOL> os . makedirs ( example_dir ) <EOL> if not os . path . exists ( root_dir ) : <EOL> os . makedirs ( root_dir ) <EOL> if not os . path . exists ( generated_dir ) : <EOL> os . makedirs ( generated_dir ) <EOL> fhindex = open ( os . path . join ( root_dir , '<STR_LIT>' ) , '<STR_LIT:w>' ) <EOL> fhindex . write ( """<STR_LIT>""" ) <EOL> seen_backrefs = set ( ) <EOL> generate_dir_rst ( '<STR_LIT:.>' , fhindex , example_dir , root_dir , plot_gallery , seen_backrefs ) <EOL> for directory in sorted ( os . listdir ( example_dir ) ) : <EOL> if os . path . isdir ( os . path . join ( example_dir , directory ) ) : <EOL> generate_dir_rst ( directory , fhindex , example_dir , root_dir , plot_gallery , seen_backrefs ) <EOL> fhindex . flush ( ) <EOL> def extract_line_count ( filename , target_dir ) : <EOL> example_file = os . path . join ( target_dir , filename ) <EOL> if six . PY2 : <EOL> lines = open ( example_file ) . readlines ( ) <EOL> else : <EOL> lines = open ( example_file , encoding = '<STR_LIT:utf-8>' ) . readlines ( ) <EOL> start_row = <NUM_LIT:0> <EOL> if lines and lines [ <NUM_LIT:0> ] . startswith ( '<STR_LIT>' ) : <EOL> lines . pop ( <NUM_LIT:0> ) <EOL> start_row = <NUM_LIT:1> <EOL> line_iterator = iter ( lines ) <EOL> tokens = tokenize . generate_tokens ( lambda : next ( line_iterator ) ) <EOL> check_docstring = True <EOL> erow_docstring = <NUM_LIT:0> <EOL> for tok_type , _ , _ , ( erow , _ ) , _ in tokens : <EOL> tok_type = token . tok_name [ tok_type ] <EOL> if tok_type in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> continue <EOL> elif ( tok_type == '<STR_LIT>' ) and check_docstring : <EOL> erow_docstring = erow <EOL> check_docstring = False <EOL> return erow_docstring + <NUM_LIT:1> + start_row , erow + <NUM_LIT:1> + start_row <EOL> def line_count_sort ( file_list , target_dir ) : <EOL> new_list = [ x for x in file_list if x . endswith ( '<STR_LIT>' ) ] <EOL> unsorted = np . zeros ( shape = ( len ( new_list ) , <NUM_LIT:2> ) ) <EOL> unsorted = unsorted . astype ( np . object ) <EOL> for count , exmpl in enumerate ( new_list ) : <EOL> docstr_lines , total_lines = extract_line_count ( exmpl , target_dir ) <EOL> unsorted [ count ] [ <NUM_LIT:1> ] = total_lines - docstr_lines <EOL> unsorted [ count ] [ <NUM_LIT:0> ] = exmpl <EOL> index = np . lexsort ( ( unsorted [ : , <NUM_LIT:0> ] . astype ( np . str ) , <EOL> unsorted [ : , <NUM_LIT:1> ] . astype ( np . float ) ) ) <EOL> if not len ( unsorted ) : <EOL> return [ ] <EOL> return np . array ( unsorted [ index ] [ : , <NUM_LIT:0> ] ) . tolist ( ) <EOL> def _thumbnail_div ( subdir , full_dir , fname , snippet , is_backref = False ) : <EOL> """<STR_LIT>""" <EOL> thumb = os . path . join ( full_dir , '<STR_LIT>' , '<STR_LIT>' , fname [ : - <NUM_LIT:3> ] + '<STR_LIT>' ) <EOL> link_name = os . path . join ( full_dir , fname ) . replace ( os . path . sep , '<STR_LIT:_>' ) <EOL> ref_name = os . path . join ( subdir , fname ) . replace ( os . path . sep , '<STR_LIT:_>' ) <EOL> if ref_name . startswith ( '<STR_LIT>' ) : <EOL> ref_name = ref_name [ <NUM_LIT:2> : ] <EOL> out = [ ] <EOL> out . append ( """<STR_LIT>""" . format ( snippet ) ) <EOL> out . append ( '<STR_LIT>' ) <EOL> out . append ( '<STR_LIT>' % thumb ) <EOL> if link_name . startswith ( '<STR_LIT>' ) : <EOL> link_name = link_name [ <NUM_LIT:2> : ] <EOL> if full_dir != '<STR_LIT:.>' : <EOL> out . append ( '<STR_LIT>' % ( full_dir , fname [ : - <NUM_LIT:3> ] ) ) <EOL> else : <EOL> out . append ( '<STR_LIT>' % link_name [ : - <NUM_LIT:3> ] ) <EOL> out . append ( """<STR_LIT>""" % ( ref_name ) ) <EOL> if is_backref : <EOL> out . append ( '<STR_LIT>' % ref_name ) <EOL> return '<STR_LIT>' . join ( out ) <EOL> def generate_dir_rst ( directory , fhindex , example_dir , root_dir , plot_gallery , seen_backrefs ) : <EOL> """<STR_LIT>""" <EOL> if not directory == '<STR_LIT:.>' : <EOL> target_dir = os . path . join ( root_dir , directory ) <EOL> src_dir = os . path . join ( example_dir , directory ) <EOL> else : <EOL> target_dir = root_dir <EOL> src_dir = example_dir <EOL> if not os . path . exists ( os . path . join ( src_dir , '<STR_LIT>' ) ) : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> src_dir ) <EOL> fhindex . write ( """<STR_LIT>""" % 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 = line_count_sort ( os . listdir ( src_dir ) , <EOL> src_dir ) <EOL> if not os . path . exists ( os . path . join ( directory , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> os . makedirs ( os . path . join ( directory , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> for fname in sorted_listdir : <EOL> if fname . endswith ( '<STR_LIT>' ) : <EOL> backrefs = generate_file_rst ( fname , target_dir , src_dir , root_dir , plot_gallery ) <EOL> new_fname = os . path . join ( src_dir , fname ) <EOL> _ , snippet , _ = extract_docstring ( new_fname , True ) <EOL> fhindex . write ( _thumbnail_div ( directory , directory , fname , snippet ) ) <EOL> fhindex . write ( """<STR_LIT>""" % ( directory , fname [ : - <NUM_LIT:3> ] ) ) <EOL> for backref in backrefs : <EOL> include_path = os . path . join ( root_dir , '<STR_LIT>' % backref ) <EOL> seen = backref in seen_backrefs <EOL> with open ( include_path , '<STR_LIT:a>' if seen else '<STR_LIT:w>' ) as ex_file : <EOL> if not seen : <EOL> print ( file = ex_file ) <EOL> print ( '<STR_LIT>' % backref , file = ex_file ) <EOL> print ( '<STR_LIT>' % ( '<STR_LIT:->' * len ( backref ) ) , <EOL> file = ex_file ) <EOL> print ( file = ex_file ) <EOL> rel_dir = os . path . join ( '<STR_LIT>' , directory ) <EOL> ex_file . write ( _thumbnail_div ( directory , rel_dir , fname , snippet , is_backref = True ) ) <EOL> seen_backrefs . add ( backref ) <EOL> fhindex . write ( """<STR_LIT>""" ) <EOL> DOCMODULES = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def make_thumbnail ( in_fname , out_fname , width , 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 = width / float ( width_in ) <EOL> scale_h = height / float ( height_in ) <EOL> if height_in * scale_w <= height : <EOL> scale = scale_w <EOL> else : <EOL> scale = scale_h <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>' , ( width , height ) , ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) ) <EOL> pos_insert = ( ( width - width_sc ) // <NUM_LIT:2> , ( 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 get_short_module_name ( module_name , obj_name ) : <EOL> """<STR_LIT>""" <EOL> parts = module_name . split ( '<STR_LIT:.>' ) <EOL> short_name = module_name <EOL> for i in range ( len ( parts ) - <NUM_LIT:1> , <NUM_LIT:0> , - <NUM_LIT:1> ) : <EOL> short_name = '<STR_LIT:.>' . join ( parts [ : i ] ) <EOL> try : <EOL> exec ( '<STR_LIT>' % ( short_name , obj_name ) ) <EOL> except ImportError : <EOL> short_name = '<STR_LIT:.>' . join ( parts [ : ( i + <NUM_LIT:1> ) ] ) <EOL> break <EOL> return short_name <EOL> class NameFinder ( ast . NodeVisitor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( NameFinder , self ) . __init__ ( ) <EOL> self . imported_names = { } <EOL> self . accessed_names = set ( ) <EOL> def visit_Import ( self , node , prefix = '<STR_LIT>' ) : <EOL> for alias in node . names : <EOL> local_name = alias . asname or alias . name <EOL> self . imported_names [ local_name ] = prefix + alias . name <EOL> def visit_ImportFrom ( self , node ) : <EOL> self . visit_Import ( node , node . module + '<STR_LIT:.>' ) <EOL> def visit_Name ( self , node ) : <EOL> self . accessed_names . add ( node . id ) <EOL> def visit_Attribute ( self , node ) : <EOL> attrs = [ ] <EOL> while isinstance ( node , ast . Attribute ) : <EOL> attrs . append ( node . attr ) <EOL> node = node . value <EOL> if isinstance ( node , ast . Name ) : <EOL> attrs . append ( node . id ) <EOL> self . accessed_names . add ( '<STR_LIT:.>' . join ( reversed ( attrs ) ) ) <EOL> else : <EOL> self . visit ( node ) <EOL> def get_mapping ( self ) : <EOL> for name in self . accessed_names : <EOL> local_name = name . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> remainder = name [ len ( local_name ) : ] <EOL> if local_name in self . imported_names : <EOL> full_name = self . imported_names [ local_name ] + remainder <EOL> yield name , full_name <EOL> def identify_names ( code ) : <EOL> """<STR_LIT>""" <EOL> finder = NameFinder ( ) <EOL> finder . visit ( ast . parse ( code ) ) <EOL> example_code_obj = { } <EOL> for name , full_name in finder . get_mapping ( ) : <EOL> module , attribute = full_name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> module_short = get_short_module_name ( module , attribute ) <EOL> cobj = { '<STR_LIT:name>' : attribute , '<STR_LIT>' : module , <EOL> '<STR_LIT>' : module_short } <EOL> example_code_obj [ name ] = cobj <EOL> return example_code_obj <EOL> def generate_file_rst ( fname , target_dir , src_dir , root_dir , plot_gallery ) : <EOL> """<STR_LIT>""" <EOL> base_image_name = os . path . splitext ( fname ) [ <NUM_LIT:0> ] <EOL> image_fname = '<STR_LIT>' % base_image_name <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> last_dir = '<STR_LIT>' <EOL> else : <EOL> last_dir += '<STR_LIT:_>' <EOL> 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> os . makedirs ( image_dir ) <EOL> if not os . path . exists ( thumb_dir ) : <EOL> os . makedirs ( thumb_dir ) <EOL> image_path = os . path . join ( image_dir , 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 , base_image_name + '<STR_LIT>' ) <EOL> time_elapsed = <NUM_LIT:0> <EOL> if plot_gallery and fname . startswith ( '<STR_LIT>' ) : <EOL> first_image_file = image_path % <NUM_LIT:1> <EOL> if os . path . exists ( stdout_path ) : <EOL> stdout = open ( stdout_path ) . read ( ) <EOL> else : <EOL> stdout = '<STR_LIT>' <EOL> if os . path . exists ( time_path ) : <EOL> time_elapsed = float ( open ( time_path ) . read ( ) ) <EOL> if not os . path . exists ( first_image_file ) or os . stat ( first_image_file ) . st_mtime <= os . stat ( src_file ) . st_mtime : <EOL> 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> 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> my_stdout = my_stdout . replace ( <EOL> my_globals [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> my_stdout = my_stdout . strip ( ) . expandtabs ( ) <EOL> if my_stdout : <EOL> stdout = '<STR_LIT>' % ( <EOL> '<STR_LIT>' . join ( my_stdout . split ( '<STR_LIT:\n>' ) ) ) <EOL> 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> 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> fig . savefig ( image_path % fig_mngr . num , ** kwargs ) <EOL> figure_list . append ( image_fname % fig_mngr . num ) <EOL> except : <EOL> print ( <NUM_LIT> * '<STR_LIT:_>' ) <EOL> print ( '<STR_LIT>' % fname ) <EOL> traceback . print_exc ( ) <EOL> print ( <NUM_LIT> * '<STR_LIT:_>' ) <EOL> finally : <EOL> os . chdir ( cwd ) <EOL> sys . stdout = orig_stdout <EOL> print ( "<STR_LIT>" % time_elapsed ) <EOL> else : <EOL> figure_list = [ f [ len ( image_dir ) : ] <EOL> for f in glob . glob ( image_path . replace ( "<STR_LIT>" , <EOL> '<STR_LIT>' ) ) ] <EOL> figure_list . sort ( ) <EOL> this_template = plot_rst_template <EOL> car_thumb_path = os . path . join ( os . path . split ( root_dir ) [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> if not os . path . exists ( car_thumb_path ) : <EOL> os . makedirs ( car_thumb_path ) <EOL> if os . path . exists ( first_image_file ) : <EOL> carousel_tfile = os . path . join ( car_thumb_path , base_image_name + '<STR_LIT>' ) <EOL> first_img = image_fname % <NUM_LIT:1> <EOL> if first_img in carousel_thumbs : <EOL> make_thumbnail ( ( image_path % carousel_thumbs [ first_img ] [ <NUM_LIT:0> ] ) , <EOL> carousel_tfile , carousel_thumbs [ first_img ] [ <NUM_LIT:1> ] , <NUM_LIT> ) <EOL> make_thumbnail ( first_image_file , thumb_file , <NUM_LIT> , <NUM_LIT> ) <EOL> if not os . path . exists ( thumb_file ) : <EOL> make_thumbnail ( '<STR_LIT>' , thumb_file , <NUM_LIT:200> , <NUM_LIT> ) <EOL> docstring , short_desc , end_row = extract_docstring ( example_file ) <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> else : <EOL> image_list = HLIST_HEADER <EOL> for figure_name in figure_list : <EOL> image_list += HLIST_IMAGE_TEMPLATE % figure_name . lstrip ( '<STR_LIT:/>' ) <EOL> time_m , time_s = divmod ( time_elapsed , <NUM_LIT> ) <EOL> f = open ( os . path . join ( target_dir , base_image_name + '<STR_LIT>' ) , '<STR_LIT:w>' ) <EOL> f . write ( this_template % locals ( ) ) <EOL> f . flush ( ) <EOL> if six . PY2 : <EOL> example_code_obj = identify_names ( open ( example_file ) . read ( ) ) <EOL> else : <EOL> example_code_obj = identify_names ( open ( example_file , encoding = '<STR_LIT:utf-8>' ) . read ( ) ) <EOL> if example_code_obj : <EOL> codeobj_fname = example_file [ : - <NUM_LIT:3> ] + '<STR_LIT>' <EOL> with open ( codeobj_fname , '<STR_LIT:wb>' ) as fid : <EOL> pickle . dump ( example_code_obj , fid , pickle . HIGHEST_PROTOCOL ) <EOL> backrefs = set ( '<STR_LIT>' . format ( ** entry ) <EOL> for entry in example_code_obj . values ( ) <EOL> if entry [ '<STR_LIT>' ] . startswith ( '<STR_LIT>' ) ) <EOL> return backrefs <EOL> def embed_code_links ( app , exception ) : <EOL> """<STR_LIT>""" <EOL> if exception is not None : <EOL> return <EOL> print ( '<STR_LIT>' ) <EOL> if app . builder . name == '<STR_LIT>' : <EOL> return <EOL> doc_resolvers = { } <EOL> doc_resolvers [ '<STR_LIT>' ] = SphinxDocLinkResolver ( app . builder . outdir , <EOL> relative = True ) <EOL> resolver_urls = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> for this_module , url in resolver_urls . items ( ) : <EOL> try : <EOL> doc_resolvers [ this_module ] = SphinxDocLinkResolver ( url ) <EOL> except HTTPError as e : <EOL> print ( "<STR_LIT>" ) <EOL> print ( e . code ) <EOL> except URLError as e : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( this_module ) ) <EOL> print ( e . args ) <EOL> example_dir = os . path . join ( app . builder . srcdir , '<STR_LIT>' ) <EOL> html_example_dir = os . path . abspath ( os . path . join ( app . builder . outdir , <EOL> '<STR_LIT>' ) ) <EOL> link_pattern = '<STR_LIT>' <EOL> orig_pattern = '<STR_LIT>' <EOL> period = '<STR_LIT>' <EOL> for dirpath , _ , filenames in os . walk ( html_example_dir ) : <EOL> for fname in filenames : <EOL> print ( '<STR_LIT>' % fname ) <EOL> full_fname = os . path . join ( html_example_dir , dirpath , fname ) <EOL> subpath = dirpath [ len ( html_example_dir ) + <NUM_LIT:1> : ] <EOL> pickle_fname = os . path . join ( example_dir , subpath , <EOL> fname [ : - <NUM_LIT:5> ] + '<STR_LIT>' ) <EOL> if os . path . exists ( pickle_fname ) : <EOL> with open ( pickle_fname , '<STR_LIT:rb>' ) as fid : <EOL> example_code_obj = pickle . load ( fid ) <EOL> fid . close ( ) <EOL> str_repl = { } <EOL> for name , cobj in example_code_obj . items ( ) : <EOL> this_module = cobj [ '<STR_LIT>' ] . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> if this_module not in doc_resolvers : <EOL> continue <EOL> try : <EOL> link = doc_resolvers [ this_module ] . resolve ( cobj , <EOL> full_fname ) <EOL> except ( HTTPError , URLError ) as e : <EOL> print ( "<STR_LIT>" ) <EOL> print ( repr ( e ) ) <EOL> continue <EOL> if link is not None : <EOL> parts = name . split ( '<STR_LIT:.>' ) <EOL> name_html = period . join ( orig_pattern % part <EOL> for part in parts ) <EOL> str_repl [ name_html ] = link_pattern % ( link , name_html ) <EOL> names = sorted ( str_repl , key = len , reverse = True ) <EOL> expr = re . compile ( r'<STR_LIT>' + <EOL> '<STR_LIT:|>' . join ( re . escape ( name ) <EOL> for name in names ) ) <EOL> def substitute_link ( match ) : <EOL> return str_repl [ match . group ( ) ] <EOL> if len ( str_repl ) > <NUM_LIT:0> : <EOL> with open ( full_fname , '<STR_LIT:rb>' ) as fid : <EOL> lines_in = fid . readlines ( ) <EOL> with open ( full_fname , '<STR_LIT:wb>' ) as fid : <EOL> for line in lines_in : <EOL> line = line . decode ( '<STR_LIT:utf-8>' ) <EOL> line = expr . sub ( substitute_link , line ) <EOL> fid . write ( line . encode ( '<STR_LIT:utf-8>' ) ) <EOL> print ( '<STR_LIT>' ) <EOL> def setup ( app ) : <EOL> app . connect ( '<STR_LIT>' , generate_example_rst ) <EOL> app . add_config_value ( '<STR_LIT>' , True , '<STR_LIT:html>' ) <EOL> app . connect ( '<STR_LIT>' , embed_code_links ) <EOL> build_image_dir = '<STR_LIT>' <EOL> if os . path . exists ( build_image_dir ) : <EOL> filelist = os . listdir ( build_image_dir ) <EOL> for filename in filelist : <EOL> if filename . endswith ( '<STR_LIT>' ) : <EOL> os . remove ( os . path . join ( build_image_dir , filename ) ) <EOL> def setup_module ( ) : <EOL> pass </s>
