
mJ]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 Z d d l Z d d l m Z d d l m Z m Z m Z d d l m Z d Z d d  Z d Z d d  Z d d  Z e
 j d d d   Z d   Z d S(   uD    Provide utility functions for implementing the ``bokeh`` command.

i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   Application(   t   ScriptHandlert   DirectoryHandlert   NotebookHandler(   t   Plotu    build_single_handler_applicationu!   build_single_handler_applicationsu   dieu   report_server_init_errorsu   set_single_plot_width_heighti   c         C` s$   t  |  d t j t j |  d S(   u	   Print an error message and exit.

    This function will call ``sys.exit`` with the given ``status`` and the
    process will terminate.

    Args:
        message (str) : error message to print

        status (int) : the exit status to pass to ``sys.exit``

    t   fileN(   t   printt   syst   stderrt   exit(   t   messaget   status(    (    s1   lib/python2.7/site-packages/bokeh/command/util.pyt   die4   s    u8  
It looks like you might be running the main.py of a directory app directly.
If this is the case, to enable the features of directory style apps, you must
call "bokeh serve" on the directory instead. For example:

    bokeh serve my_app_dir/

If this is not the case, renaming main.py will supress this warning.
c         C` s%  | p	 g  } t  j j |   }  t  j j |   rH t d |  d |  } n t  j j |   r |  j d  r t d |  d |  } q |  j d  r |  j d  r t j	 t
  n  t d |  d |  } q t d |    n t d |    | j rt d |  | j | j f   n  t |  } | S(	   uR   Return a Bokeh application built using a single handler for a script,
    notebook, or directory.

    In general a Bokeh :class:`~bokeh.application.application.Application` may
    have any number of handlers to initialize :class:`~bokeh.document.Document`
    objects for new client sessions. However, in many cases only a single
    handler is needed. This function examines the ``path`` provided, and
    returns an ``Application`` initialized with one of the following handlers:

    * :class:`~bokeh.application.handlers.script.ScriptHandler` when ``path``
      is to a ``.py`` script.

    * :class:`~bokeh.application.handlers.notebook.NotebookHandler` when
      ``path`` is to an ``.ipynb`` Jupyter notebook.

    * :class:`~bokeh.application.handlers.directory.DirectoryHandler` when
      ``path`` is to a directory containing a ``main.py`` script.

    Args:
        path (str) : path to a file or directory for creating a Bokeh
            application.

        argv (seq[str], optional) : command line arguments to pass to the
            application handler

    Returns:
        :class:`~bokeh.application.application.Application`

    Raises:
        RuntimeError

    Notes:
        If ``path`` ends with a file ``main.py`` then a warning will be printed
        regarding running directory-style apps by passing the directory instead.

    t   filenamet   argvu   .ipynbu   .pyu   main.pyu7   Expected a '.py' script or '.ipynb' notebook, got: '%s'u4   Path for Bokeh server application does not exist: %su   Error loading %s:

%s
%s (   t   ost   patht   abspatht   isdirR   t   isfilet   endswithR   t   warningst   warnt   DIRSTYLE_MAIN_WARNINGR   t
   ValueErrort   failedt   RuntimeErrort   errort   error_detailR   (   R   R   t   handlert   application(    (    s1   lib/python2.7/site-packages/bokeh/command/util.pyt    build_single_handler_applicationM   s"    %	"c         C` s   i  } i  p | } xt |  D]l } t  | | j | g    } | j d j   } | s{ d | k rr t d |   n  d } n  | | | <q W| S(   uz   Return a dictionary mapping routes to Bokeh applications built using
    single handlers, for specified files or directories.

    This function iterates over ``paths`` and ``argvs`` and calls
    :func:`~bokeh.command.util.build_single_handler_application` on each
    to generate the mapping.

    Args:
        path (seq[str]) : paths to files or directories for creating Bokeh
            applications.

        argvs (dict[str, list[str]], optional) : mapping of paths to command
            line arguments to pass to the handler for each path

    Returns:
        dict[str, Application]

    Raises:
        RuntimeError

    i    u   /u%   Don't know the URL path to use for %s(   R#   t   gett   handlerst   url_pathR   (   t   pathst   argvst   applicationsR   R"   t   route(    (    s1   lib/python2.7/site-packages/bokeh/command/util.pyt!   build_single_handler_applications   s    	c         k` s   y	 d VWn t  k
 r } | j t j k r@ t j d |  nH | j t j k re t j d |   n# t j | j } t j d | |  t j d  n Xd S(   u   A context manager to help print more informative error messages when a
    ``Server`` cannot be started due to a network problem.

    Args:
        address (str) : network address that the server will be listening on

        port (int) : network address that the server will be listening on

    Example:

        .. code-block:: python

            with report_server_init_errors(**server_kwargs):
                server = Server(applications, **server_kwargs)

        If there are any errors (e.g. port or address in already in use) then a
        critical error will be logged and the process will terminate with a
        call to ``sys.exit(1)``

    Nu4   Cannot start Bokeh server, port %s is already in useu5   Cannot start Bokeh server, address '%s' not availableu"   Cannot start Bokeh server [%s]: %ri   (	   t   EnvironmentErrort   errnot
   EADDRINUSEt   logt   criticalt   EADDRNOTAVAILt	   errorcodeR   R   (   t   addresst   portt   kwargst   et   codename(    (    s1   lib/python2.7/site-packages/bokeh/command/util.pyt   report_server_init_errors   s    	c         C` s   | d  k	 s | d  k	 r |  j } t |  d k sG t | d t  rW t j d  q | d } | pm | j | _ | p | j | _ n  d  S(   Ni   i    ut   Width/height arguments will be ignored for this muliple layout. (Size valus only apply when exporting single plots.)(	   t   Nonet   rootst   lent
   isinstanceR   R   R   t   plot_heightt
   plot_width(   t   doct   widtht   heightt   layoutt   plot(    (    s1   lib/python2.7/site-packages/bokeh/command/util.pyt   set_single_plot_width_height   s    	&
(   u    build_single_handler_applicationu!   build_single_handler_applicationsu   dieu   report_server_init_errorsu   set_single_plot_width_height(    t   __doc__t
   __future__R    R   R   R   t   loggingt	   getLoggert   __name__R/   t
   contextlibR-   R   R   R   t   bokeh.applicationR   t   bokeh.application.handlersR   R   R   t   bokeh.models.plotsR   t   __all__R   R   R9   R#   R+   t   contextmanagerR8   RD   (    (    (    s1   lib/python2.7/site-packages/bokeh/command/util.pyt   <module>	   s.   "    @'!