<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import hashlib <EOL> from os import makedirs <EOL> from os . path import basename , dirname , exists , isdir , join , relpath <EOL> import re <EOL> from shutil import copy <EOL> import sys <EOL> from tempfile import mkdtemp <EOL> import webbrowser <EOL> from docutils import nodes <EOL> from docutils . parsers . rst . directives import choice , flag , unchanged <EOL> from docutils . statemachine import ViewList <EOL> import jinja2 <EOL> from sphinx . locale import _ <EOL> from sphinx . util . compat import Directive <EOL> from . utils import out_of_date <EOL> from . . import io <EOL> from . . document import Document <EOL> from . . embed import autoload_static <EOL> from . . resources import Resources <EOL> from . . settings import settings <EOL> from . . util . string import decode_utf8 <EOL> SOURCE_TEMPLATE = jinja2 . Template ( u"""<STR_LIT>""" ) <EOL> SCRIPT_TEMPLATE = jinja2 . Template ( u"""<STR_LIT>""" ) <EOL> class bokeh_plot ( nodes . General , nodes . Element ) : <EOL> pass <EOL> def _source_position ( argument ) : <EOL> return choice ( argument , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:none>' ) ) <EOL> class BokehPlotDirective ( Directive ) : <EOL> has_content = True <EOL> optional_arguments = <NUM_LIT:2> <EOL> option_spec = { <EOL> '<STR_LIT>' : _source_position , <EOL> '<STR_LIT>' : flag , <EOL> '<STR_LIT>' : unchanged , <EOL> } <EOL> def run ( self ) : <EOL> if self . arguments and self . content : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> env = self . state . document . settings . env <EOL> app = env . app <EOL> if not hasattr ( env , '<STR_LIT>' ) : <EOL> env . bokeh_plot_tmpdir = mkdtemp ( ) <EOL> app . verbose ( "<STR_LIT>" % env . bokeh_plot_tmpdir ) <EOL> else : <EOL> tmpdir = env . bokeh_plot_tmpdir <EOL> if not exists ( tmpdir ) or not isdir ( tmpdir ) : <EOL> app . verbose ( "<STR_LIT>" % env . bokeh_plot_tmpdir ) <EOL> env . bokeh_plot_tmpdir = mkdtemp ( ) <EOL> else : <EOL> app . verbose ( "<STR_LIT>" % env . bokeh_plot_tmpdir ) <EOL> rst_source = self . state_machine . document [ '<STR_LIT:source>' ] <EOL> rst_dir = dirname ( rst_source ) <EOL> rst_filename = basename ( rst_source ) <EOL> target_id = "<STR_LIT>" % ( rst_filename , env . new_serialno ( '<STR_LIT>' ) ) <EOL> target_node = nodes . target ( '<STR_LIT>' , '<STR_LIT>' , ids = [ target_id ] ) <EOL> result = [ target_node ] <EOL> try : <EOL> source = self . _get_source ( ) <EOL> except Exception : <EOL> app . warn ( "<STR_LIT>" % ( basename ( rst_source ) , self . lineno ) ) , <EOL> node = nodes . error ( None , <EOL> nodes . paragraph ( text = "<STR_LIT>" % ( basename ( rst_source ) , self . lineno ) ) , <EOL> nodes . paragraph ( text = str ( sys . exc_info ( ) [ <NUM_LIT:1> ] ) ) ) <EOL> return [ node ] <EOL> source_position = self . options . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if source_position == '<STR_LIT>' : <EOL> result += self . _get_source_nodes ( source ) <EOL> node = bokeh_plot ( ) <EOL> node [ '<STR_LIT>' ] = target_id <EOL> node [ '<STR_LIT:source>' ] = source <EOL> node [ '<STR_LIT>' ] = relpath ( rst_dir , env . srcdir ) <EOL> node [ '<STR_LIT>' ] = rst_source <EOL> node [ '<STR_LIT>' ] = self . lineno <EOL> if '<STR_LIT>' in self . options : <EOL> node [ '<STR_LIT>' ] = self . options [ '<STR_LIT>' ] <EOL> if self . arguments : <EOL> node [ '<STR_LIT:path>' ] = self . arguments [ <NUM_LIT:0> ] <EOL> env . note_dependency ( node [ '<STR_LIT:path>' ] ) <EOL> if len ( self . arguments ) == <NUM_LIT:2> : <EOL> node [ '<STR_LIT>' ] = self . arguments [ <NUM_LIT:1> ] <EOL> result += [ node ] <EOL> if source_position == '<STR_LIT>' : <EOL> result += self . _get_source_nodes ( source ) <EOL> return result <EOL> def _get_source ( self ) : <EOL> if self . arguments : <EOL> source = open ( self . arguments [ <NUM_LIT:0> ] , "<STR_LIT:r>" ) . read ( ) <EOL> source = decode_utf8 ( source ) <EOL> else : <EOL> source = u"<STR_LIT>" <EOL> for line in self . content : <EOL> source += "<STR_LIT>" % line <EOL> return source <EOL> def _get_source_nodes ( self , source ) : <EOL> linenos = '<STR_LIT>' in self . options <EOL> emphasize_lines = self . options . get ( '<STR_LIT>' , False ) <EOL> if emphasize_lines : linenos = True <EOL> result = ViewList ( ) <EOL> text = SOURCE_TEMPLATE . render ( source = source , linenos = linenos , emphasize_lines = emphasize_lines ) <EOL> for line in text . split ( "<STR_LIT:\n>" ) : <EOL> result . append ( line , "<STR_LIT>" ) <EOL> node = nodes . paragraph ( ) <EOL> node . document = self . state . document <EOL> self . state . nested_parse ( result , <NUM_LIT:0> , node ) <EOL> return node . children <EOL> def _open ( * args , ** kwargs ) : <EOL> pass <EOL> def _save ( * args , ** kwargs ) : <EOL> pass <EOL> def _show ( obj = None ) : <EOL> if obj : <EOL> io . _obj = obj <EOL> _save . __doc__ = io . save . __doc__ <EOL> _save . __module__ = io . save . __module__ <EOL> _show . __doc__ = io . show . __doc__ <EOL> _show . __module__ = io . show . __module__ <EOL> webbrowser . open = _open <EOL> io . save = _save <EOL> io . show = _show <EOL> def _render_plot ( source , symbol ) : <EOL> io . _state . _document = Document ( ) <EOL> namespace = { } <EOL> pat = re . compile ( r"<STR_LIT>" , re . M ) <EOL> source = pat . sub ( "<STR_LIT>" , source ) <EOL> code = compile ( source , "<STR_LIT>" , mode = "<STR_LIT>" ) <EOL> eval ( code , namespace ) <EOL> if symbol is not None : <EOL> obj = namespace [ symbol ] <EOL> else : <EOL> obj = io . _obj <EOL> return obj <EOL> def html_visit_bokeh_plot ( self , node ) : <EOL> env = self . builder . env <EOL> dest_dir = join ( self . builder . outdir , node [ "<STR_LIT>" ] ) <EOL> if settings . docs_cdn ( ) == "<STR_LIT>" : <EOL> resources = Resources ( mode = "<STR_LIT>" , root_url = "<STR_LIT>" ) <EOL> else : <EOL> resources = Resources ( mode = "<STR_LIT>" ) <EOL> try : <EOL> if "<STR_LIT:path>" in node : <EOL> path = node [ '<STR_LIT:path>' ] <EOL> filename = "<STR_LIT>" % hashlib . md5 ( path . encode ( '<STR_LIT:utf-8>' ) ) . hexdigest ( ) <EOL> dest_path = join ( dest_dir , filename ) <EOL> tmpdir = join ( env . bokeh_plot_tmpdir , node [ "<STR_LIT>" ] ) <EOL> if not exists ( tmpdir ) : makedirs ( tmpdir ) <EOL> cached_path = join ( tmpdir , filename ) <EOL> if out_of_date ( path , cached_path ) or not exists ( cached_path + "<STR_LIT>" ) : <EOL> self . builder . app . verbose ( "<STR_LIT>" % path ) <EOL> plot = _render_plot ( node [ '<STR_LIT:source>' ] , node . get ( '<STR_LIT>' ) ) <EOL> js , script = autoload_static ( plot , resources , filename ) <EOL> with open ( cached_path , "<STR_LIT:w>" ) as f : <EOL> f . write ( js ) <EOL> with open ( cached_path + "<STR_LIT>" , "<STR_LIT:w>" ) as f : <EOL> f . write ( script ) <EOL> else : <EOL> self . builder . app . verbose ( "<STR_LIT>" % path ) <EOL> script = open ( cached_path + "<STR_LIT>" , "<STR_LIT:r>" ) . read ( ) <EOL> if not exists ( dest_dir ) : makedirs ( dest_dir ) <EOL> copy ( cached_path , dest_path ) <EOL> else : <EOL> filename = node [ '<STR_LIT>' ] + "<STR_LIT>" <EOL> if not exists ( dest_dir ) : makedirs ( dest_dir ) <EOL> dest_path = join ( dest_dir , filename ) <EOL> plot = _render_plot ( node [ '<STR_LIT:source>' ] , None ) <EOL> js , script = autoload_static ( plot , resources , filename ) <EOL> self . builder . app . verbose ( "<STR_LIT>" % dest_path ) <EOL> with open ( dest_path , "<STR_LIT:w>" ) as f : <EOL> f . write ( js ) <EOL> html = SCRIPT_TEMPLATE . render ( script = script ) <EOL> self . body . append ( html ) <EOL> except Exception : <EOL> err_node = nodes . error ( None , <EOL> nodes . paragraph ( text = "<STR_LIT>" % ( node [ '<STR_LIT>' ] , node [ '<STR_LIT>' ] ) ) , <EOL> nodes . paragraph ( text = str ( sys . exc_info ( ) [ <NUM_LIT:1> ] ) ) ) <EOL> node . children . append ( err_node ) <EOL> raise nodes . SkipDeparture <EOL> else : <EOL> raise nodes . SkipNode <EOL> def latex_visit_bokeh_plot ( self , node ) : <EOL> if '<STR_LIT>' in node . attributes : <EOL> self . body . append ( _ ( '<STR_LIT>' ) % node [ '<STR_LIT>' ] ) <EOL> else : <EOL> self . body . append ( _ ( '<STR_LIT>' ) ) <EOL> raise nodes . SkipNode <EOL> def texinfo_visit_bokeh_plot ( self , node ) : <EOL> if '<STR_LIT>' in node . attributes : <EOL> self . body . append ( _ ( '<STR_LIT>' ) % node [ '<STR_LIT>' ] ) <EOL> else : <EOL> self . body . append ( _ ( '<STR_LIT>' ) ) <EOL> raise nodes . SkipNode <EOL> def text_visit_bokeh_plot ( self , node ) : <EOL> if '<STR_LIT>' in node . attributes : <EOL> self . add_text ( _ ( '<STR_LIT>' ) % node [ '<STR_LIT>' ] ) <EOL> else : <EOL> self . add_text ( _ ( '<STR_LIT>' ) ) <EOL> raise nodes . SkipNode <EOL> def man_visit_bokeh_plot ( self , node ) : <EOL> if '<STR_LIT>' in node . attributes : <EOL> self . body . append ( _ ( '<STR_LIT>' ) % node [ '<STR_LIT>' ] ) <EOL> else : <EOL> self . body . append ( _ ( '<STR_LIT>' ) ) <EOL> raise nodes . SkipNode <EOL> def setup ( app ) : <EOL> app . add_node ( bokeh_plot , <EOL> html = ( html_visit_bokeh_plot , None ) , <EOL> latex = ( latex_visit_bokeh_plot , None ) , <EOL> texinfo = ( texinfo_visit_bokeh_plot , None ) , <EOL> text = ( text_visit_bokeh_plot , None ) , <EOL> man = ( man_visit_bokeh_plot , None ) ) <EOL> app . add_directive ( '<STR_LIT>' , BokehPlotDirective ) </s>
