
    DIe                       d Z ddlm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 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  ej        e          Zd Zd%dZed&d            Zd&dZd'dZ d(dZ!d)d"Z"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
    )annotationsN)datetimetimezonewraps)Path)Sequence   )user_cache_dir)APP_NAMENOTICES_CACHE_FNNOTICES_CACHE_SUBDIR)ensure_dir_exists   )ChannelNoticeChannelNoticeResponsec                >     t                     d fd            }|S )Nurlstrnamec                    t                      }t          | ||          }|r|S  | |          }|t          ||           |S N)get_notices_cache_dirget_notice_response_from_cachewrite_notice_response_to_cache)r   r   	cache_dir	cache_valreturn_valuefuncs        3lib/python3.11/site-packages/conda/notices/cache.pywrapperz cached_response.<locals>.wrapper   sX    )++	23iHH	 	tC#*<CCC    )r   r   r   r   r   )r   r!   s   ` r    cached_responser#      s8    
4[[     [ Nr"   channel_notice_responser   returnboolc                    t          j        t          j                  dfdt	          f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_atdatetime | Noner%   r&   c                    | dS | k     S )z?If there is no "expired_at" field present assume it is expired.NT )r(   nows    r    is_channel_notice_expiredzCis_notice_response_cache_expired.<locals>.is_channel_notice_expired8   s    4Cr"   c              3  8   K   | ]} |j                   V  d S r   )r(   ).0chnr-   s     r    	<genexpr>z3is_notice_response_cache_expired.<locals>.<genexpr>>   sD         	"!#.11     r"   )r(   r)   r%   r&   )r   r,   r   utcanynotices)r$   r-   r,   s    @@r     is_notice_response_cache_expiredr5   -   sr     ,x|
$
$C                *2     r"   r   c                     t          t          t                    } t          |                               t                    S )zDReturns the location of the notices cache directory as a Path object)	appauthor)r   r   r   joinpathr   )r   s    r    r   r   D   s1     x8<<<I	??##$8999r"   c                     t                      } |                     t                    }|                                s=t	          |d          5 }|                    d           ddd           n# 1 swxY w Y   |S )z?Returns the location of the notices cache file as a Path objectw N)r   r8   r   is_fileopenwrite)r   
cache_filefps      r    get_notices_cache_filerA   L   s    %''I##$455J *c"" 	bHHRLLL	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 s   A//A36A3r   r   r   r   ChannelNoticeResponse | Nonec                ,   t          j        | |          }t          j                            |          r]t          |          5 }t          j        |          }ddd           n# 1 swxY w Y   t          | ||          }t          |          s|S dS dS )z;Retrieves a notice response object from cache if it exists.N)	r   get_cache_keyospathisfiler=   jsonloadr5   )r   r   r   	cache_keyr@   datachn_ntc_resps          r    r   r   X   s     &3CCCI	w~~i    )__ 	!9R==D	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!,S$==/== 	    
	  	 s   A%%A),A)Nonec                    t          j        | j        |          }t          |d          5 }t	          j        | j        |           ddd           dS # 1 swxY w Y   dS )z3Writes our notice data to our local cache location.r:   N)r   rD   r   r=   rH   dump	json_data)r$   r   rJ   r@   s       r    r   r   g   s     &3#Y I 
i		 9	)3R8889 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9s   AAAr?   channel_noticesSequence[ChannelNotice]c           	        d |D             }t          |           5 }|                                }ddd           n# 1 swxY w Y   t          t          dt          |                                                              }|                    |          }t          | d          5 }|                    d                    |                     ddd           dS # 1 swxY w Y   dS )z;Insert channel notice into our database marking it as read.c                    h | ]	}|j         
S r+   idr/   r0   s     r    	<setcomp>z1mark_channel_notices_as_viewed.<locals>.<setcomp>w       444S#&444r"   Nr:   
)r=   readsetfilter
splitlinesunionr>   join)r?   rQ   
notice_idsr@   contentscontents_uniquecontents_news          r    mark_channel_notices_as_viewedre   s   sU    54O444J	j		 "R		" " " " " " " " " " " " " " " &s8+>+>+@+@'A'ABBCCO"((44L 
j#		 *"
<(()))* * * * * * * * * * * * * * * * * *s!   =AA))CC#&C#set[str]c           	     2   d |D             }t          |           5 }|                                }ddd           n# 1 swxY w Y   t          t          dt          |                                                              }|                    |          S )zCReturn the ids of the channel notices which have already been seen.c                    h | ]	}|j         
S r+   rU   rW   s     r    rX   z0get_viewed_channel_notice_ids.<locals>.<setcomp>   rY   r"   N)r=   r[   r\   r]   r^   intersection)r?   rQ   ra   r@   rb   rc   s         r    get_viewed_channel_notice_idsrj      s     54O444J	j		 "R		" " " " " " " " " " " " " " " &s8+>+>+@+@'A'ABBCCO""?333s   =AA)r$   r   r%   r&   )r%   r   )r   r   r   r   r   r   r%   rB   )r$   r   r   r   r%   rM   )r?   r   rQ   rR   r%   rM   )r?   r   rQ   rR   r%   rf   )$__doc__
__future__r   rH   loggingrE   r   r   	functoolsr   pathlibr   typingr	   _vendor.appdirsr   base.constantsr   r   r   utilsr   typesr   r   	getLogger__name__loggerr#   r5   r   rA   r   r   re   rj   r+   r"   r    <module>rx      s    # " " " " "   				 ' ' ' ' ' ' ' '                   , , , , , , M M M M M M M M M M % % % % % % 7 7 7 7 7 7 7 7		8	$	$  $   . : : : :	 	 	 	       	9 	9 	9 	9* * * *"4 4 4 4 4 4r"   