ó
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 Z d d l m Z d d l m Z d
 Z d e f d	 „  ƒ  YZ d S(   u^    Provide a utility class ``CodeRunner`` for use by handlers that execute
Python source code.

i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t
   ModuleTypei   (   t   make_idu
   CodeRunnert
   CodeRunnerc           B` s}   e  Z d  Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 d „  Z
 d „  Z d	 „  Z RS(
   u*    Compile and run Python source code.

    c         C` sõ   d |  _ d |  _ |  j ƒ  d d l } d |  _ y7 | j | | ƒ } t | d | d d d t ƒ|  _ Wnb t	 k
 rÌ } d d l
 } d |  _ d t j j | j ƒ | j | j f |  _ | j ƒ  |  _ n X| |  _ | |  _ | |  _ t |  _ d S(   u  

        Args:
            source (str) : python source code

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

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

        i    Nt   filenamet   modeu   exect   dont_inheritu%   Invalid syntax in "%s" on line %d:
%s(   t   Nonet   _permanent_errort   _permanent_error_detailt   reset_run_errorst   astt   _codet   parset   compilet   Truet   SyntaxErrort	   tracebackt   ost   patht   basenameR   t   linenot   textt
   format_exct   _patht   _sourcet   _argvt   Falset   ran(   t   selft   sourceR   t   argvR   t   nodest   eR   (    (    sE   lib/python2.7/site-packages/bokeh/application/handlers/code_runner.pyt   __init__8   s"    		
	%	+			c         C` s   |  j  d k r |  j S|  j  S(   uH    If code execution fails, may contain a related error message.

        N(   R   R
   t   _error(   R    (    (    sE   lib/python2.7/site-packages/bokeh/application/handlers/code_runner.pyt   error[   s    c         C` s   |  j  d k r |  j S|  j  S(   uM    If code execution fails, may contain a traceback or other details.

        N(   R   R
   t   _error_detail(   R    (    (    sE   lib/python2.7/site-packages/bokeh/application/handlers/code_runner.pyt   error_detailb   s    c         C` s   |  j  p |  j d k S(   u,    ``True`` if code execution failed

        N(   t   _failedR   R
   (   R    (    (    sE   lib/python2.7/site-packages/bokeh/application/handlers/code_runner.pyt   failedi   s    c         C` s   |  j  S(   u=    The path that new modules will be configured with.

        (   R   (   R    (    (    sE   lib/python2.7/site-packages/bokeh/application/handlers/code_runner.pyR   p   s    c         C` s   |  j  S(   u[    The configured source code that will be executed when ``run`` is
        called.

        (   R   (   R    (    (    sE   lib/python2.7/site-packages/bokeh/application/handlers/code_runner.pyR!   w   s    c         C` sh   |  j  ƒ  |  j d k r d Sd t ƒ  j d d ƒ } t t | ƒ ƒ } t j j	 |  j
 ƒ | j d <| S(   uN    Make a fresh module to run in.

        Returns:
            Module

        u
   bk_script_u   -u    u   __file__N(   R   R   R
   R   t   replaceR   t   strR   R   t   abspathR   t   __dict__(   R    t   module_namet   module(    (    sE   lib/python2.7/site-packages/bokeh/application/handlers/code_runner.pyt
   new_module   s    
c         C` s   t  |  _ d |  _ d |  _ d S(   ug    Clears any transient error conditions from a previous run.

        Returns
            None

        N(   R   R*   R
   R&   R(   (   R    (    (    sE   lib/python2.7/site-packages/bokeh/application/handlers/code_runner.pyR   “   s    		c         B` sK  zy‡ e  j ƒ  } e e j ƒ } e e j ƒ } e j j d e  j j |  j ƒ ƒ e  j j	 |  j ƒ g |  j
 e _ |  j | j U| ƒ  Wn e k
 r} e |  _ e j ƒ  |  _ e j ƒ  \ } } }	 e j |	 ƒ d \ }
 } } } d e | ƒ e  j j	 |
 ƒ | | | f |  _ n XWd e  j | ƒ | e _ | e _ e |  _ Xd S(   uL   Execute the configured source code in a module and run any post
        checks.

        Args:
            module (Module) : a module to execute the configured code in.

            post_check(callable) : a function that can raise an exception
                if expected post-conditions are not met after code execution.

        i    iÿÿÿÿu    %s
File "%s", line %d, in %s:
%sN(   R   t   getcwdt   listt   sysR   R"   t   insertt   dirnameR   R   R   R   R/   t	   ExceptionR   R*   R   R   R(   t   exc_infot
   extract_tbR-   R&   t   chdirR   (   R    R1   t
   post_checkt   _cwdt	   _sys_patht	   _sys_argvR$   t	   _exc_typet
   _exc_valuet   exc_tracebackR   t   line_numbert   funct   txt(    (    sE   lib/python2.7/site-packages/bokeh/application/handlers/code_runner.pyt   runž   s&     ""	6		(   t   __name__t
   __module__t   __doc__R%   t   propertyR'   R)   R+   R   R!   R2   R   RF   (    (    (    sE   lib/python2.7/site-packages/bokeh/application/handlers/code_runner.pyR   3   s   	#
		(   u
   CodeRunner(   RI   t
   __future__R    R   R   R   t   loggingt	   getLoggerRG   t   logR   R5   R   t   typesR   t   util.serializationR   t   __all__t   objectR   (    (    (    sE   lib/python2.7/site-packages/bokeh/application/handlers/code_runner.pyt   <module>
   s   "