σ
mάJ]c           @` s½  d  Z  d d l m Z m Z m Z m Z d d l m Z e j e	  Z
 d d l m Z d d l m Z m Z m Z d d l Z d d l m Z d d l m Z d d	 l m Z d d
 l m Z m Z d d l m Z d d l m Z m Z m Z d d l  m! Z! d d l" m# Z# d d l$ m% Z% d d l& m' Z' d d l( m) Z) d d l* m+ Z+ d d l, m- Z- d Z. d e f d     YZ/ d   Z0 d   Z1 d   Z2 e3 d  Z4 e j5 d e j6  Z7 d S(    uo   Include Bokeh plots in Sphinx HTML documentation.

For other output types, the placeholder text ``[graph]`` will
be generated.

The ``bokeh-plot`` directive can be used by either supplying:

**A path to a source file** as the argument to the directive::

    .. bokeh-plot:: path/to/plot.py

.. note::
    .py scripts are not scanned automatically! In order to include
    certain directories into .py scanning process use following directive
    in sphinx conf.py file: bokeh_plot_pyfile_include_dirs = ["dir1","dir2"]

**Inline code** as the content of the directive::

 .. bokeh-plot::

     from bokeh.plotting import figure, output_file, show

     output_file("example.html")

     x = [1, 2, 3, 4, 5]
     y = [6, 7, 6, 4, 5]

     p = figure(title="example", plot_width=300, plot_height=300)
     p.line(x, y, line_width=2)
     p.circle(x, y, size=10, fill_color="white")

     show(p)

This directive also works in conjunction with Sphinx autodoc, when
used in docstrings.

The ``bokeh-plot`` directive accepts the following options:

source-position (enum('above', 'below', 'none')):
    Where to locate the the block of formatted source
    code (if anywhere).

linenos (bool):
    Whether to display line numbers along with the source.

Examples
--------

The inline example code above produces the following output:

.. bokeh-plot::

    from bokeh.plotting import figure, output_file, show

    output_file("example.html")

    x = [1, 2, 3, 4, 5]
    y = [6, 7, 6, 4, 5]

    p = figure(title="example", plot_width=300, plot_height=300)
    p.line(x, y, line_width=2)
    p.circle(x, y, size=10, fill_color="white")

    show(p)

i    (   t   absolute_importt   divisiont   print_functiont   unicode_literals(   t   logging(   t   getenv(   t   basenamet   dirnamet   joinN(   t   uuid4(   t   nodes(   t	   Directive(   t   choicet   flag(   t   SphinxError(   t   copyfilet	   ensuredirt   status_iterator(   t   set_source_infoi   (   t   Document(   t   autoload_static(   t   Model(   t   decode_utf8i   (   t   get_sphinx_resources(   t   ExampleHandleru   BokehPlotDirectiveu   setupt   BokehPlotDirectivec           B` s7   e  Z e Z d  Z i d   d 6d   d 6Z d   Z RS(   i   c         C` s   t  |  d  S(   Nu   belowu   aboveu   none(   u   belowu   aboveu   none(   R   (   t   x(    (    s9   lib/python2.7/site-packages/bokeh/sphinxext/bokeh_plot.pyt   <lambda>   t    u   source-positionc         C` s   t  |   d  k r t St S(   N(   R   t   Nonet   Truet   False(   R   (    (    s9   lib/python2.7/site-packages/bokeh/sphinxext/bokeh_plot.pyR      R   u   linenosc      	   C` s:  |  j  j j j } |  j r3 |  j r3 t d   n  | j j d d  } |  j r t	 j
 d | j  | j } d j |  j  } ni t	 j
 d | j |  j d  |  j d } | j d  sΣ t | j j |  } n  t t |  j    } d t   j | f } t | | | |  \ } } } } | | | | t | j  f | j | <d	 | j t |  f }	 t j d
 d
 d |	 g }
 |
 g } |  j j d t  } t j | | d d d | d g  } t |  |  |  j j d d  } | d k rϋ| | g 7} n  | t j  d
 | d d g 7} | d k r6| | g 7} n  | S(   Nu7   bokeh-plot:: directive can't have both args and contentu   /u   -u*   [bokeh-plot] handling inline example in %ru   
u0   [bokeh-plot] handling external example in %r: %si    u   bokeh-plot-%s-external-%s.jsu   %s.%su    t   idsu   linenost   languageu   pythont   linenost   classesu   source-positionu   belowu   abovet   formatu   html(!   t   statet   documentt   settingst   envt	   argumentst   contentR   t   docnamet   replacet   logt   debugt   bokeh_plot_auxdirR   t
   startswitht   appt   srcdirR   t   opent   readR	   t   hext   _process_scriptR   t   bokeh_plot_filesR   R
   t   targett   optionst   getR   t   literal_blockR   t   raw(   t   selfR(   R+   t   patht   sourcet   js_namet   scriptt   jst   js_patht	   target_idR8   t   resultR"   t   codet   source_position(    (    s9   lib/python2.7/site-packages/bokeh/sphinxext/bokeh_plot.pyt   run   s:    		!%	$  (   t   __name__t
   __module__R   t   has_contentt   optional_argumentst   option_specRH   (    (    (    s9   lib/python2.7/site-packages/bokeh/sphinxext/bokeh_plot.pyR   |   s   
c         C` sP   t  |  j j d  |  j _ t |  j j  t |  j d  sL i  |  j _ n  d  S(   Nu
   bokeh_plotu   bokeh_plot_files(   R   R(   t
   doctreedirR/   R   t   hasattrR7   (   R1   (    (    s9   lib/python2.7/site-packages/bokeh/sphinxext/bokeh_plot.pyt   builder_initedΈ   s    c         C` sώ   t    } x< |  j j j   D]( \ } } } } } | j | | f  q Wt t |  d d t |  |  j d d   } x | D]w \ }	 } t	 |  j
 j | t |	   }
 t t |
   y t |	 |
  Wq t k
 rυ } t d |	 | f   q Xq Wd  S(   Nu   copying bokeh-plot files... u   brownt   stringify_funcc         S` s   t  |  d  S(   Ni    (   R   (   R   (    (    s9   lib/python2.7/site-packages/bokeh/sphinxext/bokeh_plot.pyR   Κ   R   u%   cannot copy local file %r, reason: %s(   t   setR(   R7   t   valuest   addR   t   sortedt   lent	   verbosityR   t   buildert   outdirR   R   R   R   t   OSErrorR   (   R1   t	   exceptiont   filesRA   RB   RC   R?   t   docpatht
   files_itert   fileR8   t   e(    (    s9   lib/python2.7/site-packages/bokeh/sphinxext/bokeh_plot.pyt   build_finishedΏ   s     	(		c         C` sm   |  j  d g  d  |  j  d t d  |  j d t  |  j  d t d  |  j d t  |  j d t  d S(	   u+    Required Sphinx extension setup function. u   bokeh_plot_pyfile_include_dirsu   htmlu   bokeh_plot_use_relative_pathsu
   bokeh-plotu   bokeh_missing_google_api_key_oku   builder-initedu   build-finishedN(   t   add_config_valueR   t   add_directiveR   R   t   connectRP   Ra   (   R1   (    (    s9   lib/python2.7/site-packages/bokeh/sphinxext/bokeh_plot.pyt   setupΤ   s    c         C` s4  t  j d |   }  t j   d |  k ry t d  } | d  k rd | j j rU d } qd t d   n  |  j	 d |  } n |  } t
 d | d |  } t   } | j |  | j rΕ t | j   n  t   }	 t | j |  }
 t | j d |	 |  \ } } t |
 d   } | j |  Wd  QX| | |
 |  f S(	   Nu    u   GOOGLE_API_KEYu   MISSING_API_KEYu΅   The GOOGLE_API_KEY environment variable is not set. Set GOOGLE_API_KEY to a valid API key, or set bokeh_missing_google_api_key_ok=True in conf.py to build anyway (with broken GMaps)R?   t   filenamei    u   w(   t   CODINGt   subR   t   _clear_extensionsR   R   t   configt   bokeh_missing_google_api_key_okR   R,   R   R   t   modify_documentt   errort   RuntimeErrort   error_detailR   R   R/   R   t   rootsR3   t   write(   R?   Rf   R(   R@   t   use_relative_pathst   GOOGLE_API_KEYt
   run_sourcet   ct   dt	   resourcesRC   RB   RA   t   f(    (    s9   lib/python2.7/site-packages/bokeh/sphinxext/bokeh_plot.pyR6   δ   s*    
				u   ^# -\*- coding: (.*) -\*-$(   u   BokehPlotDirectiveu   setup(8   t   __doc__t
   __future__R    R   R   R   t   sphinx.utilR   t	   getLoggerRI   R-   t   osR   t   os.pathR   R   R   t   ret   uuidR	   t   docutilsR
   t   docutils.parsers.rstR   t   docutils.parsers.rst.directivesR   R   t   sphinx.errorsR   R   R   R   t   sphinx.util.nodesR   R&   R   t   embedR   t   modelR   t   util.stringR   t   utilR   t   example_handlerR   t   __all__R   RP   Ra   Re   R   R6   t   compilet   MRg   (    (    (    s9   lib/python2.7/site-packages/bokeh/sphinxext/bokeh_plot.pyt   <module>H   s6   " <			'