o
    tf                     @  sv   d dl m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 ej	dkr,d dl
mZ nd dlmZ G dd dZdS )    )annotationsN)ASGIApp)      )Selfc                   @  s   e Zd ZU dZded< ded< ded< ded	< d
ed< d*ddZed+ddZed,ddZed-ddZ	d.ddZ
ddddd d!d/d(d)ZdS )0BackgroundServerz7A threading-based background server for Starlette apps.r   _appz
str | None_host
int | None_portzthreading.Thread | None_server_threadzuvicorn.Server | None_serverappreturnNonec                 C  s"   || _ d| _d| _d| _d| _dS )zInitialize a background server for the given Starlette app.

        Parameters
        ----------
        app : ASGIApp
            The Starlette app to run in the background.
        N)r   r	   r   r   r   )selfr    r   b/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/servir/_background_server.py__init__   s
   
zBackgroundServer.__init__c                 C  s   | j S )z.The Starlette app being run in the background.)r   r   r   r   r   r   (   s   zBackgroundServer.appstrc                 C  "   | j du s
| jdu rtd| jS )z&The host to which the server is bound.NzServer not running)r   r	   RuntimeErrorr   r   r   r   host-      zBackgroundServer.hostintc                 C  r   )z(The port on which the server is running.NzServer not running.)r   r   r   r   r   r   r   port4   r   zBackgroundServer.portr   c                 C  sP   | j du r| S | jdusJ zd| j_| j   W d| _d| _ | S d| _d| _ w )z"Stop the background server thread.NT)r   r   Zshould_exitjoinr   r   r   r   stop;   s   
zBackgroundServer.stop	localhostN   Twarning)r   r   timeoutdaemon	log_levelr   r   r"   r#   boolr$   c                C  s   | j dur| S tj| j||pt ||d}|j| _|j| _	tj
|d| _tj| jj|d| _ | j   | jjsAtd | jjr8| S )a  Start app in a background thread.

        Parameters
        ----------
        host : str, optional
            The host on which to bind the server. If not provided, the server will bind
            to localhost. You can use "0.0.0.0" to expose the server to the local network
            or use a hostname. IPv6 addresses are supported.
        port : int, optional
            The port on which to run the server. If not provided, a random port will be
            selected.
        timeout : int, optional
            The timeout for keep-alive connections, by default 1.
        daemon : bool, optional
            Whether to run the server thread as a daemon thread, by default True.
        log_level : str, optional
            The log level for the server, by default "warning".

        Returns
        -------
        BackgroundServer
            The background server instance.
        N)r   r   r   Ztimeout_keep_aliver$   )config)targetr#   gMbP?)r   uvicornZConfigr   
portpickerZpick_unused_portr   r	   r   r   Serverr   	threadingThreadrunstartstartedtimesleep)r   r   r   r"   r#   r$   r&   r   r   r   r.   K   s$   
 


zBackgroundServer.start)r   r   r   r   )r   r   )r   r   )r   r   )r   r   )r   r   r   r
   r"   r   r#   r%   r$   r   r   r   )__name__
__module____qualname____doc____annotations__r   propertyr   r   r   r   r.   r   r   r   r   r      s*   
 

r   )
__future__r   sysr+   r0   r)   r(   Zstarlette.typesr   version_infotypingr   Ztyping_extensionsr   r   r   r   r   <module>   s    
