U
    bZ                     @   s8  d Z ddlZddlZddlZddlmZmZ ddlm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mZmZ dd
lmZmZ ddlmZmZ eeZdd ZeedddZee	dddZ e	dddZ!e"e"e	ee dddZ#ee	ddddZ$e	ee ddddZ%e	ee ee" dd d!Z&dS )"z
Handles all caching logic including:
  - Retrieving from cache
  - Saving to cache
  - Determining whether not certain items have expired and need to be refreshed
    N)datetimetimezone)wraps)Path)OptionalSequenceSet   )user_cache_dir)APP_NAMENOTICES_CACHE_SUBDIRNOTICES_CACHE_FN)ensure_dir_exists	safe_open   )ChannelNoticeResponseChannelNoticec                    s    t  ttd fdd}|S )N)urlnamec                    s:   t  }t| ||}|r|S  | |}|d k	r6t|| |S N)get_notices_cache_dirget_notice_response_from_cachewrite_notice_response_to_cache)r   r   	cache_dirZ	cache_valZreturn_valuefunc 2lib/python3.8/site-packages/conda/notices/cache.pywrapper   s    

z cached_response.<locals>.wrapper)r   str)r   r   r   r   r   cached_response   s    r    )channel_notice_responsereturnc                    s<   t tjtt  tdfdd t fdd| jD S )z
    This checks the contents of the cache response to see if it is expired.

    If for whatever reason we encounter an exception while parsing the individual
    messages, we assume an invalid cache and return true.
    )
expired_atr"   c                    s   | dkrdS |  k S )zP
        If there is no "expired_at" field present assume it is expired
        NTr   r#   )nowr   r   is_channel_notice_expired7   s    zCis_notice_response_cache_expired.<locals>.is_channel_notice_expiredc                 3   s   | ]} |j V  qd S r   r$   .0Zchn)r&   r   r   	<genexpr>@   s     z3is_notice_response_cache_expired.<locals>.<genexpr>)r   r%   r   Zutcr   boolanyZnotices)r!   r   )r&   r%   r    is_notice_response_cache_expired.   s
    r,   )r"   c                  C   s   t ttd} t| tS )zDReturns the location of the notices cache directory as a Path object)Z	appauthor)r
   r   r   joinpathr   )r   r   r   r   r   D   s    r   c               	   C   s<   t  } | t}| s8t|d}|d W 5 Q R X |S )z?Returns the location of the notices cache file as a Path objectw )r   r-   r   is_filer   write)r   
cache_filefpr   r   r   get_notices_cache_fileL   s    
r4   )r   r   r   r"   c              	   C   sT   t | |}tj|rPt|d}t|}W 5 Q R X t | ||}t|sP|S dS )zE
    Retrieves a notice response object from cache if it exists.
    rN)	r   get_cache_keyospathisfiler   jsonloadr,   )r   r   r   	cache_keyr3   dataZchn_ntc_respr   r   r   r   X   s    r   )r!   r   r"   c              	   C   s6   t | j|}t|d}t| j| W 5 Q R X dS )z<
    Writes our notice data to our local cache location
    r.   N)r   r6   r   r   r:   dumpZ	json_data)r!   r   r<   r3   r   r   r   r   i   s    r   )r2   channel_noticesr"   c              	   C   sz   t dd |D }t| d}| }W 5 Q R X t tdt | }||}t| d}|d| W 5 Q R X dS )zE
    Insert channel notice into our database marking it as read.
    c                 s   s   | ]}|j V  qd S r   idr'   r   r   r   r)   {   s     z1mark_channel_notices_as_viewed.<locals>.<genexpr>r5   Nr.   
)setr   readfilter
splitlinesunionr1   join)r2   r?   
notice_idsr3   contentscontents_uniqueZcontents_newr   r   r   mark_channel_notices_as_viewedu   s    
rL   c              	   C   sP   t dd |D }t| d}| }W 5 Q R X t tdt | }||S )zM
    Return the ids of the channel notices which have already been seen.
    c                 s   s   | ]}|j V  qd S r   r@   r'   r   r   r   r)      s     z0get_viewed_channel_notice_ids.<locals>.<genexpr>r5   N)rC   r   rD   rE   rF   intersection)r2   r?   rI   r3   rJ   rK   r   r   r   get_viewed_channel_notice_ids   s
    rN   )'__doc__r:   Zloggingr7   r   r   	functoolsr   pathlibr   typingr   r   r   Z_vendor.appdirsr
   Zbase.constantsr   r   r   Zutilsr   r   typesr   r   Z	getLogger__name__Zloggerr    r*   r,   r   r4   r   r   r   rL   rN   r   r   r   r   <module>   sD   
     