ó
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
 m Z m Z m Z m Z d d l 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 Z d e f d „  ƒ  YZ d S(   uü    Provide a Bokeh Application Handler to build up documents by running
the code from ``main.py`` or ``main.ipynb`` files in specified directories.

The directory may also optionally contain:

* A ``server_lifecyle.py`` module to provide lifecycle callbacks for the
  application and sessions.

* A ``static`` subdirectory containing app-specific static resources to
  serve.

* A ``theme.yaml`` file containing a Bokeh theme to automatically apply to
  all new documents.

* A ``templates`` subdirectory containing templates for app display

A full directory layout might look like:

.. code-block:: none

    myapp
       |
       +---main.py
       +---server_lifecycle.py
       +---static
       +---theme.yaml
       +---templates
            +---index.html

i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   basenamet   dirnamet   existst   join(   t   Environmentt   FileSystemLoaderi   (   t   Handler(   t   NotebookHandler(   t   ScriptHandler(   t   ServerLifecycleHandleru   DirectoryHandlert   DirectoryHandlerc           B` s‰   e  Z d  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 d „  Z RS(   u?    Load an application directory which modifies a Document.

    c         O` sD  t  t |  ƒ j | | Ž  d | k r4 t d ƒ ‚ n  | d } | j d g  ƒ } t | d ƒ } t | d ƒ } t | ƒ r  t | ƒ r  t j d | ƒ | } n: t | ƒ rµ | } n% t | ƒ rÊ | } n t d | ƒ ‚ | |  _	 | |  _
 | j d ƒ rt n t } | d	 |  j
 d
 | ƒ |  _ t | d ƒ }	 t |	 ƒ rd|	 |  _ t d	 |  j d
 | ƒ |  _ n d |  _ t ƒ  |  _ d |  _ t | d ƒ }
 t |
 ƒ rÂd d l m } | d	 |
 ƒ |  _ n  t | d ƒ } t | ƒ ré| |  _ n  d |  _ t | d d ƒ } t | ƒ r@t d t t | ƒ ƒ ƒ } | j d ƒ |  _ n  d S(   uï   
        Keywords:
            filename (str) : a path to an application directory with either "main.py" or "main.ipynb"

            argv (list[str], optional) : a list of string arguments to make available as sys.argv to main.py
        u   filenameu(   Must pass a filename to DirectoryHandleru   argvu   main.pyu
   main.ipynbu<   Found both 'main.py' and 'main.ipynb' in %s, using 'main.py'u"   No 'main.py' or 'main.ipynb' in %su   .ipynbt   filenamet   argvu   server_lifecycle.pyu
   theme.yamli    (   t   Themeu   staticu	   templatesu
   index.htmlt   loaderN(   t   superR   t   __init__t
   ValueErrort   getR   R   t   logt   warningt   _patht   _maint   endswithR   R   t   _main_handlert
   _lifecycleR   t   _lifecycle_handlert   NoneR
   t   _themet   bokeh.themesR   t   _statict	   _templateR   R	   R   t   get_template(   t   selft   argst   kwargst   src_pathR   t   main_pyt   main_ipyt   maint   handlert	   lifecyclet	   themeyamlR   t	   appstatict   appindext   env(    (    sC   lib/python2.7/site-packages/bokeh/application/handlers/directory.pyR   S   sL    
									c         C` s   |  j  j p |  j j S(   uE    If the handler fails, may contain a related error message.

        (   R   t   errorR   (   R%   (    (    sC   lib/python2.7/site-packages/bokeh/application/handlers/directory.pyR2   Œ   s    c         C` s   |  j  j p |  j j S(   uJ    If the handler fails, may contain a traceback or other details.

        (   R   t   error_detailR   (   R%   (    (    sC   lib/python2.7/site-packages/bokeh/application/handlers/directory.pyR3   “   s    c         C` s   |  j  j p |  j j S(   u;    ``True`` if the handler failed to modify the doc

        (   R   t   failedR   (   R%   (    (    sC   lib/python2.7/site-packages/bokeh/application/handlers/directory.pyR4   š   s    c         C` s
   |  j  j S(   u®    Whether it is still safe for the Bokeh server to fork new workers.

        ``False`` if the configured code (script, notebook, etc.) has already
        been run.

        (   R   t   safe_to_fork(   R%   (    (    sC   lib/python2.7/site-packages/bokeh/application/handlers/directory.pyR5   ¡   s    c         C` s`   |  j  j r d S|  j d k	 r. |  j | _ n  |  j d k	 rL |  j | _ n  |  j j | ƒ d S(   u   Execute the configured ``main.py`` or ``main.ipynb`` to modify the
        document.

        This method will also search the app directory for any theme or
        template files, and automatically configure the document with them
        if they are found.

        N(	   R   R4   R    R   t   themeR#   t   templateR   t   modify_document(   R%   t   doc(    (    sC   lib/python2.7/site-packages/bokeh/application/handlers/directory.pyR8   ­   s    	c         C` s   |  j  j | ƒ S(   u¾    Execute `on_server_unloaded`` from ``server_lifecycle.py`` (if
        it is defined) when the server is first started.

        Args:
            server_context (ServerContext) :

        (   R   t   on_server_loaded(   R%   t   server_context(    (    sC   lib/python2.7/site-packages/bokeh/application/handlers/directory.pyR:   Ã   s    c         C` s   |  j  j | ƒ S(   um   Execute ``on_server_unloaded`` from ``server_lifecycle.py`` (if
        it is defined) when the server cleanly exits. (Before stopping the
        server's ``IOLoop``.)

        Args:
            server_context (ServerContext) :

        .. warning::
            In practice this code may not run, since servers are often killed
            by a signal.


        (   R   t   on_server_unloaded(   R%   R;   (    (    sC   lib/python2.7/site-packages/bokeh/application/handlers/directory.pyR<   Í   s    c         C` s   |  j  j | ƒ S(   u¾    Execute ``on_session_created`` from ``server_lifecycle.py`` (if
        it is defined) when a new session is created.

        Args:
            session_context (SessionContext) :

        (   R   t   on_session_created(   R%   t   session_context(    (    sC   lib/python2.7/site-packages/bokeh/application/handlers/directory.pyR=   Ý   s    c         C` s   |  j  j | ƒ S(   u¾    Execute ``on_session_destroyed`` from ``server_lifecycle.py`` (if
        it is defined) when a session is destroyed.

        Args:
            session_context (SessionContext) :

        (   R   t   on_session_destroyed(   R%   R>   (    (    sC   lib/python2.7/site-packages/bokeh/application/handlers/directory.pyR?   ç   s    c         C` s"   |  j  r d Sd t |  j ƒ Sd S(   ud    The last path component for the basename of the path to the
        configured directory.

        u   /N(   R4   R   R   R   (   R%   (    (    sC   lib/python2.7/site-packages/bokeh/application/handlers/directory.pyt   url_pathñ   s    	(   t   __name__t
   __module__t   __doc__R   t   propertyR2   R3   R4   R5   R8   R:   R<   R=   R?   R@   (    (    (    sC   lib/python2.7/site-packages/bokeh/application/handlers/directory.pyR   N   s   	9		
		
	
(   u   DirectoryHandler(   RC   t
   __future__R    R   R   R   t   loggingt	   getLoggerRA   R   t   os.pathR   R   R   R   t   jinja2R   R	   R,   R
   t   notebookR   t   scriptR   t   server_lifecycleR   t   __all__R   (    (    (    sC   lib/python2.7/site-packages/bokeh/application/handlers/directory.pyt   <module>$   s   ""