o
    ;h4                     @   s   d dl Z d dlZd dlmZ d dlmZmZmZ d dl	m
Z
 ddlmZ ddlmZmZ ddlmZ G d	d
 d
eZG dd deZG dd deZdS )    N)	timedelta)AnyOptionalProtocol)Flask   )total_seconds)ServerSideSessionServerSideSessionInterface)Defaultsc                   @   sN   e Zd Zdedee fddZdedededefddZ	dedefd	d
Z
dS )MemcacheClientProtocolkeyreturnc                 C      d S N selfr   r   r   T/var/www/html/venv/lib/python3.10/site-packages/flask_session/memcached/memcached.pyget       zMemcacheClientProtocol.getvaluetimeoutc                 C   r   r   r   )r   r   r   r   r   r   r   set   r   zMemcacheClientProtocol.setc                 C   r   r   r   r   r   r   r   delete   r   zMemcacheClientProtocol.deleteN)__name__
__module____qualname__strr   r   r   intboolr   r   r   r   r   r   r      s    r   c                   @   s   e Zd ZdS )MemcachedSessionN)r   r   r   r   r   r   r   r!      s    r!   c                       s   e Zd ZdZejZeZdZe	j
e	je	je	je	je	jfdedee dedededed	ef fd
dZdd ZdedefddZdedee fddZdeddfddZdedededdfddZ  ZS )MemcachedSessionInterfacea  A Session interface that uses memcached as session storage. (`pylibmc`, `libmc`, `python-memcached` or `pymemcache` required)

    :param client: A ``memcache.Client`` instance.
    :param key_prefix: A prefix that is added to all storage keys.
    :param use_signer: Whether to sign the session id cookie or not.
    :param permanent: Whether to use permanent session or not.
    :param sid_length: The length of the generated session id in bytes.
    :param serialization_format: The serialization format to use for the session data.

    .. versionadded:: 0.7
        The `serialization_format` and `app` parameters were added.

    .. versionadded:: 0.6
        The `sid_length` parameter was added.

    .. versionadded:: 0.2
        The `use_signer` parameter was added.
    Tappclient
key_prefix
use_signer	permanent
sid_lengthserialization_formatc                    sV    d u st  fdddD stjdtdd |    | _t |||||| d S )Nc                 3   s    | ]}t  |V  qd S r   )hasattr).0methodr$   r   r   	<genexpr>9   s    

z5MemcachedSessionInterface.__init__.<locals>.<genexpr>)r   r   r   zsNo valid memcache.Client instance provided, attempting to create a new instance on localhost with default settings.   )
stacklevel)allwarningswarnRuntimeWarning_get_preferred_memcache_clientr$   super__init__)r   r#   r$   r%   r&   r'   r(   r)   	__class__r-   r   r7   /   s   
z"MemcachedSessionInterface.__init__c              	   C   sb   ddgfddgfdddgfg}|D ]\}}zt |}|j}||W   S  ty,   Y qw td)NZpylibmc127.0.0.1:11211Zmemcache)zpymemcache.client.baser:   Zlibmczlocalhost:11211zNo memcache module found)
__import__ZClientImportError)r   Zclientsmodule_nameservermoduleZClientClassr   r   r   r5   G   s   z8MemcachedSessionInterface._get_preferred_memcache_clientr   r   c                 C   s   |dkr|t t 7 }|S )z
        Memcached deals with long (> 30 days) timeouts in a special
        way. Call this function to obtain a safe value for your timeout.
        i ' )r   time)r   r   r   r   r   _get_memcache_timeoutY   s   z/MemcachedSessionInterface._get_memcache_timeoutstore_idc                 C   s    | j |}|r| j|S d S r   )r$   r   
serializerdecode)r   rB   serialized_session_datar   r   r   _retrieve_session_datac   s   z0MemcachedSessionInterface._retrieve_session_dataNc                 C   s   | j | d S r   )r$   r   )r   rB   r   r   r   _delete_sessionj   s   z)MemcachedSessionInterface._delete_sessionsession_lifetimesessionc                 C   s.   t |}| j|}| j||| | d S r   )r   rC   encoder$   r   rA   )r   rH   rI   rB   Zstorage_time_to_liverE   r   r   r   _upsert_sessionm   s   z)MemcachedSessionInterface._upsert_session) r   r   r   __doc__r
   rC   r!   Zsession_classttlr   ZSESSION_MEMCACHEDZSESSION_KEY_PREFIXZSESSION_USE_SIGNERZSESSION_PERMANENTZSESSION_ID_LENGTHZSESSION_SERIALIZATION_FORMATr   r   r   r   r    r   r7   r5   rA   dictrF   rG   	TimeDeltar	   rK   __classcell__r   r   r8   r   r"      sN    
r"   )r@   r2   datetimer   rO   typingr   r   r   Zflaskr   Z_utilsr   baser	   r
   defaultsr   r   r!   r"   r   r   r   r   <module>   s    