o
    i*                     @   sn   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Zddlm	Z	m
Z
mZmZ ddlmZ G dd dZdS )    N   )expand_pathtraverse_objversion_tuplewrite_json_file)__version__c                   @   sZ   e Zd Zdd Zdd Zdd Zedd Zdd
dZdd Z	dddddZ
dd ZdS )Cachec                 C   s
   || _ d S N)_ydl)selfydl r   @/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/cache.py__init__   s   
zCache.__init__c                 C   s8   | j jd}|d u rtdd}tj|d}t|S )NcachedirXDG_CACHE_HOMEz~/.cachezyt-dlp)r
   paramsgetosgetenvpathjoinr   )r   res
cache_rootr   r   r   _get_root_dir   s
   zCache._get_root_dirc                 C   sP   t d|sJ d|tjj|dddd}tj| 	 || d| S )Nz	^[\w.-]+$zinvalid section  )safe%,.)
rematchurllibparsequotereplacer   r   r   r   )r   sectionkeydtyper   r   r   _get_cache_fn   s   zCache._get_cache_fnc                 C   s   | j jdduS )Nr   F)r
   r   r   r   r   r   r   enabled   s   zCache.enabledjsonc              	   C   s   |dv sJ | j sd S | |||}z#tjtj|dd | jd| d| d tt	|d| W d S  t
yO   t }| jd|d	|  Y d S w )
Nr,   T)exist_okzSaving r   z	 to cache)yt-dlp_versiondatazWriting cache to z	 failed: )r+   r)   r   makedirsr   dirnamer
   write_debugr   r   	Exception	traceback
format_excreport_warning)r   r&   r'   r0   r(   fntbr   r   r   store!   s   zCache.storec                 C   sV   t |d}|sd|id}}|rt|t|kr|d S | jd| d| d d S )Nr/   r0   z
2022.08.19z"Discarding old cache from version z (needs ))r   r   r
   r3   )r   r0   min_verversionr   r   r   	_validate0   s   
zCache._validateN)r<   c          
      C   s@  |dv sJ | j s|S | |||}tt z6t|dd&}| jd| d| d | t	
||W  d    W W  d    S 1 sHw   Y  W n7 ttfy   ztj|}W n tyu }	 z
t|	}W Y d }	~	nd }	~	ww | jd| d| d	 Y n	w W d    |S W d    |S 1 sw   Y  |S )
Nr-   zutf-8)encodingzLoading r   z from cachezCache retrieval from z	 failed (r;   )r+   r)   
contextlibsuppressOSErroropenr
   r3   r>   r,   load
ValueErrorKeyErrorr   r   getsizestrr7   )
r   r&   r'   r(   defaultr<   cache_fncachef	file_sizeoer   r   r   rD   8   s:   

z
Cache.loadc                    s   | j s| jd d S |   t fdddD s"td  d| jjd  dd	d
 tj rA| jjdd	d
 t	
  | jd d S )NzFCache is disabled (Did you combine --no-cache-dir and --rm-cache-dir?)c                 3   s    | ]}| v V  qd S r	   r   ).0termr   r   r   	<genexpr>S   s    zCache.remove.<locals>.<genexpr>)cachetmpzNot removing directory z& - this does not look like a cache dirzRemoving cache dir z .T)skip_eolr   )r+   r
   	to_screenr   anyr4   r   r   existsshutilrmtreer*   r   rP   r   removeM   s   
zCache.remover-   )r,   N)__name__
__module____qualname__r   r   r)   propertyr+   r:   r>   rD   rZ   r   r   r   r   r      s    

r   )r@   r,   r   r    rX   r5   urllib.parser"   utilsr   r   r   r   r=   r   r   r   r   r   r   <module>   s    