a
    h!                     @   st  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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 d dlmZ ddlmZmZmZmZ ddlmZmZmZmZmZmZ dZdZee j!Z"g d	Z#ej$G d
d dZ%G dd dej&j'Z(ej)dd Z*dd Z+dd Z,G dd dej&j-Z.dd Z/dd Z0dd Z1e%dddZ2dd  Z3e%dd!d"Z4dS )#    N)Path)ZipFile   )Indirectplugin_dirsall_plugins_loadedplugin_specs)get_executable_pathget_system_config_dirsget_user_config_dirsmerge_dicts
orderedSetwrite_stringZyt_dlp_pluginsZytdlp_plugins)COMPAT_PACKAGE_NAMEPACKAGE_NAME
PluginSpecdirectoriesload_all_pluginsload_pluginsregister_plugin_specc                   @   s.   e Zd ZU eed< eed< eed< eed< dS )r   module_namesuffixdestinationplugin_destinationN)__name__
__module____qualname__str__annotations__r    r   r   C/mnt/pikpak/tmp/myenv/lib/python3.9/site-packages/yt_dlp/plugins.pyr   6   s   
r   c                   @   s   e Zd ZdZdd ZdS )PluginLoaderz+Dummy loader for virtual namespace packagesc                 C   s   d S Nr   )selfmoduler   r   r    exec_moduleA   s    zPluginLoader.exec_moduleN)r   r   r   __doc__r%   r   r   r   r    r!   >   s   r!   c              
   C   s   zLt | 0}ttjdd | D W  d    W S 1 s@0    Y  W nJ ty^   Y n: ty } z"td|  d| d W Y d }~n
d }~0 0 dS )Nc                 s   s   | ]}t |jV  qd S r"   )r   parents).0filer   r   r    	<genexpr>I   s   zdirs_in_zip.<locals>.<genexpr>z!WARNING: Could not read zip file z: 
r   )	r   set	itertoolschainfrom_iterablenamelistFileNotFoundError	Exceptionr   )archiveZzip_er   r   r    dirs_in_zipE   s    
.,r5   c                  c   sx   dd } | g t dtdR ddiE d H  | t gt dtdR ddiE d H  dd	 tttjD E d H  d S )
Nc              	   w   sb   t tt|ddD ]J}|tkr qtt" ||   E d H  W d    q1 sR0    Y  qd S )NTZlazy)r   mapr   _BASE_PACKAGE_PATH
contextlibsuppressOSErroriterdir)containing_folderZ
root_pathsZ
config_dirr   r   r    _get_package_pathsS   s
    z0default_plugin_paths.<locals>._get_package_pathszyt-dlpr=   plugins zyt-dlp-pluginsc                 s   s   | ]}|t kr|V  qd S r"   )r8   )r(   pathr   r   r    r*   k       z'default_plugin_paths.<locals>.<genexpr>)r   r
   r	   r7   r   sysrA   )r>   r   r   r    default_plugin_pathsR   s$    	rD   c                 c   s0   t | }| std| | E d H  d S )NzInvalid plugin directory: )r   is_dir
ValueErrorr<   )	candidateZcandidate_pathr   r   r    candidate_plugin_pathsn   s    rH   c                   @   s2   e Zd ZdZdd Zdd ZdddZd	d
 ZdS )PluginFinderz
    This class provides one or multiple namespace packages.
    It searches in sys.path and yt-dlp config folders for
    the existing subdirectories from which the modules can be imported
    c                 G   s&   i | _ ttjdd |D | _d S )Nc                 s   s$   | ]}t |d dd V  qdS ).c                 S   s   d | |fS )NrJ   )join)abr   r   r    <lambda>   rB   z1PluginFinder.__init__.<locals>.<genexpr>.<lambda>N)r-   
accumulatesplit)r(   namer   r   r    r*      s   z(PluginFinder.__init__.<locals>.<genexpr>)Z_zip_content_cacher,   r-   r.   r/   packages)r#   rR   r   r   r    __init__|   s    zPluginFinder.__init__c                 c   s   t jdd tjD }t|d }t|ddD ]|}|| }z8| rP|V  n$|j	dv rt|
 rt|t|v rt|V  W q2 ty } ztd|j d W Y d }~q2d }~0 0 q2d S )	Nc                 s   s$   | ]}|d krt  nt|V  qdS )defaultN)rD   rH   )r(   rG   r   r   r    r*      s   z0PluginFinder.search_locations.<locals>.<genexpr>rJ   Tr6   )z.zipz.eggz.whlz-Permission error while accessing modules in "z"
)r-   r.   r/   r   valuer   rP   r   rE   r   is_filer5   PermissionErrorr   filename)r#   fullnameZcandidate_locationspartsrA   rG   r4   r   r   r    search_locations   s    
zPluginFinder.search_locationsNc                 C   sL   || j vrd S ttt| |}|s.t|tjj|t	 dd}||_
|S )NT)
is_package)rR   listr7   r   r[   ModuleNotFoundError	importlib	machinery
ModuleSpecr!   submodule_search_locations)r#   rY   rA   targetr[   specr   r   r    	find_spec   s    
zPluginFinder.find_specc                 C   s*   t   | jD ]}|tjv rtj|= qd S r"   )r5   cache_clearrR   rC   modules)r#   packager   r   r    invalidate_caches   s    

zPluginFinder.invalidate_caches)NN)r   r   r   r&   rS   r[   re   ri   r   r   r   r    rI   u   s
   
rI   c                  C   sV   t t8 tjt } r4t| jW  d    S W d    n1 sH0    Y  g S r"   )	r9   r:   r^   r_   utilre   r   r]   rb   )rd   r   r   r    r      s    6r   c                 c   sb   t  d|  }tt6 t|}tj|j| ddE d H  W d    n1 sT0    Y  d S )NrJ   )rA   prefix)	r   r9   r:   r^   r_   import_modulepkgutiliter_modules__path__)Z
subpackagerY   pkgr   r   r    rn      s    
rn   c                    s   t   fddS )Nc                    sV   t | oT| joT| joT| jd oT| jt d| jgv oTt| dd d u S )N___all__ZPLUGIN_NAME)inspectisclassr   endswithr   
startswithgetattr)objr$   r   r   r   r    rN      s    


z%get_regular_classes.<locals>.<lambda>)rs   
getmembersry   r   ry   r    get_regular_classes   s    r{   plugin_specc           
      C   s  | j | j }}i }tjds$tjs(|S t|D ]\}}}tdd |	dD rTq0zRt
jdk rxt|tjrx||}n,||}tj|}|t
j|< |j| W n2 ty   td|dtjdd	  Y q0Y n0 |t||| q0d
tjv rxttb tj |t!t" t#|d}tj|}	|	t
j|j$< |j|	 |t|	|j$| W d    n1 sn0    Y  || j%_t&|| j'j| j'_|S )NZYTDLP_NO_PLUGINSc                 s   s   | ]}| d V  qdS )rq   N)rv   )r(   xr   r   r    r*      rB   zload_plugins.<locals>.<genexpr>rJ   )   
   zError while importing module r+   )limitrT   z__init__.py)(r   r   osenvirongetr   rU   rn   anyrP   rC   version_info
isinstance	zipimportzipimporterload_modulere   r_   rj   module_from_specrg   loaderr%   r2   r   	traceback
format_excupdater{   r9   r:   r1   spec_from_file_locationr   r	   r   rQ   r   r   r   )
r}   rQ   r   Zregular_classesfinderr   rq   r$   rd   r?   r   r   r    r      sB    


4r   c                  C   s"   t j D ]} t|  q
dt_d S )NT)r   rU   valuesr   r   r|   r   r   r    r      s    
r   c                 C   s:   | j tjvr6| tj| j < tjdtt d| j   d S )Nr   rJ   )r   r   rU   rC   	meta_pathinsertrI   r   r|   r   r   r    r      s    r   )5r9   Zdataclasses	functoolsr_   importlib.abcZimportlib.machineryimportlib.utilrs   r-   r   rm   rC   r   r   pathlibr   zipfiler   globalsr   r   r   r   utilsr	   r
   r   r   r   r   r   r   __file__parentr8   rr   Z	dataclassr   abcLoaderr!   cacher5   rD   rH   MetaPathFinderrI   r   rn   r{   r   r   r   r   r   r   r    <module>   sF    	

41