a
    d~                     @   sf   d dl mZmZ G dd dedZG dd deZG dd deZG d	d
 d
eZG dd deZdS )    )ABCMetaabstractmethodc                   @   s(   e Zd ZdZedd Zedd ZdS )BaseStorageBackendzAbstract class of storage backends.

    All backends need to implement two apis: ``get()`` and ``get_text()``.
    ``get()`` reads the file as a byte stream and ``get_text()`` reads the file
    as texts.
    c                 C   s   d S N selffilepathr   r   b/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/basicsr/utils/file_client.pyget   s    zBaseStorageBackend.getc                 C   s   d S r   r   r   r   r   r
   get_text   s    zBaseStorageBackend.get_textN)__name__
__module____qualname____doc__r   r   r   r   r   r   r
   r      s
   
r   )	metaclassc                   @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
MemcachedBackenda  Memcached storage backend.

    Attributes:
        server_list_cfg (str): Config file for memcached server list.
        client_cfg (str): Config file for memcached client.
        sys_path (str | None): Additional path to be appended to `sys.path`.
            Default: None.
    Nc                 C   sr   |d urdd l }|j| zdd l}W n tyB   tdY n0 || _|| _|j| j| j| _	|
 | _d S )Nr   z4Please install memcached to enable MemcachedBackend.)syspathappendmcImportErrorserver_list_cfg
client_cfgZMemcachedClientZGetInstance_clientZpyvector
_mc_buffer)r   r   r   sys_pathr   r   r   r   r
   __init__    s    zMemcachedBackend.__init__c                 C   s0   t |}dd l}| j|| j || j}|S )Nr   )strr   r   ZGetr   ZConvertBuffer)r   r	   r   	value_bufr   r   r
   r   /   s
    zMemcachedBackend.getc                 C   s   t d S r   NotImplementedErrorr   r   r   r
   r   6   s    zMemcachedBackend.get_text)Nr   r   r   r   r   r   r   r   r   r   r
   r      s   	
r   c                   @   s    e Zd ZdZdd Zdd ZdS )HardDiskBackendzRaw hard disks storage backend.c                 C   s>   t |}t|d}| }W d    n1 s00    Y  |S )Nrbr   openreadr   r	   fr   r   r   r
   r   =   s    &zHardDiskBackend.getc                 C   s>   t |}t|d}| }W d    n1 s00    Y  |S )Nrr%   r(   r   r   r
   r   C   s    &zHardDiskBackend.get_textN)r   r   r   r   r   r   r   r   r   r
   r#   :   s   r#   c                   @   s*   e Zd ZdZdddZdd Zd	d
 ZdS )LmdbBackenda5  Lmdb storage backend.

    Args:
        db_paths (str | list[str]): Lmdb database paths.
        client_keys (str | list[str]): Lmdb client keys. Default: 'default'.
        readonly (bool, optional): Lmdb environment parameter. If True,
            disallow any write operations. Default: True.
        lock (bool, optional): Lmdb environment parameter. If False, when
            concurrent access occurs, do not lock the database. Default: False.
        readahead (bool, optional): Lmdb environment parameter. If False,
            disable the OS filesystem readahead mechanism, which may improve
            random read performance when a database is larger than RAM.
            Default: False.

    Attributes:
        db_paths (list): Lmdb database path.
        _client (list): A list of several lmdb envs.
    defaultTFc           
      K   s   zdd l }W n ty&   tdY n0 t|tr8|g}t|trTdd |D | _nt|trjt|g| _t|t| jksJ dt| dt| j di | _t|| jD ](\}}	|j	|	f|||d|| j|< qd S )	Nr   z*Please install lmdb to enable LmdbBackend.c                 S   s   g | ]}t |qS r   )r   ).0vr   r   r
   
<listcomp>h       z(LmdbBackend.__init__.<locals>.<listcomp>zCclient_keys and db_paths should have the same length, but received z and .)readonlylock	readahead)
lmdbr   
isinstancer   listdb_pathslenr   zipr&   )
r   r8   Zclient_keysr2   r3   r4   kwargsr5   clientr   r   r   r
   r   ^   s$    



zLmdbBackend.__init__c                 C   sl   t |}|| jv s"J d| d| j| }|jdd }||d}W d   n1 s^0    Y  |S )zGet values according to the filepath from one lmdb named client_key.

        Args:
            filepath (str | obj:`Path`): Here, filepath is the lmdb key.
            client_key (str): Used for distinguishing different lmdb envs.
        zclient_key z is not in lmdb clients.F)writeasciiN)r   r   beginr   encode)r   r	   
client_keyr<   Ztxnr   r   r   r
   r   r   s    
.zLmdbBackend.getc                 C   s   t d S r   r    r   r   r   r
   r      s    zLmdbBackend.get_textN)r,   TFFr"   r   r   r   r
   r+   J   s   
r+   c                   @   s8   e Zd ZdZeeedZdddZdddZ	d	d
 Z
dS )
FileClienta  A general file client to access files in different backend.

    The client loads a file or text in a specified backend from its path
    and return it as a binary file. it can also register other backend
    accessor with a given name and backend class.

    Attributes:
        backend (str): The storage backend type. Options are "disk",
            "memcached" and "lmdb".
        client (:obj:`BaseStorageBackend`): The backend object.
    )diskZ	memcachedr5   rC   c                 K   sH   || j vr(td| dt| j   || _| j | f i || _d S )NzBackend z0 is not supported. Currently supported ones are )	_backends
ValueErrorr7   keysbackendr<   )r   rG   r;   r   r   r
   r      s    

zFileClient.__init__r,   c                 C   s(   | j dkr| j||S | j|S d S )Nr5   )rG   r<   r   )r   r	   rA   r   r   r
   r      s    
zFileClient.getc                 C   s   | j |S r   )r<   r   r   r   r   r
   r      s    zFileClient.get_textN)rC   )r,   )r   r   r   r   r#   r   r+   rD   r   r   r   r   r   r   r
   rB      s   

rB   N)	abcr   r   r   r   r#   r+   objectrB   r   r   r   r
   <module>   s
   $: