
    sio                       S r SSKJr  SSKrSSKrSSKJrJr  SSKJr  SSK	J
r
  SSKJr  SSKJr  S	S
KJrJrJrJr  S	SKJr  S	SKJr  SSKJr  \(       a  SSKJr  SSKJr  \R:                  " \5      rS r     SS jr!\SS j5       r"SS jr#        SS jr$      SS jr%      SS jr&      SS jr'S S jr(g)!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)TYPE_CHECKING)user_cache_dir   )APP_NAMENOTICES_CACHE_FNNOTICES_CACHE_SUBDIR"NOTICES_DECORATOR_DISPLAY_INTERVAL)json)ensure_dir_exists   )ChannelNoticeResponse)Sequence)ChannelNoticec                4   ^  [        T 5      SU 4S jj5       nU$ )Nc                r   > [        5       n[        XU5      nU(       a  U$ T" X5      nUb  [        XB5        U$ N)get_notices_cache_dirget_notice_response_from_cachewrite_notice_response_to_cache)urlname	cache_dir	cache_valreturn_valuefuncs        3lib/python3.13/site-packages/conda/notices/cache.pywrapper cached_response.<locals>.wrapper(   s=    )+	23iH	C#*<C    )r   strr   r&   r   )r!   r#   s   ` r"   cached_responser'   '   s     
4[  Nr%   c                   ^^ [         R                  " [        R                  5      mSU4S jjm[	        U4S jU R
                   5       5      $ )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.
c                   > U c  gU T:  $ )z?If there is no "expired_at" field present assume it is expired.T )
expired_atnows    r"   is_channel_notice_expiredCis_notice_response_cache_expired.<locals>.is_channel_notice_expiredD   s    Cr%   c              3  H   >#    U  H  nT" UR                   5      v   M     g 7fr   )r+   ).0chnr-   s     r"   	<genexpr>3is_notice_response_cache_expired.<locals>.<genexpr>J   s$      2C 	"#..112s   ")r+   zdatetime | Nonereturnbool)r   r,   r   utcanynotices)channel_notice_responser-   r,   s    @@r"    is_notice_response_cache_expiredr:   9   s>     ,,x||
$C   *22  r%   c                 b    [        [        [        S9n [        U 5      R                  [        5      $ )zDReturns the location of the notices cache directory as a Path object)	appauthor)r
   r   r   joinpathr   )r   s    r"   r   r   P   s&     x8<I	?##$899r%   c                 f   [        5       n U R                  [        5      nUR                  5       (       dk  [	        US5       nUR                  S5        SSS5        UR                  5       nUR                  [        -
  n[        R                  " XR                  U45        U$ ! , (       d  f       NU= f)a  
Return path of notices cache

If the file does not exist, we create it with natural filesystem timestamps,
then set only the modification time to be in the past. This ensures notices
are checked and displayed immediately rather than waiting for the full
display interval.
w N)r   r=   r   is_fileopenwritestatst_mtimer   osutimest_atime)r   
cache_filefprD   
past_mtimes        r"   get_notices_cache_filerL   X   s     &'I##$45J*c"bHHRL #  ]]%GG

mmZ89 #"s   B""
B0c                ,   [         R                  " X5      n[        R                  R	                  U5      (       aI  [        U5       n[        R                  " U5      nSSS5        [        XW5      n[        U5      (       d  U$ gg! , (       d  f       N.= f)z;Retrieves a notice response object from cache if it exists.N)	r   get_cache_keyrF   pathisfilerB   r   loadr:   )r   r   r   	cache_keyrJ   datachn_ntc_resps          r"   r   r   p   so     &33CCI	ww~~i  )_99R=D ,S=/== > !_s   B
Bc                    [         R                  " U R                  U5      n[        US5       n[        R
                  " U R                  U5        SSS5        g! , (       d  f       g= f)z3Writes our notice data to our local cache location.r?   N)r   rN   r   rB   r   dump	json_data)r9   r   rR   rJ   s       r"   r   r      sN     &33##YI 
i			)33R8 
		s   "A
A'c           	        U Vs1 s H  o"R                   iM     nn[        U 5       nUR                  5       nSSS5        [        [	        S[        WR                  5       5      5      5      nUR                  U5      n[        U S5       nUR                  SR                  U5      5        SSS5        gs  snf ! , (       d  f       N= f! , (       d  f       g= f)z;Insert channel notice into our database marking it as read.Nr?   
)	idrB   readsetfilter
splitlinesunionrC   join)rI   channel_noticesr1   
notice_idsrJ   contentscontents_uniquecontents_news           r"   mark_channel_notices_as_viewedrf      s     %44OS&&OJ4	j	R	 
 &s8+>+>+@'ABCO"((4L 
j#	"
<() 
	 5		 
	s   B3B8	!C	8
C	
Cc           	     &   U Vs1 s H  o"R                   iM     nn[        U 5       nUR                  5       nSSS5        [        [	        S[        WR                  5       5      5      5      nUR                  U5      $ s  snf ! , (       d  f       NP= f)zCReturn the ids of the channel notices which have already been seen.N)rZ   rB   r[   r\   r]   r^   intersection)rI   ra   r1   rb   rJ   rc   rd   s          r"   get_viewed_channel_notice_idsri      sr     %44OS&&OJ4	j	R	 
 &s8+>+>+@'ABCO""?33 5		s   A=B
Bc                     [        [        5       5      n U R                  5        H*  nUR                  5       (       d  M  UR	                  5         M,     g)z$
Removes all files in notices cache
N)r   r   iterdirrA   unlink)r   rI   s     r"   clear_cacherm      s@     *,-I'')
 *r%   )r9   r   r4   r5   )r4   r   )r   r&   r   r&   r   r   r4   zChannelNoticeResponse | None)r9   r   r   r   r4   None)rI   r   ra   Sequence[ChannelNotice]r4   rn   )rI   r   ra   ro   r4   zset[str])r4   rn   ))__doc__
__future__r   loggingrF   r   r   	functoolsr   pathlibr   typingr	   platformdirsr
   base.constantsr   r   r   r   common.serializer   utilsr   typesr   collections.abcr   r   	getLogger__name__loggerr'   r:   r   rL   r   r   rf   ri   rm   r*   r%   r"   <module>r      s   #  	 '     '  $ % (($			8	$$2	. : :0 	  $( ! 	92	9?C	9		9**'>*	*"44'>44 r%   