o
    i                      @   sl  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 Z/dd Z0de$fddZ1dd  Z2de$fd!d"Z3d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_stringyt_dlp_plugins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!   B/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/plugins.pyr   5   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_module@   s   zPluginLoader.exec_moduleN)r   r   r   __doc__r'   r!   r!   r!   r"   r#   =   s    r#   c              
   C   s   z't | }ttjdd | D W  d    W S 1 s w   Y  W dS  ty1   Y dS  tyN } ztd|  d| d W Y d }~dS d }~ww )Nc                 s   s    | ]}t |jV  qd S r$   )r   parents).0filer!   r!   r"   	<genexpr>H   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   )archivezip_er!   r!   r"   dirs_in_zipD   s   
$ r8   c                  c   sz    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   sd    t tt|ddD ]%}|tkrq
tt ||   E d H  W d    n1 s*w   Y  q
d S )NTlazy)r   mapr   _BASE_PACKAGE_PATH
contextlibsuppressOSErroriterdir)containing_folder
root_paths
config_dirr!   r!   r"   _get_package_pathsR   s   z0default_plugin_paths.<locals>._get_package_pathszyt-dlprA   plugins zyt-dlp-pluginsc                 s   s    | ]	}|t kr|V  qd S r$   )r<   )r*   pathr!   r!   r"   r,   j   s    z'default_plugin_paths.<locals>.<genexpr>)r   r
   r	   r;   r   sysrG   )rD   r!   r!   r"   default_plugin_pathsQ   s&   	 rI   c                 c   s2    t | }| std| | E d H  d S )NzInvalid plugin directory: )r   is_dir
ValueErrorr@   )	candidatecandidate_pathr!   r!   r"   candidate_plugin_pathsm   s
   rN   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 )NrP   )join)abr!   r!   r"   <lambda>   s    z1PluginFinder.__init__.<locals>.<genexpr>.<lambda>N)r/   
accumulatesplit)r*   namer!   r!   r"   r,   ~   
    
z(PluginFinder.__init__.<locals>.<genexpr>)_zip_content_cacher.   r/   r0   r1   packages)r%   rZ   r!   r!   r"   __init__{   s   
zPluginFinder.__init__c                 c   s    t jdd tjD }t|d }t|ddD ]=}|| }z| r)|V  n|j	dv r;|
 r;|t|v r;|V  W q tyW } ztd|j d W Y d }~qd }~ww d S )	Nc                 s   s&    | ]}|d krt  nt|V  qdS )defaultN)rI   rN   )r*   rL   r!   r!   r"   r,      rX   z0PluginFinder.search_locations.<locals>.<genexpr>rP   Tr9   )z.zipz.eggz.whlz-Permission error while accessing modules in "z"
)r/   r0   r1   r   valuer   rV   r   rJ   r   is_filer8   PermissionErrorr   filename)r%   fullnamecandidate_locationspartsrG   rL   r7   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)rZ   listr;   r   rd   ModuleNotFoundError	importlib	machinery
ModuleSpecr#   submodule_search_locations)r%   ra   rG   targetrd   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$   )r8   cache_clearrZ   rH   modules)r%   packager!   r!   r"   invalidate_caches   s   

zPluginFinder.invalidate_caches)NN)r   r   r   r(   r[   rd   rn   rr   r!   r!   r!   r"   rO   t   s    
rO   c                  C   sX   t t tjt } rt| jW  d    S W d    g S 1 s%w   Y  g S r$   )	r=   r>   rg   rh   utilrn   r   rf   rk   )rm   r!   r!   r"   r      s   
r   c                 c   sf    t  d|  }tt t|}tj|j| ddE d H  W d    d S 1 s,w   Y  d S )NrP   )rG   prefix)	r   r=   r>   rg   rh   import_modulepkgutiliter_modules__path__)
subpackagera   pkgr!   r!   r"   rw      s   
"rw   c                    s   t   fddS )Nc                    sV   t | o*| jo*| jo*| jd o*| jt d| jgv o*t| dd d u S )N___all__PLUGIN_NAME)inspectisclassr   endswithr   
startswithgetattr)objr&   r   r   r!   r"   rT      s   


z%get_regular_classes.<locals>.<lambda>)r~   
getmembersr   r!   r   r"   get_regular_classes   s   r   plugin_specc           
      C   sl  | j | j}}i }tjdstjs|S t|D ]K\}}}tdd |	dD r*qz|
|}tj|}|tj|< |j| W n tyY   td|dtjdd  Y qw |t||| qd	tjv rtt1 tj|tt t|d
}tj|}	|	tj|j< |j|	 |t|	|j| W d    n1 sw   Y  || j _t!|| j"j| j"_|S )NYTDLP_NO_PLUGINSc                 s   s    | ]}| d V  qdS )r{   N)r   )r*   xr!   r!   r"   r,      s    zload_plugins.<locals>.<genexpr>rP   zError while importing module r-   )limitr\   z__init__.py)#r   r   osenvirongetr   r]   rw   anyrV   rn   rh   rs   module_from_specrH   rp   loaderr'   r4   r   	traceback
format_excupdater   r=   r>   r3   spec_from_file_locationr   r	   r   rW   r   r   r   )
r   rW   r   regular_classesfinderr   r{   rm   r&   rE   r!   r!   r"   r      sB   


r   c                  C   s"   t j D ]} t|  qdt_d S )NT)r   r]   valuesr   r   r   r!   r!   r"   r      s   

r   c                 C   s>   | j tjvr| tj| j < tjdtt d| j   d S d S )Nr   rP   )r   r   r]   rH   	meta_pathinsertrO   r   r   r!   r!   r"   r      s   "r   )4r=   dataclasses	functoolsrh   importlib.abcimportlib.machineryimportlib.utilr~   r/   r   rv   rH   r   pathlibr   zipfiler   globalsr   r   r   r   utilsr	   r
   r   r   r   r   r   r   __file__parentr<   r|   	dataclassr   abcLoaderr#   cacher8   rI   rN   MetaPathFinderrO   r   rw   r   r   r   r   r!   r!   r!   r"   <module>   sF     	

4+