
    yfL1                         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mZmZmZmZmZmZ edddedddZd Zd
d	Zy)    N   )IDENTITY
NO_DEFAULTLazyListdeprecation_warningis_iterable_liketry_callvariadicTF)defaultexpected_typeget_all	casesenseis_user_inputtraverse_stringc          	      ,   |t         urt        d       d t        t              rfdnfdfdd fdfdt	        |d	      D ]"  \  }}	 | |	|t        |      k(  d
      }
|
 |
c S  t         u rdS S )a4  
    Safely traverse nested `dict`s and `Iterable`s

    >>> obj = [{}, {"key": "value"}]
    >>> traverse_obj(obj, (1, "key"))
    'value'

    Each of the provided `paths` is tested and the first producing a valid result will be returned.
    The next path will also be tested if the path branched but no results could be found.
    Supported values for traversal are `Mapping`, `Iterable`, `re.Match`,
    `xml.etree.ElementTree` (xpath) and `http.cookies.Morsel`.
    Unhelpful values (`{}`, `None`) are treated as the absence of a value and discarded.

    The paths will be wrapped in `variadic`, so that `'key'` is conveniently the same as `('key', )`.

    The keys in the path can be one of:
        - `None`:           Return the current object.
        - `set`:            Requires the only item in the set to be a type or function,
                            like `{type}`/`{type, type, ...}/`{func}`. If a `type`, return only
                            values of this type. If a function, returns `func(obj)`.
        - `str`/`int`:      Return `obj[key]`. For `re.Match`, return `obj.group(key)`.
        - `slice`:          Branch out and return all values in `obj[key]`.
        - `Ellipsis`:       Branch out and return a list of all values.
        - `tuple`/`list`:   Branch out and return a list of all matching values.
                            Read as: `[traverse_obj(obj, branch) for branch in branches]`.
        - `function`:       Branch out and return values filtered by the function.
                            Read as: `[value for key, value in obj if function(key, value)]`.
                            For `Iterable`s, `key` is the index of the value.
                            For `re.Match`es, `key` is the group number (0 = full match)
                            as well as additionally any group names, if given.
        - `dict`:           Transform the current object and return a matching dict.
                            Read as: `{key: traverse_obj(obj, path) for key, path in dct.items()}`.
        - `any`-builtin:    Take the first matching object and return it, resetting branching.
        - `all`-builtin:    Take all matching objects and return them as a list, resetting branching.

        `tuple`, `list`, and `dict` all support nested paths and branches.

    @params paths           Paths which to traverse by.
    @param default          Value to return if the paths do not match.
                            If the last key in the path is a `dict`, it will apply to each value inside
                            the dict instead, depth first. Try to avoid if using nested `dict` keys.
    @param expected_type    If a `type`, only accept final values of this type.
                            If any other callable, try to call the function on each result.
                            If the last key in the path is a `dict`, it will apply to each value inside
                            the dict instead, recursively. This does respect branching paths.
    @param get_all          If `False`, return the first matching result, otherwise all matching ones.
    @param casesense        If `False`, consider string dictionary keys as case insensitive.

    `traverse_string` is only meant to be used by YoutubeDL.prepare_outtmpl and is not part of the API

    @param traverse_string  Whether to traverse into objects as strings.
                            If `True`, any non-compatible object will first be
                            converted into a string and then traversed into.
                            The return value of that path will be a string instead,
                            not respecting any further branching.


    @returns                The result of the object traversal.
                            If successful, `get_all=True`, and the path branches at least once,
                            then a list of results is returned instead.
                            If no `default` is given and the last path branches, a `list` of results
                            is always returned. If a path ends on a `dict` that result will always be a `dict`.
    z=The is_user_input parameter is deprecated and no longer worksc                 F    t        | t              r| j                         S | S N)
isinstancestrcasefold)ks    8/usr/lib/python3/dist-packages/yt_dlp/utils/traversal.py<lambda>ztraverse_obj.<locals>.<lambda>Y   s    As);      c                 $    t        |       r| S d S r   )r   valr   s    r   r   ztraverse_obj.<locals>.<lambda>\   s    z#}'E 4 r   c                 0    t        xs t        | f      S )Nargs)r	   r   r   s    r   r   ztraverse_obj.<locals>.<lambda>^   s    )B(# P r   c                 ,    d}d }(r& du st               st         t              rd}d}n }nt         t              rpt	        t                     }t               dkD  st        |t              r.t        d  D              sJ t        t                     r<}n8t        |f      }n(t         t        t        f      r.d}t        j                  j                  fd D              }n du rd}t        t        j                   j"                        r"t%        j&                  j(                  	      t        t*        j,                  j.                        rj1                         }nbt3              s.t        t4        j6                  j8                  j:                        r}n%t        t<        j>                        rjA                         }nrd}tC              }nd}nt               rtd}t        t        j                   j"                        r"t%        j&                  j(                  	      t        t*        j,                  j.                        rjE                         }nt3              s.t        t4        j6                  j8                  j:                        rtG              }nt        t<        j>                        r\t        j                  tG        jI                         gjA                               jK                         jE                               }nrd}tG        tC                    }nd} fd
|D        }|swdjM                  |      }ndt         t$              rEfd jE                         D        }|D ci c]  \  }}|tN        ur|||n c}}xs d }nt        t*        j,                  j.                        rt        t        j                   j"                        r"t%        j&                  j(                  	      st        jP                   f      rt        jR                   f      n#t	         fdjE                         D        d       }nMt        t<        j>                        rt         tT              sr5tW        jX                  tZ              5  jI                         }d d d        nt         tB              rt	         fdjK                         jE                         D        d       }nt         tT        t        f      rt3        t*        j,                  j\                  t4        j6                  j8                  j:                  f      r9t         t              }tW        jX                  tZ              5      }d d d        nrtW        jX                  tZ              5  tC                  }d d d        nt        t4        j6                  j8                  j:                        rt         tB              r j_                  d      \  }	}
ja                  d      sjc                  d      s }	d |	ja                  d      rd|	 }	n|	r|	ja                  d      sd|	 }	fd}|	r%t        te        |jg                  |	                  }n |      }||r|fS |ffS c c}}w # 1 sw Y   xY w# 1 sw Y   %xY w# 1 sw Y   1xY w)NF.T r   c              3   <   K   | ]  }t        |t                y wr   )r   type.0items     r   	<genexpr>z2traverse_obj.<locals>.apply_key.<locals>.<genexpr>o   s     Bd:dD1Bs   r   c              3   8   K   | ]  } |      d      yw)r   Nr"   )r&   branch
apply_pathis_lastobjs     r   r(   z2traverse_obj.<locals>.apply_key.<locals>.<genexpr>w   s&      3G8>
3033Gs   )keyvaluec              3   H   K   | ]  \  }}t        ||f       s|  yw)r   N)r	   )r&   r   vr.   s      r   r(   z2traverse_obj.<locals>.apply_key.<locals>.<genexpr>   s"     KDAqAq60JaKs   "" c              3   >   K   | ]  \  }}| |d       f  yw)FNr"   )r&   r   r1   _traverse_objr,   r-   s      r   r(   z2traverse_obj.<locals>.apply_key.<locals>.<genexpr>   s%     [tq!M#q%AB[s   c              3   @   K   | ]  \  }} |      k(  s|  y wr   r"   r&   r   r1   r   r.   s      r   r(   z2traverse_obj.<locals>.apply_key.<locals>.<genexpr>   s      L$!Q!9KAL   c              3   @   K   | ]  \  }} |      k(  s|  y wr   r"   r6   s      r   r(   z2traverse_obj.<locals>.apply_key.<locals>.<genexpr>   s"     [TQQRWZHZq[r7   /@z().z./c                     | S dk(  r| j                   S j                  d      r%t        | j                   j                  dd  f      S dk(  r| j                  S t        d      )Nr:   r   r   ztext()z#apply_specials is missing case for )attrib
startswithr	   gettextSyntaxError)elementspecials    r   apply_specialsz7traverse_obj.<locals>.apply_key.<locals>.apply_specials   sp    ?"Nc>">>)%%c*#GNN$6$6gabk^LLh&"<<'!$G{"STTr   )4callabler   slicesetnextiterlenr$   alltupler	   list	itertoolschainfrom_iterablehttpcookiesMorseldictr.   r/   collectionsabcMappingvaluesr   xmletreeElementTreeElementreMatchgroupsr   items	enumerategroup	groupdictjoinr   __contains__r?   int
contextlibsuppress
IndexErrorSequence
rpartitionr>   endswithmapiterfind)r.   r-   r,   	branchingresultr'   iter_objr   r1   xpath_rD   rC   r4   r+   r   r   r   r   s   ```         @r   	apply_keyztraverse_obj.<locals>.apply_key`   sh   	;?czXc]je.D 	[FS!S	?D3x!|z$5BcBBBBc5:. F!$cV4dE]+I__22 3GBE3G GF CZI#t||2233CGG399=#{667!#&*S#)):O:O:W:W*XC* !	Sc]I#t||2233CGG399=#{66799;!#&*S#)):O:O:W:W*X$S>C*$??syy{:SZZ\:;MMO))+- !!	$SX.KHKFT"[syy{[H?G7;q!=G:$= 172   
 [__445#t||2233CGG399=8AXcN^N^fiekElhswwcV4L#))+LdS  RXX&#s#y((4 , YYs^F, , C%[S]]_-B-B-D[]abc5\*koo&>&>		@U@U@]@]%^_&sE2	((4 & XF& & ((4 + Xc]F+ + SYY22::;
3PS@T #s 3E1g%%c*73C3CD3I $E7u//5UG	U c.#,,u2EFG',I&<<F9<<q, ,& &+ +s*   .[,[2[>.\
2[;>\
\c              3   ~   K   t        |       }t        |t              }|t        u ry |D ]
  }d|f |} d|f y w)NFT)rI   rH   r   )iterableiteratorprevr'   s       r   	lazy_lastztraverse_obj.<locals>.lazy_last   sP     >Hj): 	D+D	 Djs   ;=c           	         | f}d}d } t        |t        t        t        t        f            D ]  \  }}s t        |t              r|j                         }|t        t        fv r.d}d |D        }|t        u rt        |d       f}nt        |      f}dt        |      r%t        j                  |      j                  d d        g }|D ]%  }	 ||	|      \  }
}||
z  }|j                  |       ' t         j"                  j%                  |      } |r't        |t        t        t&        f      st)        |      }||t        |t              fS )NFc              3   .   K   | ]  }|d i fvs
|  y wr   r"   )r&   r-   s     r   r(   z3traverse_obj.<locals>.apply_path.<locals>.<genexpr>   s      ND":8M N   )r
   r   bytesrT   rG   r   r   anyrK   rH   rM   rE   inspect	signaturebindappendrN   rO   rP   rL   rm   )	start_objpath	test_typeobjshas_branchedr.   lastfiltered_objsnew_objsr-   ro   resultsrt   r   ry   	type_tests               r   r+   z traverse_obj.<locals>.apply_path   s>   |"8D3tS2I#JK 	;ID#C!5llnsCj $ N N#: 57D /1D%c]!!#&++D$7H )%.sC%>"	7	)()
 ??00:D/	;2 ZdD%-@Ay$'D\:c4#888r   c                      | ||      \  }}}t        d |D              }	r#|r!|r|j                         S |rt        u rg S S y |r|d   S |r|ri S d S )Nc              3   .   K   | ]  }|d i fvs
|  y wr   r"   r%   s     r   r(   z6traverse_obj.<locals>._traverse_obj.<locals>.<genexpr>  s     NDtD":7M4Nr|   r   )r   exhaustr   )
r-   r   allow_emptyr   r   r   is_dictr+   r   r   s
          r   r4   z#traverse_obj.<locals>._traverse_obj  so    )3Cy)I&wNGNN|(($
2r??$wqzQ"QTQr   r   TN)r   r   r   r$   ra   rJ   )r-   r   r   r   r   r   r   pathsindexr   rp   r4   rt   r+   r   ry   r   s    ```` `    @@@@@@r   traverse_objr      s    D J&[\BH-&O	P	z= z=x
!9F
R !* tsD%3u:*=tDM
 j(45g5r   c                 6    t        | gd |D        i |ddiS )Nc              3   :   K   | ]  }d gt        |        yw).N)r
   )r&   keyss     r   r(   zget_first.<locals>.<genexpr>   s     H$5htn5Hs   r   F)r   )r-   r   kwargss      r   	get_firstr     s#    bH%HbFb\abbr   c                 d    t        | j                  t        |            D ]  }||s|r|c S  |S r   )rm   r?   r
   )dkey_or_keysr   skip_false_valuesr   s        r   dict_getr   #  s7    155(;/0 ?+<J Nr   )NT)collections.abcrU   rg   http.cookiesrQ   r   rN   r]   xml.etree.ElementTreerY   _utilsr   r   r   r   r   r	   r
   r   r   r   r"   r   r   <module>r      sK         	    (tTj%H6Vcr   