o
    -wiD                     @  s   d dl mZ d dlZd dlZd dlZd dlZd dlmZmZ d dl	m
Z
 d dlmZ d dlmZ dZee dZee d	ZG d
d deZdddZdddZG dd dejZeG dd deZeG dd deZdS )    )annotationsN)finaloverride)env)ipc_support)
SockClient3z-(\d+)-unix-(.+)z-(\d+)-tcp-localhost-(\d+)c                   @  s   e Zd ZdZdS )WandbServiceConnectionErrorz)Failed to connect to the service process.N)__name__
__module____qualname____doc__ r   r   `/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/sdk/lib/service/service_token.pyr	      s    r	   returnNonec                   C  s   t jtjd dS )z=Clear the environment variable that stores the service token.N)osenvironpopr   SERVICEr   r   r   r   clear_service_in_env      r   ServiceToken | Nonec                  C  sP   t jtj} | sdS t|  }r|S t|  }r|S tdtj d| )zRead the token from environment variables.

    Returns:
        The token if the correct environment variable is set, or None.

    Raises:
        ValueError: If the environment variable is set but cannot be
            parsed.
    NzFailed to parse =)	r   r   getr   r   UnixServiceTokenfrom_env_stringTCPServiceToken
ValueError)token
unix_token	tcp_tokenr   r   r   from_env   s   
r"   c                   @  s:   e Zd ZdZejdddZdddZejdd
dZdS )ServiceTokenz1A way of connecting to a running service process.r   r   c                 C     dS )zConnect to the service process.

        Returns:
            A socket object for communicating with the service.

        Raises:
            WandbServiceConnectionError: on failure to connect.
        Nr   selfr   r   r   connect7       zServiceToken.connectr   c                 C  s   |   tjtj< dS )z7Save the token in this process's environment variables.N)_as_env_stringr   r   r   r   r%   r   r   r   save_to_envB   r   zServiceToken.save_to_envstrc                 C  r$   )z.Returns a string representation of this token.Nr   r%   r   r   r   r)   F   r(   zServiceToken._as_env_stringNr   r   r   r   )r   r+   )	r
   r   r   r   abcabstractmethodr'   r*   r)   r   r   r   r   r#   4   s    

r#   c                   @  sB   e Zd ZdZddd	ZedddZedd ZedddZ	dS )r   z3Connects to the service using a Unix domain socket.
parent_pidintpathr+   r   r   c                C     || _ || _d S N)_parent_pid_path)r&   r0   r2   r   r   r   __init__O      
zUnixServiceToken.__init__r   c              
   C  s`   t jstdttjtj}z|| j W t	|S  ty/ } z	td| j |d }~ww )NzAF_UNIX socket not supportedz'Failed to connect to service on socket )
r   SUPPORTS_UNIXr	   socketAF_UNIXSOCK_STREAMr'   r6   	Exceptionr   r&   socker   r   r   r'   S   s   
zUnixServiceToken.connectc                 C  s   d tt| jdt| jfS )N-unix)join_CURRENT_VERSIONr+   r5   r6   r%   r   r   r   r)   d   s   zUnixServiceToken._as_env_stringr   UnixServiceToken | Nonec                 C  s.   t | }|s	dS | \}}tt||dS )z5Returns a Unix service token parsed from the env var.N)r0   r2   )_UNIX_TOKEN_RE	fullmatchgroupsr   r1   )r   matchr0   r2   r   r   r   r   o   s
   
z UnixServiceToken.from_env_stringN)r0   r1   r2   r+   r   r   r,   )r   r+   r   rE   
r
   r   r   r   r7   r   r'   r)   staticmethodr   r   r   r   r   r   K   s    


r   c                   @  sB   e Zd ZdZdddZedd
dZedd ZedddZ	dS )r   z:Connects to the service using TCP over a localhost socket.r0   r1   portr   r   c                C  r3   r4   )r5   _port)r&   r0   rL   r   r   r   r7   ~   r8   zTCPServiceToken.__init__r   c              
   C  sV   t  t jt j}z|d| jf W t|S  ty* } z	td| j |d }~ww )N	localhostz%Failed to connect to service on port )r:   AF_INETr<   r'   rM   r=   r	   r   r>   r   r   r   r'      s   
zTCPServiceToken.connectc                 C  s    d tt| jddt| jfS )NrA   tcprN   )rC   rD   r+   r5   rM   r%   r   r   r   r)      s   zTCPServiceToken._as_env_stringr   r+   TCPServiceToken | Nonec                 C  s2   t | }|s	dS | \}}tt|t|dS )z4Returns a TCP service token parsed from the env var.N)r0   rL   )_TCP_TOKEN_RErG   rH   r   r1   )r   rI   r0   rL   r   r   r   r      s
   
zTCPServiceToken.from_env_stringN)r0   r1   rL   r1   r   r   r,   )r   r+   r   rQ   rJ   r   r   r   r   r   z   s    

r   r-   )r   r   )
__future__r   r.   r   rer:   typing_extensionsr   r   wandbr   wandb.sdk.lib.servicer   wandb.sdk.lib.sock_clientr   rD   compilerF   rR   r=   r	   r   r"   ABCr#   r   r   r   r   r   r   <module>   s(    

.