o
    i                     @   s\   d dl Z ddlmZ ddlmZmZmZ G dd deZG dd	 d	eZG d
d deZ	dS )    N   )PostProcessor   )	Namespacefilter_dictfunction_with_reprc                       sl   e Zd Z fddZedd Zedd Zedd Zd	d
 Z	e
dd Ze
dd ZeeedZ  ZS )MetadataParserPPc                    sN   t  | g | _|D ]}|^}}|| jv sJ | j|| g|R   qd S N)super__init___actionsActionsappend)self
downloaderactionsfactionargs	__class__ W/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/postprocessor/metadataparser.pyr      s   zMetadataParserPP.__init__c                 G   s,   || j vrt|d|| g|R   dS )zEach action can be:
                (Actions.INTERPRET, from, to) OR
                (Actions.REPLACE, field, search, replace)
        z is not a valid actionN)r   
ValueError)clsr   datar   r   r   validate_action   s   
z MetadataParserPP.validate_actionc                 C   s:   t d| rd|  dS ddlm} || }|r|| S )Nz[a-zA-Z_]+$z%(z)sr   )	YoutubeDL)rematchr   validate_outtmpl)tmplr   errr   r   r   field_to_template   s   
z"MetadataParserPP.field_to_templatec                 C   s   t d| rd|  dS t d| s| S d}d}t d| D ]}|t | ||  7 }|d|d	 d7 }| }q|t| k rN|t | |d
 7 }|S )z
        Converts a string like
           '%(title)s - %(artist)s'
        to a regex like
           '(?P<title>.+)\ \-\ (?P<artist>.+)'
        z\w+z(?s)(?P<z>.+)z	%\(\w+\)sr    z%\((\w+)\)sz(?P<r   N)	r   	fullmatchsearchfinditerescapestartgroupendlen)fmtlastposregexr   r   r   r   format_to_regex%   s   
z MetadataParserPP.format_to_regexc                 C   s   | j D ]}|| qg |fS r	   )r   )r   infor   r   r   r   run=   s   

zMetadataParserPP.runc                    s2    fdd}  t|S )Nc              	      s   j | }djd |}|d u r)d d d S t|  D ]\}}|| |< d| dd| q1d S )NzSearching for  in zCould not interpret z as zParsed z from z: )	_downloaderevaluate_outtmplwrite_debugpatternr&   	to_screenr   	groupdictitems)r1   data_to_parser   	attributevalueinpoutout_rer   templater   r   r   D   s   
z(MetadataParserPP.interpretter.<locals>.f)r#   r   compiler0   )r   r?   r@   r   r   r>   r   interpretterB   s   
zMetadataParserPP.interpretterc                    s"    fdd}t |S )Nc                    s   |   }|d u rd   d S t|ts(d  dt|j  d S dd  d |\|  < }|rQd  d|     d S d	d   d S )
NzVideo does not have a zCannot replace in field z since it is a zReplacing all r3   z with zChanged z to: zDid not find )	getr8   
isinstancestrreport_warningtype__name__r6   subn)r1   valnfieldreplacer&   	search_rer   r   r   r   U   s   

z$MetadataParserPP.replacer.<locals>.f)r   rC   )r   rO   r&   rP   r   r   rN   r   replacerS   s   
zMetadataParserPP.replacer)	INTERPRETREPLACE)rJ   
__module____qualname__r   classmethodr   staticmethodr#   r0   r2   r   rD   rR   r   r   __classcell__r   r   r   r   r      s    
	




r   c                       s(   e Zd Zedd Z fddZ  ZS )MetadataFromFieldPPc                 C   sD   t d|}|d u rtd|| jj|ddd|dfS )Nz#(?s)(?P<in>.*?)(?<!\\):(?P<out>.+)$zit should be FROM:TO, not inz\::r@   )r   r   r   r   rS   r*   rP   )r   r   r   r   r   r   	to_actionk   s   zMetadataFromFieldPP.to_actionc                    s    t  | fdd|D  d S )Nc                    s   g | ]}  |qS r   )r]   ).0r   r   r   r   
<listcomp>w   s    z0MetadataFromFieldPP.__init__.<locals>.<listcomp>)r
   r   )r   r   formatsr   r_   r   r   v   s    zMetadataFromFieldPP.__init__)rJ   rU   rV   rW   r]   r   rY   r   r   r   r   rZ   j   s    

rZ   c                       s   e Zd Z fddZ  ZS )MetadataFromTitlePPc                    s(   t  || jjd|fg | d d S )Ntitlezyt_dlp.postprocessor.MetadataFromTitlePP is deprecated and may be removed in a future version. Use yt_dlp.postprocessor.MetadataFromFieldPP instead)r
   r   r   rS   deprecation_warning)r   r   titleformatr   r   r   r   |   s   zMetadataFromTitlePP.__init__)rJ   rU   rV   r   rY   r   r   r   r   rb   {   s    rb   )
r   commonr   utilsr   r   r   r   rZ   rb   r   r   r   r   <module>   s    c