U
    g#                     @   s2  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 ddlmZmZmZmZmZmZ dZdZG d	d
 d
ejjZejdd ZG dd dejj Z!dd Z"dd Z#dd Z$dd Z%e	j&'d e!e de d ddddgZ(dS )    N)Path)ZipFile   )	functools)Configget_executable_pathget_system_config_dirsget_user_config_dirs
orderedSetwrite_stringZyt_dlp_pluginsZytdlp_pluginsc                   @   s   e Zd ZdZdd ZdS )PluginLoaderz+Dummy loader for virtual namespace packagesc                 C   s   d S N )selfmoduler   r   2/tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/plugins.pyexec_module!   s    zPluginLoader.exec_moduleN)__name__
__module____qualname____doc__r   r   r   r   r   r      s   r   c              
   C   s   z@t | .}ttjdd | D W  5 Q R  W S Q R X W nJ tk
rT   Y n8 tk
r } ztd|  d| d W 5 d }~X Y nX t S )Nc                 s   s   | ]}t |jV  qd S r   )r   parents).0filer   r   r   	<genexpr>)   s    zdirs_in_zip.<locals>.<genexpr>z!WARNING: Could not read zip file z: 
)	r   set	itertoolschainfrom_iterablenamelistFileNotFoundError	Exceptionr   )archiveZzip_er   r   r   dirs_in_zip%   s    
"(r%   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 )Nr'   )join)abr   r   r   <lambda><       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   r0   r   r   r   __init__9   s    zPluginFinder.__init__c                 c   sN  g }dddd}| |tdtdddi | |t ftdtdddi | tttj t	t
 |ttj W 5 Q R X tjr| |tjddi t|d	 }t|d
dD ]}|| }z>| r|V  n*|jdkr| r|t|kr|V  W q tk
rF } ztd|j d W 5 d }~X Y qX qd S )Nplugins)containing_folderc              
   w   sD   t tt|ddD ],}tt ||   E d H  W 5 Q R X qd S )NTZlazy)r
   mapr   
contextlibsuppressOSErroriterdir)r3   Z
root_pathsZ
config_dirr   r   r   _get_package_pathsB   s    z9PluginFinder.search_locations.<locals>._get_package_pathszyt-dlpr3    zyt-dlp-pluginsr'   Tr4   )z.zipz.eggz.whlz-Permission error while accessing modules in "z"
)extendr	   r   r   r5   r   syspathr6   r7   
ValueErrorremove__file__parentr   Z_plugin_dirsr.   r
   is_dirsuffixis_filer%   PermissionErrorr   filename)r   fullnameZcandidate_locationsr:   partsr>   	candidater$   r   r   r   search_locations?   sH    
zPluginFinder.search_locationsNc                 C   sH   || j krd S ttt| |}|s*d S tjj|t dd}||_	|S )NT)
is_package)
r0   listr5   strrK   	importlib	machinery
ModuleSpecr   submodule_search_locations)r   rH   r>   targetrK   specr   r   r   	find_specj   s    
zPluginFinder.find_specc                 C   s*   t   | jD ]}|tjkrtj|= qd S r   )r%   cache_clearr0   r=   modules)r   packager   r   r   invalidate_cachesv   s    

zPluginFinder.invalidate_caches)NN)r   r   r   r   r1   rK   rU   rY   r   r   r   r   r&   2   s
   +
r&   c                  C   s   t jt} | r| jS g S r   )rO   utilrU   PACKAGE_NAMErR   )rT   r   r   r   directories}   s    r\   c              	   c   sN   t  d|  }tt, t|}tj|j| ddE d H  W 5 Q R X d S )Nr'   )r>   prefix)	r[   r6   r7   ModuleNotFoundErrorrO   import_modulepkgutiliter_modules__path__)Z
subpackagerH   pkgr   r   r   ra      s    
ra   c                    s   t   fddS )Nc                    sF   t | oD| joD| joD| jd oD| jt d| jgkS )N___all__)inspectisclassr   endswithr   
startswithgetattr)objr   module_namerD   r   r   r+      s    


zload_module.<locals>.<lambda>)rf   
getmembersrl   r   rl   r   load_module   s    ro   c           	      C   sH  i }t jdr|S t| D ]\}}}tdd |dD r@qzRtjdk rdt|t	j
rd||}n,||}tj|}|tj|< |j| W n4 tk
r   td|dtjdd	  Y qY nX |t||| qttX tj| tt t| d
}tj|}|tj|j< |j| |t||j| W 5 Q R X |S )NZYTDLP_NO_PLUGINSc                 s   s   | ]}| d V  qdS )rd   N)ri   )r   xr   r   r   r      s     zload_plugins.<locals>.<genexpr>r'   )   
   zError while importing module r   )limitz__init__.py) osenvirongetra   anyr.   r=   version_info
isinstance	zipimportzipimporterro   rU   rO   rZ   module_from_specrW   loaderr   r"   r   	traceback
format_excupdater6   r7   r!   spec_from_file_locationr   r   COMPAT_PACKAGE_NAMEr/   )	r/   rD   classesfinderrm   rd   r   rT   r2   r   r   r   load_plugins   s6    


 r   z
.extractorz.postprocessorr[   r   ))r6   rO   importlib.abcZimportlib.machineryimportlib.utilrf   r   ru   r`   r=   r   r{   pathlibr   zipfiler   compatr   utilsr   r   r   r	   r
   r   r[   r   abcLoaderr   cacher%   MetaPathFinderr&   r\   ra   ro   r   	meta_pathinsertre   r   r   r   r   <module>   s6    	
K	& 