
    V|hYG                       S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SK	r	S SK
r
S SKrS SKrSSKJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJ r J!r!J"r"  \SSS\SS.S jr#S	 r$SS
.S jr% " S S\5      r&\RN                  SSS.S%S jj5       r(\RN                  SSS.S&S jj5       r(S'SSS.S(S jjjr(\RN                  SSSS.S)S jj5       r)\RN                  SS.S*S jj5       r)\RN                  SSSS.S+S jj5       r)\RN                  SSS.S,S jj5       r)SSSSSSSS.S jr)\RN                  SS.S*S jj5       r*\RN                  SSSS.S)S jj5       r*SSSSSSS.S jr*SSS .S! jr+S" r,S# r-S-S$ jr.g).    )annotationsN   )IDENTITY
NO_DEFAULTExtractorErrorLazyListdeprecation_warningget_elements_html_by_classget_elements_html_by_attributeget_elements_by_attributeget_element_by_classget_element_html_by_attributeget_element_by_attributeget_element_html_by_idget_element_by_idget_element_html_by_classget_elements_by_class get_element_text_and_html_by_tagis_iterable_liketry_callurl_or_nonevariadicTF)defaultexpected_typeget_all	casesenseis_user_inputtraverse_stringc                 ^^^^^^^^^^^ U[         La  [        S5        S m[        T[        5      (       a  U4S jmOU4S jmUUUUUU4S jmS mUUUU4S jmUUU4S jm[	        US	5       H%  u  pU[        U5      :H  n
 T" X	U
S
5      nUb  Us  $ M'     T[         L a  S$ T$ ! [         a1  nU
(       a  [        UR                  UR                  S9Se SnAMl  SnAff = f)a  
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.
    - `filter`-builtin: Return the value if it is truthy, `None` otherwise.

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

@params paths           Paths by which to traverse.
@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                P    [        U [        5      (       a  U R                  5       $ U $ N)
isinstancestrcasefold)ks    P/home/james-whalen/.local/lib/python3.13/site-packages/yt_dlp/utils/traversal.py<lambda>traverse_obj.<locals>.<lambda>l   s    As););BB    c                .   > [        U T5      (       a  U $ S $ r!   )r"   valr   s    r&   r'   r(   o   s    z#}'E'E O4 Or)   c                4   > [        T=(       d    [        U 4S9$ )Nargs)r   r   r+   s    r&   r'   r(   q   s    )B(# Pr)   c                  >^ ^^^ SnS nTc7  T(       a0  T SL d%  [        T 5      (       d  [        T [        5      (       a  SnSnGOvT c  TnGOo[        T [        5      (       a|  [	        [        T 5      5      n[        T 5      S:  d  [        U[        5      (       a7  [        S T  5       5      (       d   e[        T[        T 5      5      (       a  TnGO[        UT4S9nGO[        T [        [        45      (       a/  Sn[        R                  R                  UUU4S jT  5       5      nGOT SL Ga  Sn[        T[        R                   R"                  5      (       a  [%        TTR&                  TR(                  S	9m[        T[*        R,                  R.                  5      (       a  TR1                  5       nGO	[3        T5      (       d3  [        T[4        R6                  R8                  R:                  5      (       a  TnGO[        T[<        R>                  5      (       a  TRA                  5       nGOT(       a  Sn[C        T5      nGO{SnGOw[        T 5      (       Ga  Sn[        T[        R                   R"                  5      (       a  [%        TTR&                  TR(                  S	9m[        T[*        R,                  R.                  5      (       a  TRE                  5       nO[3        T5      (       d3  [        T[4        R6                  R8                  R:                  5      (       a  [G        T5      nO[        T[<        R>                  5      (       a]  [        R                  " [G        TRI                  5       /TRA                  5       Q75      TRK                  5       RE                  5       5      nO T(       a  Sn[G        [C        T5      5      nOSnU 4S
 jU 5       nU(       d  SRM                  U5      nGO[        T [$        5      (       aQ  UUU4S jT RE                  5        5       nU VVs0 s H  u  pxUc  T[N        Ld  M  Xxb  UOT_M     snn=(       d    S nGOk[        T[*        R,                  R.                  5      (       a  [        T[        R                   R"                  5      (       a  [%        TTR&                  TR(                  S	9mT(       d  [        TRP                  T 4S9(       a  [        TRR                  T 4S9O$[	        UU 4S jTRE                  5        5       S 5      nGO[        T[<        R>                  5      (       a  [        T [T        5      (       d  T(       a6  [V        RX                  " [Z        5         TRI                  T 5      nS S S 5        GO,[        T [B        5      (       a3  [	        UU 4S jTRK                  5       RE                  5        5       S 5      nGO[        T [T        [        45      (       a  [3        T[*        R,                  R\                  [4        R6                  R8                  R:                  45      (       a:  [        T [        5      n[V        RX                  " [Z        5         TT    nS S S 5        GO@T(       a3  [V        RX                  " [Z        5         [C        T5      T    nS S S 5        GOGO[        T[4        R6                  R8                  R:                  5      (       a  [        T [B        5      (       a  T R_                  S5      u  pmTRa                  S5      (       d  TRc                  S5      (       d  T n	S mU	Ra                  S5      (       a  SU	 3n	O"U	(       a  U	Ra                  S5      (       d  SU	 3n	U4S jnU	(       a%  [        [e        UTRg                  U	5      5      5      nOU" T5      nX3(       a  U4$ U44$ s  snnf ! , (       d  f       N"= f! , (       d  f       N3= f! , (       d  f       ND= f)NF.T r   c              3  B   #    U  H  n[        U[        5      v   M     g 7fr!   )r"   type.0items     r&   	<genexpr>2traverse_obj.<locals>.apply_key.<locals>.<genexpr>   s     Bcd:dD11cs   r.   c              3  >   >#    U  H  nT" TUT5      S    v   M     g7f)r   Nr1   )r5   branch
apply_pathis_lastobjs     r&   r7   r8      s#      3GBE
303#s   )keyvaluec              3  P   >#    U  H  u  p[        TX4S 9(       d  M  Uv   M     g7f)r.   N)r   )r5   r%   vr>   s      r&   r7   r8      s      KHDAA60JaaHs   &	& c              3  B   >#    U  H  u  pUT" TUS T5      4v   M     g7f)FNr1   )r5   r%   rA   _traverse_objr<   r=   s      r&   r7   r8      s#     [{tqM#q%AB{s   c              3  H   >#    U  H  u  pT" U5      T:X  d  M  Uv   M     g 7fr!   r1   r5   r%   rA   r$   r>   s      r&   r7   r8      s      L+$!!9KAA+   "	"c              3  H   >#    U  H  u  pT" U5      T:X  d  M  Uv   M     g 7fr!   r1   rF   s      r&   r7   r8      s$     [-DTQQRWZHZqq-DrG   /@z().z./c                   > Tc  U $ TS:X  a  U R                   $ TR                  S5      (       a"  [        U R                   R                  TSS  4S9$ TS:X  a  U R                  $ [        ST< 35      e)NrJ   r   r.   ztext()z#apply_specials is missing case for )attrib
startswithr   gettextSyntaxError)elementspecials    r&   apply_specials7traverse_obj.<locals>.apply_key.<locals>.apply_specials   ss    ?"Nc>">>)%%c**#GNN$6$6gabk^LLh&"<<'!$G{"STTr)   )4callabler"   slicesetnextiterlenr3   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__rO   int
contextlibsuppress
IndexErrorSequence
rpartitionrN   endswithmapiterfind)r>   r=   r<   	branchingresultr6   iter_objr%   rA   xpath_rT   rS   rD   r;   r$   r   r   r   s   ```         @r&   	apply_keytraverse_obj.<locals>.apply_keys   s   	;?czXc]]je.D.D 	[FS!!S	?D3x!|z$55BcBBBBBc5:.. F!$cV4dE]++I__22 3GBE3G GF CZI#t||22333CGG399=#{6677!#&&*S#)):O:O:W:W*X*XC** !	Sc]]I#t||22333CGG399=#{667799;!#&&*S#)):O:O:W:W*X*X$S>C**$??syy{:SZZ\:;MMO))+- !!	$SX.KHKFT""[syy{[H?G?Gtq=G:$= 3172x   
 [__4455#t||22333CGG399=8AXcN^N^fiekElhswwcV4L#))+LdS  RXX&&#s##y((4 YYs^F 54 C%%[S]]_-B-B-D[]abc5\**koo&>&>		@U@U@]@]%^__&sE2	((4 XF 54 ((4 Xc]F 54 ! SYY22::;;
3PS@T@T #s 3Eg%%c**73C3CD3I3I $$E7u//55UG	U c.#,,u2EFG',I&<<F9<<q 54 54 54s0   _5_2_*__0
_
_-0
_>c              3     #    [        U 5      n[        U[        5      nU[        L a  g U H  nSU4v   UnM     SU4v   g 7f)NFT)rZ   rY   r   )iterableiteratorprevr6   s       r&   	lazy_lasttraverse_obj.<locals>.lazy_last   sJ     >Hj):D+D  Djs   >A c           	       > U 4nSnS nT" [        U[        [        [        [        45      5       GH	  u  peT(       d%  [        U[        5      (       a  UR                  5       nU[        [        4;   a0  SnS U 5       nU[        L a  [        US 5      4nO[        U5      4nMr  U[        L a  [        S U5      nM  [        U5      (       a&  [        R                  " U5      R                  S S 5        / nU H#  n	T" XYU5      u  pXJ-  nUR!                  U5        M%     ["        R$                  R'                  U5      nGM     U(       a,  [        U[        [        [(        45      (       d  [+        TU5      nX4[        U[        5      4$ )NFc              3  8   #    U  H  oS 0 4;  d  M  Uv   M     g 7fr!   r1   )r5   r=   s     r&   r7   3traverse_obj.<locals>.apply_path.<locals>.<genexpr>  s      ND":8M   	)r   r#   bytesre   rX   r"   r$   anyr\   rY   r^   filterrV   inspect	signaturebindappendr_   r`   ra   r]   r~   )	start_objpath	test_typeobjshas_branchedr>   lastfiltered_objsnew_objsr=   r   resultsr   r   r   	type_tests               r&   r;    traverse_obj.<locals>.apply_path   sI   |"8D3tS2I#JKIDC!5!5llnsCj $ N N#: 57D /1Df}dD)%c]]!!#&++D$7H%.s%>"	)( 
 ??00:D7 L: ZdD%-@AAy$'D:c4#888r)   c                   > T" XU5      u  pEn[        S U 5       5      nT	(       a3  U(       a,  U(       a  UR                  5       $ U(       a  T[        L a  / $ T$ g U(       a  US   $ U(       a	  U(       a  0 $ S $ )Nc              3  8   #    U  H  oS 0 4;  d  M  Uv   M     g 7fr!   r1   r4   s     r&   r7   6traverse_obj.<locals>._traverse_obj.<locals>.<genexpr>$  s     NGDD":7M44Gr   r   )r   exhaustr   )
r=   r   allow_emptyr   r   r   is_dictr;   r   r   s
          r&   rD   #traverse_obj.<locals>._traverse_obj"  sk    )3Cy)I&wNGNN|(($
2r??$wqzQ"QTQr)   r   TNexpected)
r   r	   r"   r3   rr   r[   _RequiredErrorr   orig_msgr   )r=   r   r   r   r   r   r   pathsindexr   r<   r   erD   r   r;   r$   r   r   s    ```` `      @@@@@@r&   traverse_objr   &   s    F J&[\BH-&&O	P	z= z=x
%9 %9N
R !*3u:%	P"3gt<F! "	 + j(45g5  	P$QZZ!**E4O 	Ps   B&&
C!0&CC!c                  ^  U 4S j$ )Nc                   > T$ r!   r1   )r   r?   s    r&   r'   value.<locals>.<lambda>=  s    Ur)   r1   )r?   s   `r&   r?   r?   <  s	    r)   r   c                 ^ ^ UU 4S jnU$ )Nc                (   > U c  [        ST 3TS9eU $ )NzUnable to extract r   )r   )r?   r   names    r&   funcrequire.<locals>.funcA  s"    = #5dV!<xPPr)   r1   )r   r   r   s   `` r&   requirer   @  s     Kr)   c                      \ rS rSrSrg)r   iJ  r1   N)__name__
__module____qualname____firstlineno____static_attributes__r1   r)   r&   r   r   J  s    r)   r   undlangextc                    g r!   r1   r   s     r&   subs_list_to_dictr   N  s	     NQr)   c                  g r!   r1   )subsr   r   s      r&   r   r   R  s	     BEr)   c              h   U c  [         R                  " [        XS9$ [        R                  " [
        5      nU  H  n[        UR                  S5      5      (       d  UR                  S5      (       d  M:  UR                  SS5      n[        U[        5      (       d  U(       d  Mj  UnUR                  S5      n[        U[        5      (       d  U(       d  UR                  SS5        OX$S'   X5   R                  U5        M     [        U5      nUR                  5        H  n U R                  S S9  M     U$ )	aX  
Convert subtitles from a traversal into a subtitle dict.
The path should have an `all` immediately before this function.

Arguments:
`ext`      The default value for `ext` in the subtitle dict

In the dict you can set the following additional items:
`id`       The subtitle id to sort the dict into
`quality`  The sort order for each subtitle
Nr   urldataidr   c                8    U R                  SS5      =(       d    S$ )Nqualityr   )pop)xs    r&   r'   #subs_list_to_dict.<locals>.<lambda>y  s    i 3 8q 8r)   )r>   )	functoolspartialr   rf   defaultdictr^   r   rO   r   r"   r#   r   re   ri   sort)r   r   r   r   subsub_idsub_exts          r&   r   r   V  s     |  !2GG$$T*F3775>**3776??t$&#&&F''%.'3''t$ E
c"  &\F		8	9   Mr)   taghtmlregexc                    g r!   r1   attrr?   r   r   r   s        r&   find_elementr   ~  s    ]`r)   )r   c                    g r!   r1   clsr   s     r&   r   r     s    +.r)   c                    g r!   r1   )r   r   r   r   s       r&   r   r     s    ORr)   )r   r   c                    g r!   r1   r   s      r&   r   r     s    8;r)   )r   r   r   r   r?   r   r   c                  ^ ^	 T (       d#  U(       d  U(       d  U(       a  U(       d   S5       eSnU(       a[  U(       aT  U(       a   S5       eU(       a   S5       eU(       a  [         O[        n[        R                  " XUT =(       d    UU(       + S9$ U(       aO  U(       a   S5       eT b   S5       eU(       a   S5       eU(       a  [        O[
        n[        R                  " X5      $ U(       a7  U(       a  [        O[        n[        R                  " XT =(       d    UU(       + S9$ [        [        U5      5      m	U	U 4S	 j$ )
Nz3One of tag, id, cls or (attr AND value) is required[\w:.-]+Cannot match both attr and clszCannot match both attr and idr   escape_valuezCannot match both cls and idCannot match both cls and tagCannot use regex with clsc                "   > [        TU 5      T   $ r!   )r   )r   r   r   s    r&   r'   find_element.<locals>.<lambda>  s    8dCEJr)   )
r   r   r   r   r   r   r   r   rw   bool)
r   r   r   r   r?   r   r   ANY_TAGr   r   s
   `        @r&   r   r     s    "f1ff/G888w666v04,:R  UwY^U^__	555v{;;;{555y,0(6J  ++	)-%3D  s~gPUIVVT
OEJJr)   c                    g r!   r1   r   s     r&   find_elementsr     s    ,/r)   c                    g r!   r1   r   s        r&   r   r     s    ^ar)   )r   r   r   r?   r   r   c                j   U(       d  U(       a  U(       d   S5       eU(       aL  U(       aE  U(       a   S5       eU(       a  [         O[        n[        R                  " XbX0=(       d    SU(       + S9$ U (       a   S5       eU(       a   S5       eU(       a  [        O[
        n[        R                  " Xa5      $ )Nz*One of cls or (attr AND value) is requiredr   r   r   r   r   )r   r   r   r   r
   r   )r   r   r   r?   r   r   r   s          r&   r   r     s    4EP$PP"888w15-;T  U8J{]bYbcc33371119)-%3HDT''r)   )startendc                   ^ ^ UU 4S jnU$ )Nc                   > U c  g SnT(       a!  U R                  T5      (       a  [        T5      nT(       a$  U R                  T5      (       a  X[        T5      *  $ XS  $ )Nr   )rN   r[   r}   )s	start_idxr   r   s     r&   trimtrim_str.<locals>.trim  sT    9	Q\\%((E
I1::c??Cy))}r)   r1   )r   r   r   s   `` r&   trim_strr     s     Kr)   c                J   ^ ^ [         R                  " T 5      U U4S j5       nU$ )Nc                   > T" U 0 TD6$ r!   r1   )rq   r   kwargss    r&   innerunpack.<locals>.inner  s    U%f%%r)   )r   wraps)r   r   r  s   `` r&   unpackr    s%    __T& & Lr)   c                6    [        U /S U 5       Q70 UDSS0D6$ )Nc              3  @   #    U  H  nS /[        U5      Q7v   M     g7f).N)r   )r5   keyss     r&   r7   get_first.<locals>.<genexpr>  s     H%$5htn5%s   r   F)r   )r=   r   r   s      r&   	get_firstr	    s#    bH%HbFb\abbr)   c                    [        U R                  [        U5      5       H  nUc  M  U(       d	  U(       a  M  Us  $    U$ r!   )r~   rO   r   )dkey_or_keysr   skip_false_valuesr,   s        r&   dict_getr    s5    155(;/0?+<+<J 1 Nr)   )r   
str | Noner   r  returnz=collections.abc.Callable[[list[dict]], dict[str, list[dict]]])r   list[dict] | Noner   r  r   r  r  zdict[str, list[dict]]r!   )r   r  )r   r#   r?   r#   r   r  )r   r#   )r   r#   r   r  )r   r#   )NT)/
__future__r   rf   collections.abcrx   r   http.cookiesrb   r   r_   rn   typingxml.etree.ElementTreerj   _utilsr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r?   r   r   overloadr   r   r   r   r  r	  r  r1   r)   r&   <module>r     s   "        	       2 (tTj%S6l "' 	^ 	 ,1T  Q  Q HMae  E  E%D % %P =AUZ `  ` #( .  . /3%u R  R #( ;  ; 4d$UZ_ K4 $) /  / >BV[ a  a 4d$URW (  cr)   