U
    9%e                      @   s   d dl 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
 d dlmZmZmZmZmZmZ dgZeeeef dddZeee d	d
dZee eeeef dddZeedddZeeeeef f ddddZG dd dZdS )    N)	timedelta)EventThread)AnyCallableDictOptionalTupleUnionparse_rendezvous_endpoint)
config_strreturnc                 C   s   i }|   } | s|S | d}|D ]Z}|dd^}}|  }|sJtd|r\|d   }nd}|sttd| d|||< q"|S )	zExtracts key-value pairs from a rendezvous configuration string.

    Args:
        config_str:
            A string in format <key1>=<value1>,...,<keyN>=<valueN>.
    ,=   zZThe rendezvous configuration string must be in format <key1>=<value1>,...,<keyN>=<valueN>.r   Nz%The rendezvous configuration option 'z' must have a value specified.)stripsplit
ValueError)r   config
key_valueskvkeyvaluesvalue r   i/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torch/distributed/elastic/rendezvous/utils.py_parse_rendezvous_config   s*    


r   )port_strr   c                 C   s   | rt d| rt| S dS )z3Tries to extract the port number from ``port_str``.z^[0-9]{1,5}$N)rematchint)r   r   r   r   _try_parse_port9   s    r!   )endpointdefault_portr   c                 C   s   | dk	r|   } | sd|fS | d dkrD| d dkrD| fg ^}}n| dd^}}t|dkr|d dkr|d dkr|dd }t|dkrt|d }|dks|d	krtd
|  dn|}td|std|  d||fS )a5  Extracts the hostname and the port number from a rendezvous endpoint.

    Args:
        endpoint:
            A string in format <hostname>[:<port>].
        default_port:
            The port number to use if the endpoint does not include one.

    Returns:
        A tuple of hostname and port number.
    N	localhostr   []:r   i   z,The port number of the rendezvous endpoint 'z)' must be an integer between 0 and 65536.z^[\w\.:-]+$z)The hostname of the rendezvous endpoint 'zN' must be a dot-separated list of labels, an IPv4 address, or an IPv6 address.)r   rsplitlenr!   r   r   r   )r"   r#   hostrestportr   r   r   r   @   s*    $

)r+   r   c              
   C   s"  | dkrdS zt | }W n tk
r2   d}Y nX |rB|jrBdS ztj| dtjtjd}W n, ttjfk
r } zg }W 5 d}~X Y nX dd |D }t	 }| |krdS tj|dtjtjd}|D ]V}|d r|d | kr dS |r|d d	 t
|kr dS |d d	 |kr dS qd
S )a=  Indicates whether ``host`` matches the hostname of this machine.

    This function compares ``host`` to the hostname as well as to the IP
    addresses of this machine. Note that it may return a false negative if this
    machine has CNAME records beyond its FQDN or IP addresses assigned to
    secondary NICs.
    r$   TN)protoflagsc                 S   s   g | ]}|d  d qS )   r   r   ).0Zhost_addr_infor   r   r   
<listcomp>   s   z-_matches_machine_hostname.<locals>.<listcomp>   r0   r   F)	ipaddress
ip_addressr   is_loopbacksocketgetaddrinfoIPPROTO_TCPAI_CANONNAMEgaierrorgethostnamestr)r+   addrZhost_addr_list_Zhost_ip_listZ	this_hostZ	addr_list	addr_infor   r   r   _matches_machine_hostnameo   sJ    

   
   rA   )secondsr   c                 C   s*   t | trtj|  } | dkr&t|  dS )zSuspends the current thread for ``seconds``.

    Args:
        seconds:
            Either the delay, in seconds, or a tuple of a lower and an upper
            bound within which a random delay will be picked.
    g{Gz?N)
isinstancetuplerandomuniformtimesleep)rB   r   r   r   _delay   s    

rI   c                   @   s   e Zd ZU dZG dd dZee ed< ee ed< ee	j
 ed< eed< eed eed	d
ddZeee dddZed	dddZd	dddZd	dddZed	dddZedd Zd	S )_PeriodicTimerzRepresents a timer that periodically runs a specified function.

    Args:
        interval:
            The interval, in seconds, between each run.
        function:
            The function to run.
    c                   @   sJ   e Zd ZU eed< ed ed< eedf ed< ee	ef ed< e
ed< dS )	z_PeriodicTimer._Contextinterval.Nfunction.argskwargs
stop_eventN)__name__
__module____qualname__float__annotations__r   r	   r   r   r=   r   r   r   r   r   _Context   s
   
rV   _name_thread
_finalizer_ctxrL   N)rK   rM   rN   rO   r   c                 O   sV   d | _ |  | _| | j_|| j_|p*d| j_|p6i | j_t | j_	d | _
d | _d S )Nr   )rW   rV   rZ   total_secondsrK   rM   rN   rO   r   rP   rX   rY   )selfrK   rM   rN   rO   r   r   r   __init__   s    

z_PeriodicTimer.__init__)r   c                 C   s   | j S )zGets the name of the timer.)rW   r\   r   r   r   name   s    z_PeriodicTimer.name)r_   r   c                 C   s   | j rtd|| _dS )zSets the name of the timer.

        The specified name will be assigned to the background thread and serves
        for debugging and troubleshooting purposes.
        The timer has already started.N)rX   RuntimeErrorrW   )r\   r_   r   r   r   set_name   s    z_PeriodicTimer.set_namec                 C   s\   | j rtdt| j| jpd| jfdd| _ t| | j| j | jj	| _
d| j
_| j   dS )zStart the timer.r`   ZPeriodicTimerT)targetr_   rN   daemonFN)rX   ra   r   _runrW   rZ   weakreffinalize_stop_threadrP   rY   atexitstartr^   r   r   r   rj      s           z_PeriodicTimer.startc                 C   s   | j r|    dS )z'Stop the timer at the next opportunity.N)rY   r^   r   r   r   cancel	  s    z_PeriodicTimer.cancelc                 C   s$   | j | js | j| j| j q d S N)rP   waitrK   rM   rN   rO   )ctxr   r   r   re     s    z_PeriodicTimer._runc                 C   s   |   |   d S rl   )setjoin)threadrP   r   r   r   rh     s    z_PeriodicTimer._stop_thread)rQ   rR   rS   __doc__rV   r   r=   rU   r   rf   rg   r   r   r   r]   propertyr_   rb   rj   rk   staticmethodre   rh   r   r   r   r   rJ      s*   
rJ   )r4   rE   r   r7   rG   rf   datetimer   	threadingr   r   typingr   r   r   r   r	   r
   __all__r=   r   r    r!   r   boolrA   rT   rI   rJ   r   r   r   r   <module>   s    &/7 