σ
mάJ]c           @` s±   d  Z  d d l m Z m Z m Z m Z d d l Z e j e  Z	 d d l
 Z
 d d l Z d d l m Z m Z d d l m Z d d l m Z d Z d
 e f d     YZ d S(   uο   Provide a Bokeh Application Handler to build up documents by compiling
and executing Python source code.

This Handler is used by the Bokeh server command line tool to build
applications that run off scripts and notebooks.

.. code-block:: python

    def make_doc(doc):

        # do work to modify the document, add plots, widgets, etc.

        return doc

    app = Application(FunctionHandler(make_doc))

    server = Server({'/bkapp': app}, io_loop=IOLoop.current())

    server.start()

i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsNi   (   t
   set_curdoct   curdoci   (   t
   CodeRunner(   t   Handleru   CodeHandlert   CodeHandlerc           B` s   e  Z d  Z d d d d d g Z d   Z e d    Z e d    Z e d	    Z e d
    Z	 d   Z
 d   Z d   Z d   Z d   Z RS(   u0    Run source code which modifies a Document

    u   output_notebooku   output_fileu   showu   saveu   reset_outputc         O` sΑ   t  t |   j | |   d | k r4 t d   n  | d } d | k rY t d   n  | d } | j d g   } t | | |  |  _ i  |  _ x' t j D] } |  j	 |  |  j | <q Wd S(   u&  

        Args:
            source (str) : python source code

            filename (str) : a filename to use in any debugging or error output

            argv (list[str], optional) : a list of string arguments to make
                available as ``sys.argv`` when the code executes

        u   sourceu   Must pass source to CodeHandleru   filenameu#   Must pass a filename to CodeHandleru   argvN(
   t   superR   t   __init__t
   ValueErrort   getR   t   _runnert   _loggerst   _io_functionst   _make_io_logger(   t   selft   argst   kwargst   sourcet   filenamet   argvt   f(    (    s>   lib/python2.7/site-packages/bokeh/application/handlers/code.pyR
   M   s    

	c         C` s
   |  j  j S(   uE    If the handler fails, may contain a related error message.

        (   R   t   error(   R   (    (    s>   lib/python2.7/site-packages/bokeh/application/handlers/code.pyR   m   s    c         C` s
   |  j  j S(   uJ    If the handler fails, may contain a traceback or other details.

        (   R   t   error_detail(   R   (    (    s>   lib/python2.7/site-packages/bokeh/application/handlers/code.pyR   t   s    c         C` s
   |  j  j S(   u;    ``True`` if the handler failed to modify the doc

        (   R   t   failed(   R   (    (    s>   lib/python2.7/site-packages/bokeh/application/handlers/code.pyR   {   s    c         C` s   |  j  j S(   u    Whether it is still safe for the Bokeh server to fork new workers.

        ``False`` if the code has already been executed.

        (   R   t   ran(   R   (    (    s>   lib/python2.7/site-packages/bokeh/application/handlers/code.pyt   safe_to_fork   s    c         ` s¦    j  j   } | d k r d S| t j | j <  j j |  t   } t	     j
   } z)    f d   }  j  j | |  Wd  j |  t	 |  Xd S(   u
   

        Nc          ` s;   t    }  |    k	 r7 t d  j  j j f   n  d  S(   Nu'   %s at '%s' replaced the output document(   R   t   RuntimeErrort   _originR   t   path(   t   newdoc(   t   docR   (    s>   lib/python2.7/site-packages/bokeh/application/handlers/code.pyt
   post_check¦   s    	(   R   t
   new_modulet   Nonet   syst   modulest   __name__t   _modulest   appendR   R   t   _monkeypatch_iot   runt   _unmonkeypatch_io(   R   R!   t   modulet   old_doct   old_ioR"   (    (   R!   R   s>   lib/python2.7/site-packages/bokeh/application/handlers/code.pyt   modify_document   s    	
c         C` s;   |  j  r d Sd t j j t j j |  j j   d Sd S(   uO    The last path component for the basename of the configured filename.

        u   /i    N(   R   R$   t   osR   t   splitextt   basenameR   (   R   (    (    s>   lib/python2.7/site-packages/bokeh/application/handlers/code.pyt   url_path°   s    	c         ` s      f d   } | S(   Nc          ` s    t  j  j  j j    d  S(   N(   t   logt   infot   _logger_textR   R   (   R   R   (   t   nameR   (    s>   lib/python2.7/site-packages/bokeh/application/handlers/code.pyt   loggerΎ   s    (    (   R   R8   R9   (    (   R8   R   s>   lib/python2.7/site-packages/bokeh/application/handlers/code.pyR   ½   s    c         C` sW   d d  l  j } i  } x; t j D]0 } t | |  | | <t | | |  j |  q W| S(   Ni    (   t   bokeh.iot   ioR   R   t   getattrt   setattrR   (   R   R;   t   oldR   (    (    s>   lib/python2.7/site-packages/bokeh/application/handlers/code.pyR*   Ε   s    c         C` s8   d d  l  j } x" | D] } t | | | |  q Wd  S(   Ni    (   R:   R;   R=   (   R   R>   R;   R   (    (    s>   lib/python2.7/site-packages/bokeh/application/handlers/code.pyR,   Ξ   s    (   R'   t
   __module__t   __doc__R   R
   t   propertyR   R   R   R   R0   R4   R   R*   R,   (    (    (    s>   lib/python2.7/site-packages/bokeh/application/handlers/code.pyR   D   s   	 	#				(   u   CodeHandler(   R@   t
   __future__R    R   R   R   t   loggingt	   getLoggerR'   R5   R1   R%   t   io.docR   R   t   code_runnerR   t   handlerR   t   __all__R   (    (    (    s>   lib/python2.7/site-packages/bokeh/application/handlers/code.pyt   <module>   s   "