U
    ānld  ć                   @   sN   d dl Z dadd ZG dd dZG dd deZG dd	 d	eZd
d ZdS )é    Nc                   C   s   t S ©N)Śstorage© r   r   ś6/var/www/html/LibreTranslate/libretranslate/storage.pyŚget_storage   s    r   c                   @   st   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )ŚStoragec                 C   s   t dd S ©Nznot implemented©Ś	Exception©ŚselfŚkeyr   r   r   Śexists   s    zStorage.existsc                 C   s   t dd S r   r	   ©r   r   Śvaluer   r   r   Śset_bool   s    zStorage.set_boolc                 C   s   t dd S r   r	   r   r   r   r   Śget_bool   s    zStorage.get_boolc                 C   s   t dd S r   r	   r   r   r   r   Śset_int   s    zStorage.set_intc                 C   s   t dd S r   r	   r   r   r   r   Śget_int   s    zStorage.get_intc                 C   s   t dd S r   r	   r   r   r   r   Śset_str   s    zStorage.set_strc                 C   s   t dd S r   r	   r   r   r   r   Śget_str   s    zStorage.get_strc                 C   s   t dd S r   r	   ©r   Śnsr   r   r   r   r   Śset_hash_int   s    zStorage.set_hash_intc                 C   s   t dd S r   r	   ©r   r   r   r   r   r   Śget_hash_int   s    zStorage.get_hash_intc                 C   s   t dd S r   r	   r   r   r   r   Śinc_hash_int   s    zStorage.inc_hash_intc                 C   s   t dd S r   r	   r   r   r   r   Śdec_hash_int    s    zStorage.dec_hash_intc                 C   s   t dd S r   r	   ©r   r   r   r   r   Śget_hash_keys#   s    zStorage.get_hash_keysc                 C   s   t dd S r   r	   r   r   r   r   Śdel_hash%   s    zStorage.del_hashN)Ś__name__Ś
__module__Ś__qualname__r   r   r   r   r   r   r   r   r   r   r   r   r    r   r   r   r   r      s   r   c                   @   s|   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )ŚMemoryStoragec                 C   s
   i | _ d S r   ©Śstore)r   r   r   r   Ś__init__)   s    zMemoryStorage.__init__c                 C   s
   || j kS r   r%   r   r   r   r   r   ,   s    zMemoryStorage.existsc                 C   s   t || j|< d S r   ©Śboolr&   r   r   r   r   r   /   s    zMemoryStorage.set_boolc                 C   s   t | j| S r   r(   r   r   r   r   r   2   s    zMemoryStorage.get_boolc                 C   s   t || j|< d S r   )Śintr&   r   r   r   r   r   5   s    zMemoryStorage.set_intc                 C   s   t | j |d”S ©Nr   )r*   r&   Śgetr   r   r   r   r   8   s    zMemoryStorage.get_intc                 C   s   || j |< d S r   r%   r   r   r   r   r   ;   s    zMemoryStorage.set_strc                 C   s   t | j |d”S )NŚ )Śstrr&   r,   r   r   r   r   r   >   s    zMemoryStorage.get_strc                 C   s*   || j kri | j |< t|| j | |< d S r   )r&   r*   r   r   r   r   r   A   s    

zMemoryStorage.set_hash_intc                 C   s   | j  |i ”}t| |d”S r+   )r&   r,   r*   )r   r   r   Śdr   r   r   r   F   s    zMemoryStorage.get_hash_intc                 C   sL   || j kri | j |< || j | kr2d| j | |< n| j | |  d7  < d S ©Nr   é   r%   r   r   r   r   r   J   s
    

zMemoryStorage.inc_hash_intc                 C   sL   || j kri | j |< || j | kr2d| j | |< n| j | |  d8  < d S r0   r%   r   r   r   r   r   S   s
    

zMemoryStorage.dec_hash_intc                 C   s*   || j kr"dd | j |  ” D S g S d S )Nc                 S   s    g | ]\}}t |t|iqS r   )r.   r*   ©Ś.0ŚkŚvr   r   r   Ś
<listcomp>^   s     z2MemoryStorage.get_all_hash_int.<locals>.<listcomp>)r&   Śitemsr   r   r   r   Śget_all_hash_int\   s    
zMemoryStorage.get_all_hash_intc                 C   s   | j | |= d S r   r%   r   r   r   r   r    b   s    zMemoryStorage.del_hashN)r!   r"   r#   r'   r   r   r   r   r   r   r   r   r   r   r   r8   r    r   r   r   r   r$   (   s   		r$   c                   @   s|   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )ŚRedisStoragec                 C   s   t  |”| _| j ”  d S r   )ŚredisZfrom_urlŚconnZping)r   Z	redis_urir   r   r   r'   g   s    zRedisStorage.__init__c                 C   s   t | j |”S r   )r)   r;   r   r   r   r   r   r   k   s    zRedisStorage.existsc                 C   s   | j  ||rdnd” d S )NŚ1Ś0©r;   Śsetr   r   r   r   r   n   s    zRedisStorage.set_boolc                 C   s   t | j |”S r   )r)   r;   r,   r   r   r   r   r   q   s    zRedisStorage.get_boolc                 C   s   | j  |t|” d S r   )r;   r?   r.   r   r   r   r   r   t   s    zRedisStorage.set_intc                 C   s    | j  |”}|d krdS |S d S r+   )r;   r,   ©r   r   r5   r   r   r   r   w   s    zRedisStorage.get_intc                 C   s   | j  ||” d S r   r>   r   r   r   r   r   ~   s    zRedisStorage.set_strc                 C   s&   | j  |”}|d krdS | d”S d S )Nr-   śutf-8)r;   r,   Śdecoder@   r   r   r   r      s    zRedisStorage.get_strc                 C   s&   | j  ||”}|d krdS t|S d S r+   )r;   Zhgetr*   )r   r   r   r5   r   r   r   r      s    zRedisStorage.get_hash_intc                 C   s   | j  |||” d S r   )r;   Zhsetr   r   r   r   r      s    zRedisStorage.set_hash_intc                 C   s   t | j ||”S r   ©r*   r;   Zhincrbyr   r   r   r   r      s    zRedisStorage.inc_hash_intc                 C   s   t | j ||d”S )Né’’’’rC   r   r   r   r   r      s    zRedisStorage.dec_hash_intc                 C   s   dd | j  |” ” D S )Nc                 S   s    i | ]\}}|  d ”t|qS )rA   )rB   r*   r2   r   r   r   Ś
<dictcomp>   s      z1RedisStorage.get_all_hash_int.<locals>.<dictcomp>)r;   Zhgetallr7   r   r   r   r   r8      s    zRedisStorage.get_all_hash_intc                 C   s   | j  ||” d S r   )r;   Zhdelr   r   r   r   r       s    zRedisStorage.del_hashN)r!   r"   r#   r'   r   r   r   r   r   r   r   r   r   r   r   r8   r    r   r   r   r   r9   f   s   r9   c                 C   s6   |   d”rt an |   d”r&t| antd|  tS )Nz	memory://zredis://zInvalid storage URI: )Ś
startswithr$   r   r9   r
   )Śstorage_urir   r   r   Śsetup   s    


rH   )r:   r   r   r   r$   r9   rH   r   r   r   r   Ś<module>   s   !>8