a
    mzfK                     @   sT  d 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	 ddl
mZ ddl
mZ G dd	 d	ZG d
d deZG dd deZG dd deZG dd dZG dd dZdd Zdd Zdejddddedddd ejd!dd"ed#d$d%d&ed'd(d%d)ed*d(d%d+ed,d(d%d-ed.d(d%d/ed0d(d%d1ed2d(d%d3ed4d(d5Zd6d7 ZdS )8a:  Command line tool for starting a Cheroot WSGI/HTTP server instance.

Basic usage:

.. code-block:: shell-session

    $ # Start a server on 127.0.0.1:8000 with the default settings
    $ # for the WSGI app myapp/wsgi.py:application()
    $ cheroot myapp.wsgi

    $ # Start a server on 0.0.0.0:9000 with 8 threads
    $ # for the WSGI app myapp/wsgi.py:main_app()
    $ cheroot myapp.wsgi:main_app --bind 0.0.0.0:9000 --threads 8

    $ # Start a server for the cheroot.server.Gateway subclass
    $ # myapp/gateway.py:HTTPGateway
    $ cheroot myapp.gateway:HTTPGateway

    $ # Start a server on the UNIX socket /var/spool/myapp.sock
    $ cheroot myapp.wsgi --bind /var/spool/myapp.sock

    $ # Start a server on the abstract UNIX socket CherootServer
    $ cheroot myapp.wsgi --bind @CherootServer

.. spelling::

   cli
    N)import_module)suppress   )server)wsgic                   @   s   e Zd ZdZdS )BindLocationz=A class for storing the bind location for a Cheroot instance.N)__name__
__module____qualname____doc__ r   r   9/var/www/media/lib/python3.9/site-packages/cheroot/cli.pyr   )   s   r   c                   @   s   e Zd ZdZdd ZdS )	TCPSocketz
TCPSocket.c                 C   s   ||f| _ dS )zInitialize.

        Args:
            address (str): Host name or IP address
            port (int): TCP port number

        N	bind_addr)selfaddressportr   r   r   __init__0   s    zTCPSocket.__init__Nr   r	   r
   r   r   r   r   r   r   r   -   s   r   c                   @   s   e Zd ZdZdd ZdS )
UnixSocketzUnixSocket.c                 C   s
   || _ dS )Initialize.Nr   )r   pathr   r   r   r   >   s    zUnixSocket.__init__Nr   r   r   r   r   r   ;   s   r   c                   @   s   e Zd ZdZdd ZdS )AbstractSocketzAbstractSocket.c                 C   s   dj |d| _dS )r   z {sock_path})Z	sock_pathN)formatr   )r   Zabstract_socketr   r   r   r   F   s    zAbstractSocket.__init__Nr   r   r   r   r   r   C   s   r   c                   @   s4   e Zd ZdZedd Zdd Zdd Zdd	 Zd
S )ApplicationzApplication.c                 C   st   | d\}}}tt||pd}tt2 t|tjrNt|W  d   S W d   n1 sb0    Y  | |S )z@Read WSGI app/Gateway path string and import application module.:ZapplicationN)		partitiongetattrr   r   	TypeError
issubclassr   ZGateway	GatewayYo)cls	full_pathZmod_path_Zapp_pathZappr   r   r   resolveN   s    
4zApplication.resolvec                 C   s   t |std|| _dS )r   zHApplication must be a callable object or cheroot.server.Gateway subclassN)callabler   wsgi_app)r   r'   r   r   r   r   Z   s
    zApplication.__init__c                 C   s(   dd t | D }|t |  |S )z%Return keyword args for Server class.c                 S   s(   i | ] \}}| d s|dur||qS )r$   N)
startswith).0argvaluer   r   r   
<dictcomp>e   s   z+Application.server_args.<locals>.<dictcomp>)varsitemsupdate)r   parsed_argsargsr   r   r   server_argsc   s
    
zApplication.server_argsc                 C   s   t jf i | |S )Server.)r   Serverr2   )r   r0   r   r   r   r   m   s    zApplication.serverN)	r   r	   r
   r   classmethodr%   r   r2   r   r   r   r   r   r   K   s   
	
r   c                   @   s    e Zd ZdZdd Zdd ZdS )r!   zGateway.c                 C   s
   || _ dS )zInit.N)gateway)r   r6   r   r   r   r   u   s    zGatewayYo.__init__c                 C   sL   t | }|d |d< |jdur(|j|d< |jdur<|j|d< tjf i |S )r3   r   NZ
maxthreadsZ
minthreads)r-   max
numthreadsr   Z
HTTPServer)r   r0   r2   r   r   r   r   y   s    



zGatewayYo.serverN)r   r	   r
   r   r   r   r   r   r   r   r!   r   s   r!   c                 C   sx   |  drt| dd S tjdj| d}z,|j}|j}|dusL|durXt||W S W n t	yl   Y n0 t
| dS )z.Convert bind address string to a BindLocation.@r   Nz//{addr})addr)r   )r(   r   urllibparseurlparser   hostnamer   r   
ValueErrorr   )bind_addr_stringmatchr:   r   r   r   r   parse_wsgi_bind_location   s    

rB   c                 C   s
   t | jS )z6Convert bind address string to bind address parameter.)rB   r   )r@   r   r   r   parse_wsgi_bind_addr   s    rC   Z
APP_MODULEz<WSGI application callable or cheroot.server.Gateway subclass)metavartypehelpZADDRESSr   z
[::1]:8000z4Network interface to listen on (default: [::1]:8000))rD   destrE   defaultrF   PATHzSet the working directoryZserver_namez7Web server name to be advertised via Server HTTP header)rG   rE   rF   INTr8   z Minimum number of worker threads)rD   rG   rE   rF   r7   z Maximum number of worker threadstimeoutz+Timeout in seconds for accepted connectionsZshutdown_timeoutz:Time in seconds to wait for worker threads to cleanly exitrequest_queue_sizez$Maximum number of queued connectionsZaccepted_queue_sizez*Maximum number of active requests in queueZaccepted_queue_timeoutz2Timeout in seconds for putting requests into queue)	_wsgi_appz--bindz--chdirz--server-namez	--threadsz--max-threadsz	--timeoutz--shutdown-timeoutz--request-queue-sizez--accepted-queue-sizez--accepted-queue-timeoutc                  C   sd   t jdd} t D ]\}}| j|fi | q|  }dtjv pNtjdd |j	
|  dS )zCCreate a new Cheroot instance with arguments from the command line.z2Start an instance of the Cheroot WSGI/HTTP server.)description r   N)argparseArgumentParser	_arg_specr.   add_argument
parse_argssysr   insertrM   r   Z
safe_start)parserr*   specZraw_argsr   r   r   main   s    rY   )r   rP   osrU   urllib.parser;   	importlibr   
contextlibr   rO   r   r   r   r   r   r   r   r!   rB   rC   r%   chdirstrintrR   rY   r   r   r   r   <module>   s   'D