B
    ‹æ@\˜   ã               @   sÄ   d Z ddlmZmZmZmZ ddlZe 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	d
„ZdZddd„Zddd„Ze
jddd„ƒZdd„ ZdS )zD Provide utility functions for implementing the ``bokeh`` command.

é    )Úabsolute_importÚdivisionÚprint_functionÚunicode_literalsN)ÚApplication)ÚScriptHandlerÚDirectoryHandlerÚNotebookHandler)ÚPlot)Ú build_single_handler_applicationÚ!build_single_handler_applicationsÚdieÚreport_server_init_errorsÚset_single_plot_width_heighté   c             C   s   t | tjd t |¡ dS )a	   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``

    )ÚfileN)ÚprintÚsysÚstderrÚexit)ÚmessageZstatus© r   ú1lib/python3.7/site-packages/bokeh/command/util.pyr   4   s    r   a8  
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 | ¡} t j | ¡r.t| |d}njt j | ¡rŒ|  d¡rRt| |d}q˜|  d¡r~|  d¡rpt 	t
¡ t| |d}q˜td|  ƒ‚ntd|  ƒ‚|jr´td| |j|jf ƒ‚t|ƒ}|S )aR   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.

    )ÚfilenameÚargvz.ipynbz.pyzmain.pyz7Expected a '.py' script or '.ipynb' notebook, got: '%s'z4Path for Bokeh server application does not exist: %szError loading %s:

%s
%s )ÚosÚpathÚabspathÚisdirr   ÚisfileÚendswithr	   ÚwarningsÚwarnÚDIRSTYLE_MAIN_WARNINGr   Ú
ValueErrorZfailedÚRuntimeErrorÚerrorZerror_detailr   )r   r   ZhandlerÚapplicationr   r   r   r   M   s"    %



r   c             C   sb   i }i p
|}xP| D ]H}t || |g ¡ƒ}|jd  ¡ }|sRd|krNtd| ƒ‚d}|||< qW |S )az   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

    r   ú/z%Don't know the URL path to use for %s)r   ÚgetZhandlersZurl_pathr%   )ÚpathsZargvsZapplicationsr   r'   Zrouter   r   r   r      s    
r   c          
   k   sŠ   y
dV  W nz t k
r„ } z\|jtjkr6t d|¡ n4|jtjkrPt d| ¡ ntj|j }t d||¡ t d¡ W dd}~X Y nX dS )a“   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)``

    Nz4Cannot start Bokeh server, port %s is already in usez5Cannot start Bokeh server, address '%s' not availablez"Cannot start Bokeh server [%s]: %rr   )	ÚEnvironmentErrorÚerrnoZ
EADDRINUSEÚlogZcriticalZEADDRNOTAVAILZ	errorcoder   r   )ZaddressZportÚkwargsÚeZcodenamer   r   r   r   ´   s    
r   c             C   s`   |d k	s|d k	r\| j }t|ƒdks0t|d tƒs<t d¡ n |d }|pL|j|_|pX|j|_d S )Nr   r   ztWidth/height arguments will be ignored for this muliple layout. (Size valus only apply when exporting single plots.))ÚrootsÚlenÚ
isinstancer
   r!   r"   Zplot_heightZ
plot_width)ÚdocÚwidthZheightZlayoutZplotr   r   r   r   Ö   s    r   )r   )N)N)NN)Ú__doc__Z
__future__r   r   r   r   ZloggingZ	getLoggerÚ__name__r-   Ú
contextlibr,   r   r   r!   Zbokeh.applicationr   Zbokeh.application.handlersr   r   r	   Zbokeh.models.plotsr
   Ú__all__r   r#   r   r   Úcontextmanagerr   r   r   r   r   r   Ú<module>	   s&   


@
'!