U
    /e0                     @  s   d Z ddl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mZmZ ddlmZ ddlmZmZmZmZ dZd!d	d
ddddZdZd"d	dddddZd#ddddddZejd$ddd	dddd ZdS )%zD Provide utility functions for implementing the ``bokeh`` command.

    )annotationsN)DictIteratorList)Application)DirectoryHandlerHandlerNotebookHandlerScriptHandler) build_single_handler_application!build_single_handler_applicationsdiereport_server_init_errors   strintNone)messagestatusreturnc                 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)r   r    r   6/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/command/util.pyr   6   s    r   a9  
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 suppress this warning.
zList[str] | Noner   )pathargvr   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}qtd|  ntd|  |jrtd| |j|jf t|}|S )a;   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 |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.

    )filenamer   z.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 )osr   abspathisdirr   isfileendswithr	   warningswarnDIRSTYLE_MAIN_WARNINGr
   
ValueErrorfailedRuntimeErrorerrorZerror_detailr   )r   r   handlerapplicationr   r   r   r   O   s"    %



r   z	List[str]zDict[str, List[str]] | NonezDict[str, Application])pathsargvsr   c                 C  s^   i }|p
i }| D ]H}t |||g }|jd  }|sPd|krLtd| d}|||< q|S )a{   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:
        paths (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   gethandlersZurl_pathr*   )r.   r/   Zapplicationsr   r-   Zrouter   r   r   r      s    
r   z
str | Nonez
int | NonezIterator[None])addressportkwargsr   c              
   k  s   z
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 5 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   )	OSErrorerrnoZ
EADDRINUSElogcriticalZEADDRNOTAVAIL	errorcoder   r   )r3   r4   r5   ecodenamer   r   r   r      s    
r   )r   )N)N)NN)__doc__
__future__r   logging	getLogger__name__r8   
contextlibr7   r    r   r%   typingr   r   r   Zbokeh.applicationr   Zbokeh.application.handlersr   r   r	   r
   __all__r   r'   r   r   contextmanagerr   r   r   r   r   <module>   s$   

A'