o
    /wiZ                     @  s   U d dl m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	 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 e	rNd dlZn
d dlmZ edZg dZded< ddgZdZG dd dZdddZdS )    )annotationsN)TracebackType)Any)IO)TYPE_CHECKING)BaseStorage)GrpcStorageProxy)JournalFileBackend)NamedTemporaryFilePool)_LazyImportgrpc)inmemorysqlitecached_sqlitejournaljournal_redisgrpc_rdbgrpc_journal_filez	list[Any]STORAGE_MODESr   r   i,  c                   @  s4   e Zd ZdddZdd
dZdddZdddZdS )StorageSupplierstorage_specifierstrkwargsr   returnNonec                 K  s(   || _ || _d | _d | _d | _d | _d S )N)r   
extra_argstempfileserverthreadproxy)selfr   r    r!   T/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/optuna/testing/storages.py__init__/   s   
zStorageSupplier.__init__optuna.storages.InMemoryStorage | optuna.storages._CachedStorage | optuna.storages.RDBStorage | optuna.storages.JournalStorage | optuna.storages.GrpcStorageProxyc                 C  s  | j dkrt| jdkrtdtj S d| j v rEt  | _d	| jj
}tjj|fdddtiii| j}d	| j v rCtj|S |S | j d
kratjjd}| jdt |_tj|S | j dkr| jdt  | _| jd usxJ tjtjj| jj
}| |S d| j v r| jdt  | _| jd usJ t| jj
}tj|S | j dkrt  | _d	| jj
}| tj|S | j dkrd| jv sJ | | jd S J )Nr   r   z.InMemoryStorage does not accept any arguments!r   zsqlite:///{}engine_kwargsconnect_argstimeoutcachedr   zredis://localhostredisr   filer   r   
grpc_proxybase_storage)r   lenr   
ValueErroroptunastoragesInMemoryStorager
   r   formatname
RDBStorageSQLITE3_TIMEOUT_CachedStorager   JournalRedisBackendget	fakeredisFakeStrictRedis_redisJournalStorager	   _create_proxy)r    urlrdb_storagejournal_redis_storagestoragefile_storager!   r!   r"   	__enter__7   s^   
	










zStorageSupplier.__enter__rA   r   r   c                 C  sZ   t  }tjjj|d|| _tj| jjd| _	| j	  t
d|d| _| jjdd | jS )N	localhost)target)hostport<   )r'   )_find_free_portr/   r0   _grpcr   make_server	threadingThreadstartr   r   r   wait_server_ready)r    rA   rG   r!   r!   r"   r=   o   s   
zStorageSupplier._create_proxyexc_typetype[BaseException]exc_valBaseExceptionexc_tbr   c                 C  sh   | j r| j   | jr| j  d | _| jr2| jd usJ | jd  | j  d | _d | _d S d S )N   )r   closer   r   r   stopwaitjoin)r    rP   rR   rT   r!   r!   r"   __exit__x   s   



zStorageSupplier.__exit__N)r   r   r   r   r   r   )r   r$   )rA   r   r   r   )rP   rQ   rR   rS   rT   r   r   r   )__name__
__module____qualname__r#   rC   r=   rZ   r!   r!   r!   r"   r   .   s
    


8	r   r   intc               	   C  sR   t  t jt j} tddD ]}z| d|f |W   S  ty$   Y qw J d)Ni2  i,3  rD   Fzmust not reach here)socketAF_INETSOCK_STREAMrangebindOSError)sockrG   r!   r!   r"   rI      s   
rI   )r   r^   )
__future__r   r_   rL   typesr   typingr   r   r   r9   r/   optuna.storagesr   r   optuna.storages.journalr	   optuna.testing.tempfile_poolr
   r   optuna._importsr   r   __annotations__STORAGE_MODES_HEARTBEATr5   r   rI   r!   r!   r!   r"   <module>   s0    
\