U
    g                      @   s   d dl Z d dlZd dlZddlmZ ddlmZmZ ddlm	Z	m
Z
mZmZmZ G dd deZG dd	 d	ed
ZG dd de	ZdS )    N   )Request)	HTTPErrornetwork_exceptions)PostProcessingErrorRetryManager_configuration_argsdeprecation_warningencodeFilenamec                   @   s    e Zd Zedd Zdd ZdS )PostProcessorMetaClassc                    s   t   fdd}|S )Nc                    sP   |  |}| ddi|  | |f||}|d k	r<|\}}| ddi| |S )Nstatusstartedfinished)_copy_infodict_hook_progress)selfinfoargskwargsZ	info_copyret_func ?/tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/postprocessor/common.pyrun   s    
z/PostProcessorMetaClass.run_wrapper.<locals>.run	functoolswraps)r   r   r   r   r   run_wrapper   s    z"PostProcessorMetaClass.run_wrapperc                 C   s*   d|kr|  |d |d< t| |||S )Nr   )r   type__new__)clsnamebasesattrsr   r   r   r!      s    zPostProcessorMetaClass.__new__N)__name__
__module____qualname__staticmethodr   r!   r   r   r   r   r      s   
r   c                   @   s   e Zd ZdZdZd2ddZedd Zd3dd	Zd
d Z	dd Z
dd Zdd Zdd Zdd Zd4ddZdd Zdd ZedddddddZdd  Zd5d"d#Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/d0d1ZdS )6PostProcessorak  Post Processor class.

    PostProcessor objects can be added to downloaders with their
    add_post_processor() method. When the downloader has finished a
    successful download, it will take its internal chain of PostProcessors
    and start calling the run() method on each one of them, first with
    an initial argument and then with the returned value of the previous
    PostProcessor.

    PostProcessor objects follow a "mutual registration" process similar
    to InfoExtractor objects.

    Optionally PostProcessor can use a list of additional command-line arguments
    with self._configuration_args.
    Nc                 C   s*   g | _ | | j | | |  | _d S N)_progress_hooksadd_progress_hookreport_progressset_downloaderpp_keyPP_NAME)r   
downloaderr   r   r   __init__7   s    
zPostProcessor.__init__c                 C   s2   | j d d }|d d  dkr.|dd  S |S )N   Zffmpeg)r&   lower)r"   r#   r   r   r   r0   =   s    zPostProcessor.pp_keyTc                 O   s<   | j r8|rd| j dnd}| j j| | f||S d S )N[z]  )_downloaderr1   	to_screen)r   textprefixr   r   tagr   r   r   r:   B   s    zPostProcessor.to_screenc                 O   s   | j r| j j|f||S d S r+   )r9   report_warningr   r;   r   r   r   r   r   r>   G   s    zPostProcessor.report_warningc                 C   s   t | jdt}||ddS )Nr	      
stacklevel)getattrr9   r	   )r   msgwarnr   r   r   r	   K   s    z!PostProcessor.deprecation_warningc                 C   s   | j r| j |S t|ddS )Nr@   rA   )r9   deprecated_featurer	   )r   rD   r   r   r   rF   O   s    z PostProcessor.deprecated_featurec                 O   s(   |  d | jr$| jj|f||S d S )Nzq"yt_dlp.postprocessor.PostProcessor.report_error" is deprecated. raise "yt_dlp.utils.PostProcessingError" instead)r	   r9   report_errorr?   r   r   r   rG   T   s    
zPostProcessor.report_errorc                 O   s   | j r| j j|f||S d S r+   )r9   write_debugr?   r   r   r   rH   Z   s    zPostProcessor.write_debugc                 O   s6   | j r| j j||S ttd |D ]}t| q"d S r+   )r9   _delete_downloaded_filessetfilterosremove)r   Zfiles_to_deleter   filenamer   r   r   rI   ^   s    z&PostProcessor._delete_downloaded_filesc                 O   s"   | j r| j jj||f||S |S r+   )r9   paramsget)r   r#   defaultr   r   r   r   r   	get_paramd   s    zPostProcessor.get_paramc                 C   s&   || _ t|dg D ]}| | qdS )z Sets the downloader for this PP.Z_postprocessor_hooksN)r9   rC   r-   )r   r2   phr   r   r   r/   i   s    zPostProcessor.set_downloaderc                 C   s   t | jdt|S )Nr   )rC   r9   dict)r   	info_dictr   r   r   r   o   s    zPostProcessor._copy_infodict)videoaudioimages	simulatedc                    s   | ||d  fdd}|S )N)rV   rW   rX   c                    s   t   fdd}|S )Nc                    sv   s |  ds|  dr g |fS |ddkr2dn|ddkrDdnd} | rZ| |S | d	|  g |fS d S )
NZsimulateZskip_downloadZvcodecnonerV   ZacodecrW   rX   z	Skipping )rR   rP   r:   )r   r   Zformat_type)allowedr   rY   r   r   wrapperw   s    
z>PostProcessor._restrict_to.<locals>.decorator.<locals>.wrapperr   )r   r\   r[   rY   r   r   	decoratorv   s    z-PostProcessor._restrict_to.<locals>.decoratorr   )rV   rW   rX   rY   r^   r   r]   r   _restrict_tor   s    zPostProcessor._restrict_toc                 C   s   g |fS )a  Run the PostProcessor.

        The "information" argument is a dictionary like the ones
        composed by InfoExtractors. The only difference is that this
        one has an extra field called "filepath" that points to the
        downloaded file.

        This method returns a tuple, the first element is a list of the files
        that can be deleted, and the second of which is the updated
        information.

        In addition, this method may raise a PostProcessingError
        exception if post processing fails.
        r   )r   Zinformationr   r   r   r      s    zPostProcessor.runCannot update utime of filec                 C   s<   zt t|||f W n tk
r6   | | Y nX d S r+   )rL   utimer
   	Exceptionr>   )r   pathatimemtimeZerrnoter   r   r   	try_utime   s    zPostProcessor.try_utimec                 O   s   t |  | d|f||S )NZpostprocessor_args)r   r0   rR   )r   Zexer   r   r   r   r   r      s      z!PostProcessor._configuration_argsc                 C   s6   | j s
d S |||  d | j D ]}|| q$d S )N)rU   Zpostprocessor)r,   updater0   )r   r   rU   rS   r   r   r   r      s    
zPostProcessor._hook_progressc                 C   s   | j | d S r+   )r,   append)r   rS   r   r   r   r-      s    zPostProcessor.add_progress_hookc                 C   s   d| |d< | j sd S | }|d |d |d}| di }|d}|rj| j j| j ||dd | j | j |d	pd
| d S )Nz%(postprocessor)s %(status)sZ_default_templaterU   )r   progressprogress_templatepostprocessF)quietzpostprocess-titlez%yt-dlp %(progress._default_template)s)r9   copypoprR   rP   r:   Zevaluate_outtmplZto_console_title)r   sZprogress_dictrj   tmplr   r   r   r.      s"    

 zPostProcessor.report_progressc              
   C   s,   t j|||| j| j| di dd d S )NZretry_sleep_functionsZ	extractor)r   rE   Z
sleep_func)r   Zreport_retryr:   r>   rR   rP   )r   errcountretriesr   r   r   _retry_download   s    zPostProcessor._retry_download)i  )expected_http_errorsc                C   s   |  | j d|  t| dd| jD ]|}z| jt|}W q* tk
r } zDt	|t
rt|j|krtW Y * d S td| j d| |_W Y q*W 5 d }~X Y q*X q*t| |jdpdS )Nz query: Zextractor_retries   zUnable to communicate with z API: charsetzutf-8)rH   r1   r   rR   rt   r9   urlopenr   r   
isinstancer   r   r   errorjsonloadsreaddecodeheaders)r   urlru   retryZrsper   r   r   _download_json   s    zPostProcessor._download_json)N)T)N)r`   )r&   r'   r(   __doc__r9   r3   classmethodr0   r:   r>   r	   rF   rG   rH   rI   rR   r/   r   r)   r_   r   rf   r   r   r-   r.   rt   r   r   r   r   r   r*   $   s2   





r*   )	metaclassc                   @   s   e Zd ZdS )AudioConversionErrorN)r&   r'   r(   r   r   r   r   r      s   r   )r   r{   rL   Z
networkingr   Znetworking.exceptionsr   r   utilsr   r   r   r	   r
   r    r   r*   r   r   r   r   r   <module>   s   	 3