
    D_iY              	         % S r SSKJr  SSKrSSKJrJrJr  SSKJ	r	  SSK
JrJrJrJr  \(       a  SSK
Jr  \R                   " \5      r\\S   \\\4   -     rS	\S
'   SqSq " S S\5      rSS jr        S S jr        S!S jrS"S jr S#       S$S jjrS%S jr               S&S jr!S'S jr"0 r#S\S'   \	" 0 5      r$S\S'   S\$\$SSSSSS4	                   S(S jjr%g))zDAdapted from https://github.com/noahmorrison/chevron.

MIT License.
    )annotationsN)IteratorMappingSequence)MappingProxyType)TYPE_CHECKINGAnyLiteralcast)	TypeAlias)Fr   r   Scopes   c                      \ rS rSrSrSrg)ChevronError    z$Custom exception for Chevron errors. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       W/home/james-whalen/.local/lib/python3.13/site-packages/langchain_core/utils/mustache.pyr   r       s    .r   r   c                     U R                  US5      u  p [        UR                  S5      -  qX 4$ ! [         a    U S4s $ f = f)zParse a literal from the template.

Args:
    template: The template to parse.
    l_del: The left delimiter.

Returns:
    The literal and the template.
r   
 )split_CURRENT_LINEcount
ValueError)templatel_delliterals      r   grab_literalr%   )   sS    $NN5!4t,, 	  "~s   ,1 AAc                    UR                  S5      S:w  d  U(       a1  UR                  SSS9S   nUR                  5       =(       d    U(       + $ g)zDo a preliminary check to see if a tag could be a standalone.

Args:
    template: The template. (Not used.)
    literal: The literal.
    is_standalone: Whether the tag is standalone.

Returns:
    Whether the tag could be a standalone.
r   r   )maxsplitF)findrsplitisspace)r"   r$   is_standalonepaddings       r   
l_sa_checkr.   B   sG      ||DR=...226
  /K/r   c                    U(       a<  US;  a6  U R                  SS5      nUS   R                  5       =(       d
    US   (       + $ g)zDo a final check to see if a tag could be a standalone.

Args:
    template: The template.
    tag_type: The type of the tag.
    is_standalone: Whether the tag is standalone.

Returns:
    Whether the tag could be a standalone.
>   variable	no escaper   r   r   F)r   r+   )r"   tag_typer,   
on_newlines       r   
r_sa_checkr4   \   sE      )BB^^D!,
 !}$$&;jm*;; r   c           	        SSSSSSSSS	.n U R                  US
5      u  p@UR	                  5       (       d  S[         3n[        U5      eUR                  US   S5      nUS:w  a  US
S nUS:X  a2  UR                  S5      (       a  SnUSS nOCS[         3n[        U5      eUS:X  a)  US:X  a#  US:X  a  U R                  S5      (       a  U S
S n SnXtR	                  5       4U 4$ ! [         a  nS[         3n[        U5      UeSnAff = f)a  Parse a tag from a template.

Args:
    template: The template.
    l_del: The left delimiter.
    r_del: The right delimiter.

Returns:
    The tag and the template.

Raises:
    ChevronError: If the tag is unclosed.
    ChevronError: If the set delimiter tag is unclosed.
commentsectioninverted sectionendpartialset delimiter?z
no escape?r1   )!#^/>={&r   zunclosed tag at line Nzempty tag at line r   r0   rA   set delimiterr'   z#unclosed set delimiter tag
at line {{}}})r   r!   r   r   stripgetendswith
startswith)r"   r#   r_del	tag_typestagemsgr2   s           r   	parse_tagrQ   v   sK     	I' ua0 99;;"=/23 }}SVZ0H :!"g ##<<&Hcr(C 9HCs## 	L  TMTM$$ AB< yy{#X..Y  '%m_53Q&'s   C$ $
D.DDrE   rF   c              #    #    Sq SqSn/ nUnUnU (       Ga!  [        X5      u  ppU (       d  SU4v   GO[        XU5      n[	        XU5      u  pUu  pU	S:X  a)  U
R                  5       R                  S5      nUS   US   peOWU	S	;   a  UR                  U
5        [         qO9U	S
:X  a3   UR                  5       nX:w  a  SU
 SU S[         S-    3n[        U5      e[        X	U5      nU(       a,  U R                  SS5      S   n U	S:w  a  UR                  S5      nU(       a  SU4v   U	S;  a  X4v   U (       a  GM!  U(       a  SUS    S[         3n[        U5      eg! [         a   nSU
 S[         S-    3n[        U5      UeSnAff = f7f)a  Tokenize a mustache template.

Tokenizes a mustache template in a generator fashion,
using file-like objects. It also accepts a string containing
the template.

Args:
    template: a file-like object, or a string of a mustache template
    def_ldel: The default left delimiter
        ("{{" by default, as in spec compliant mustache)
    def_rdel: The default right delimiter
        ("}}" by default, as in spec compliant mustache)

Yields:
    Mustache tags in the form of a tuple (tag_type, tag_key)
    where tag_type is one of:

    * literal
    * section
    * inverted section
    * end
    * partial
    * no escape

    and tag_key is either the key or in the case of a literal tag,
    the literal itself.

Raises:
    ChevronError: If there is a syntax error in the template.
r   NTr$   rD    r   r'   >   r7   r8   r9   zTrying to close tag "z%"
Looks like it was not opened.
line z"
last open tag is "z"
line r   r:   >   r6   r;   zUnexpected EOF
the tag "z&" was never closed
was opened at line )r   _LAST_TAG_LINEr%   r.   rQ   rH   r   appendpop
IndexErrorr   r4   rstrip)r"   def_ldeldef_rdelr,   open_sectionsr#   rL   r$   rN   r2   tag_keydelslast_sectionrO   rP   s                  r   tokenizer_      s    D MNMMEE
(9 g&& #8mD "(59
 &==?((-D7DH5 88  )*N /,002 & ,G9 5))5 7)A-.0 
 #3'' #8}E ~~dA.r2H 9$!..- g&& 88%%Q (V %b)* +""0!13 	
 3 O  /+G9 5)A-.0 
 #3'Q./s1   B"F%E 5B F8"F
F$E??FFc                    SSSS.nU R                  SS5      n UR                  5        H  u  p#U R                  X#5      n M     U $ )zHReturn the HTML-escaped string with these characters escaped: `" & < >`.z&quot;z&lt;z&gt;)"<r@   rC   z&amp;)replaceitems)string
html_codescharcodes       r   _html_escaperi   K  sN     J ^^C)F &&(
+ )Mr   c               F   U S:X  a  US   $ U H  n US;   a  Us  $ UnU R                  S5       H  nUS;   a  Us  s  $ [        U[        5      (       a   Xx   nM+  [        U[
        [        45      (       a   U[        U5         nMW  S	[        U5      R                   S
[        U5       3n	[	        U	5      e    UR                  (       a  Us  $ M     U(       a  [        R                  SU 5        U(       a
  U SU  SU 3$ g! [        [        4 a    SU< S3n	[        U	5      Sef = f! [        [        [        4 a    SU< S3n	[        U	5      Sef = f! [         a    US;   a  Us s  $ U=(       d    Ss s  $ f = f! [        [        [        [        [        4 a     GM  f = f)a  Retrieve a value from the current scope using a dot-separated key path.

Traverses through nested dictionaries and lists using dot notation.

Supports special key `'.'` to return the current scope.

Args:
    key: Dot-separated key path (e.g., `'user.name'` or `'.'` for current scope).
    scopes: List of scope dictionaries to search through.
    warn: Whether to log a warning when a key is not found.
    keep: Whether to return the original template tag when key is not found.
    def_ldel: Left delimiter for template (used when keep is `True`).
    def_rdel: Right delimiter for template (used when keep is `True`).

Returns:
    The value found at the key path.

        If not found, returns the original template tag when keep is `True`,
        otherwise returns an empty string.
.r   )r   FzKey z not found in dictNzInvalid index z for list/tuplezCannot traverse into z>. Mustache templates only support dict, list, and tuple. Got: r   zCould not find key '%s'rS   )r   
isinstancedictKeyError	TypeErrorlisttupleintr!   rW   typer    _CHEVRON_return_scope_when_falsyAttributeErrorloggerwarning)
keyscopeswarnkeeprY   rZ   scoperesolved_scopechildrP   s
             r   _get_keyr   Z  s   < czay 3	 
""N3 "Z/))nd336)7)>
  u>>8)7E
)C 0^0D0M0M/N O  $^ 457 
 $C.(9 (<, "BB)) CU p 0#61SE8*--Y %i0 6 $UI-?@&sm56 '
I> 8 .uiG(o478& " ,!Z/))%++, *j)L 	 		su   E<E<E<D E<6D'3E<8E $D$$E<')EE<E9%E<)E94E<8E99E<<F F c                ,     X   $ ! [          a     gf = f)z+Load a partial.

Returns:
    The partial.
r   )rn   )namepartials_dicts     r   _get_partialr     s"    "" s    
z dict[str, list[tuple[str, str]]]g_token_cachezMappingProxyType[str, str]
EMPTY_DICTr   Fc	                  ^^^^^^^ [        U [        5      (       a  [        U [        5      (       d
  S U  5       n	O(U [        ;   a  S [        U     5       n	O[	        U TT5      n	Sn
Tc  U/mU	 GH  u  pTS   nUS:X  a  TS	 M  U(       d+  [        T5      S:w  a  US;   a  TR                  SS5        MF  MH  US	:X  a  XR                  S
S
T-   5      -  n
Mg  US:X  aN  [        UTTTTTS9nUSL a  US:X  a  TS   n[        U[        5      (       d  [        U5      nU
[        U5      -  n
M  US:X  a4  [        UTTTTTS9n[        U[        5      (       d  [        U5      nX-  n
M  US:X  Gaa  [        UTTTTTS9n[        U5      (       a  Sn/ nU	 Hh  nUSU4:X  a    O_UR                  U5        Uu  nnUS	:X  a  UU-  nM0  US:X  a  UT SU ST 3-  nME  USR                  TSSSSSSSSS.U   UT5      -  nMj     U[        U'   U" US$UUUUUUU4S jj5      nU
U-  n
GM  [        U[        [        45      (       a{  [        U[        5      (       df  / nSnU	 H6  nUSU4:X  a  US-  nUSU4:X  a  US-  nUS:  a    OUR                  U5        M8     U H  nU/TQn[        UUTTTTTTS9nU
U-  n
M     GMH  TR                  SU5        GM]  US:X  a2  [        UTTTTTS9nTR                  S[        SU(       + 5      5        GM  US :X  d  GM  [!        UT5      nU
R#                  S
5      S!   nTnUR%                  5       (       a  UU-  n[        UTTTUTTTS"9nUR%                  5       (       a  UR'                  S#5      nU
U-  n
GM     U
$ )%a  Render a mustache template.

Renders a mustache template with a data scope and inline partial capability.

Args:
    template: A file-like object or a string containing the template.
    data: A python dictionary with your data scope.
    partials_path: The path to where your partials are stored.
         If set to None, then partials won't be loaded from the file system
         (defaults to '.').
    partials_ext: The extension that you want the parser to look for
        (defaults to 'mustache').
    partials_dict: A python dictionary which will be search for partials
         before the filesystem is. {'include': 'foo'} is the same
         as a file called include.mustache
         (defaults to {}).
    padding: This is for padding partials, and shouldn't be used
        (but can be if you really want to).
    def_ldel: The default left delimiter
         ("{{" by default, as in spec compliant mustache).
    def_rdel: The default right delimiter
         ("}}" by default, as in spec compliant mustache).
    scopes: The list of scopes that get_key will look through.
    warn: Log a warning when a template substitution isn't found in the data
    keep: Keep unreplaced tags when a substitution isn't found in the data.

Returns:
    A string containing the rendered template.
c              3  $   #    U  H  ov   M     g 7fNr   .0tokens     r   	<genexpr>render.<locals>.<genexpr>  s     ,IuU   c              3  $   #    U  H  ov   M     g 7fr   r   r   s     r   r   r     s     =%<E%%<r   r   r   r9   r   >   r7   r8   Fr$   r   r0   )rz   r{   rY   rZ   Trk   r1   r7   z& rS   z	{}{} {}{}r<   r=   r>   r?   r@   rA   rC   )r6   r7   r8   r9   r:   rD   r1   r0   c                P   > [        U 0 TTTTU=(       a    U/TQ=(       d    TTTS9	$ )N)datar   r-   rY   rZ   ry   rz   r{   )render)	r"   r   rY   rZ   r{   r-   r   ry   rz   s	     r   <lambda>render.<locals>.<lambda>e  s5     &3 '!)!) $ 8$CV!!
1r   )r"   ry   r-   r   rY   rZ   rz   r{   r8   zLiteral[False]r:      )r"   r   rY   rZ   r-   ry   rz   r{   z 	r   )rl   r   strr   r_   leninsertrc   r   ri   callablerU   formatr   r   r   r   
rpartitionr+   rX   )r"   r   r   r-   rY   rZ   ry   rz   r{   tokensoutputrN   rx   current_scopethingr|   texttagsr   r2   r\   rendtags_with_same_key	new_scoper:   leftpart_paddingpart_outs     ```````                   r   r   r     s3   R (H%%j3.G.G -J,I	]	"=]8%<= (Hh7F~ q	 %<q	 3v;!#355a' 6
 Ikk$w77F JV$THxE } q	eS))E
l5))F KV$THxE eS))E
OF IV$THxE .0#E,KK&(-%Hg9,![08*Bwiq
 CC 2 2$+.+.47'*+.14-0,.	 '	( $$!  $6 '+d#
 
 $ EHh#788ESVAWAW %&"#EC 00*a/*,*a/*-1!KK& $ #E!& 0 0I!!%( '&3!)!)!!	D dNF #$ a' &&V$THxE MM!T"2I>? I"36G $$T*1-D"L||~~$  +!!$	H ||~~#??51 hFk n Mr   )r"   r   r#   r   returnztuple[str, str])r"   r   r$   r   r,   boolr   r   )r"   r   r2   r   r,   r   r   r   )r"   r   r#   r   rL   r   r   ztuple[tuple[str, str], str])rE   rF   )r"   r   rY   r   rZ   r   r   zIterator[tuple[str, str]])re   r   r   r   )rx   r   ry   r   rz   r   r{   r   rY   r   rZ   r   r   r	   )r   r   r   Mapping[str, str]r   r   )r"   zstr | list[tuple[str, str]]r   zMapping[str, Any]r   r   r-   r   rY   r   rZ   r   ry   zScopes | Nonerz   r   r{   r   r   r   )&r   
__future__r   loggingcollections.abcr   r   r   typesr   typingr   r	   r
   r   r   	getLoggerr   rv   rp   r   r   __annotations__r   rT   SyntaxErrorr   r%   r.   r4   rQ   r_   ri   r   r   r   r   r   r   r   r   <module>r      s  
 #  7 7 "   			8	$ *WS#X->>?	 ? /; /2  
	4  
	4I/d :>| |  | 36| | Ha	aa 	a
 a a a 	aH
  35/ 4)9")=
& = -/('1 p)p
p %p 	p
 p p p p p 	pr   