o
    g                     @   s   d dl Z d dlZd dlZd dlZ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Zd dlZzd dlZW n eyA   dZY nw eeZG dd dZG dd deZG dd	 d	eZG d
d deZdd Zdd ZdS )    N)contextmanager)DictTupleUnionc                   @   s    e Zd ZdZdd Zdd ZdS )Basez Base class for caching backends.c                 C      t  NNotImplementedErrorselfurlcontent r   C/home/ubuntu/webapp/venv/lib/python3.10/site-packages/zeep/cache.pyadd      zBase.addc                 C   r   r   r	   )r   r   r   r   r   get   r   zBase.getN)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s    r   c                   @   s,   e Zd ZdZdd Zdd Zedd ZdS )	VersionedCacheBasezuVersioned base class for caching backends.
    Note when subclassing a version class attribute must be provided.
    c                 C   s   t |}| j| S )zHelper function for encoding cacheable content as base64.
        :param data: Content to be encoded.
        :rtype: bytes
        )base64	b64encode_version_stringr   datar   r   r   _encode_data'   s   

zVersionedCacheBase._encode_datac                 C   s(   | | jrt|t| jd S dS )z~Helper function for decoding base64 cached content.
        :param data: Content to be decoded.
        :rtype: bytes
        N)
startswithr   r   	b64decodelenr   r   r   r   _decode_data/   s   zVersionedCacheBase._decode_datac                 C   s0   t | dddusJ dd| j }t|dS )z]Expose the version prefix to be used in content serialization.
        :rtype: bytes
        _versionNzJA version must be provided in order to use the VersionedCacheBase backend.z	$ZEEP:%s$ascii)getattrr#   bytesencode)r   prefixr   r   r   r   7   s
   
z"VersionedCacheBase._version_stringN)r   r   r   r   r   r"   propertyr   r   r   r   r   r   "   s    r   c                   @   sR   e Zd ZU dZi Zeeeeje	e
ef f f ed< dddZdd Zdd	 Zd
S )InMemoryCachezDSimple in-memory caching using dict lookup with support for timeouts_cache  c                 C   s
   || _ d S r   )_timeout)r   timeoutr   r   r   __init__I   s   
zInMemoryCache.__init__c                 C   sL   t d| t|ttfstdt|jt	j	
t	jj|f| j|< d S )NCaching contents of %sz'a bytes-like object is required, not {})loggerdebug
isinstancestrr&   	TypeErrorformattyper   datetimenowtimezoneutcr+   r   r   r   r   r   L   s   zInMemoryCache.addc                 C   sR   z	| j | \}}W n	 ty   Y nw t|| js!td| |S td| d S )NCache HIT for %sCache MISS for %s)r+   KeyError_is_expiredr-   r1   r2   )r   r   createdr   r   r   r   r   T   s   zInMemoryCache.getN)r,   )r   r   r   r   r+   r   r4   r   r8   r   r&   __annotations__r/   r   r   r   r   r   r   r*   C   s   
 &
r*   c                   @   s:   e Zd ZdZdZdddZedd Zd	d
 Zdd Z	dS )SqliteCachez7Cache contents via a sqlite database on the filesystem.1Nr,   c                 C   s   t d u rtd|dkrtdt | _|| _|r|nt | _| 	 }|
 }|d |  W d    d S 1 s=w   Y  d S )Nz.sqlite3 module is required for the SqliteCachez:memory:zkThe SqliteCache doesn't support :memory: since it is not thread-safe. Please use zeep.cache.InMemoryCache()z
                    CREATE TABLE IF NOT EXISTS request
                    (created timestamp, url text, content text)
                )sqlite3RuntimeError
ValueError	threadingRLock_lockr-   _get_default_cache_path_db_pathdb_connectioncursorexecutecommit)r   pathr.   connrM   r   r   r   r/   f   s    


"zSqliteCache.__init__c                 c   sV    t sJ | j t j| jt jd}|V  |  W d    d S 1 s$w   Y  d S )N)detect_types)rD   rI   connectrK   PARSE_DECLTYPESclose)r   
connectionr   r   r   rL      s   
"zSqliteCache.db_connectionc                 C   s   t d| | |}|  '}| }|d|f |dtjtjj	||f |
  W d    d S 1 s9w   Y  d S )Nr0   z!DELETE FROM request WHERE url = ?z<INSERT INTO request (created, url, content) VALUES (?, ?, ?))r1   r2   r   rL   rM   rN   r8   r9   r:   r;   rO   )r   r   r   r   rQ   rM   r   r   r   r      s   


"zSqliteCache.addc                 C   s   |   }| }|d|f | }W d    n1 sw   Y  |r<|d \}}t|| js<td| | |S td| d S )Nz0SELECT created, content FROM request WHERE url=?r   r<   r=   )	rL   rM   rN   fetchallr?   r-   r1   r2   r"   )r   r   rQ   rM   rowsr@   r   r   r   r   r      s   


zSqliteCache.get)Nr,   )
r   r   r   r   r#   r/   r   rL   r   r   r   r   r   r   rB   a   s    


rB   c                 C   sL   |du rdS t j t jjjtjd}| jtjd}|t j|d7 }||kS )z&Return boolean if the value is expiredNF)tzinfo)seconds)r8   r9   r:   r;   replacepytz	timedelta)valuer.   r9   max_ager   r   r   r?      s   r?   c               
   C   sj   t dd} zt|  W n  ty- } z|jtjkr"tj| r"n W Y d }~nd }~ww tj	| dS )NzeepFzcache.db)
platformdirsuser_cache_dirosmakedirsOSErrorerrnoEEXISTrP   isdirjoin)rP   excr   r   r   rJ      s   rJ   )r   r8   rf   loggingrc   rG   
contextlibr   typingr   r   r   ra   r\   rD   ImportError	getLoggerr   r1   r   r   r*   rB   r?   rJ   r   r   r   r   <module>   s,    

!F