
    ghmR              	         U d Z ddlmZ ddlZddlmZmZmZ ddlm	Z	 ddl
mZmZmZmZmZmZ erddlmZ  ej        e          Zeeed         eeef         f                  Zd	ed
<   dada G d de          Zd;dZd<dZd=dZ d>dZ!	 d?d@d"Z"dAd$Z#dBd*Z$dCd.Z%i Z&d/ed0<    e	i           Z'd1ed2<   d3e'e'd3dddd4d4f	dDd:Z(dS )EzDAdapted from https://github.com/noahmorrison/chevron.

MIT License.
    )annotationsN)IteratorMappingSequence)MappingProxyType)TYPE_CHECKINGAnyLiteralOptionalUnioncast)	TypeAlias)Fr   r   Scopes   c                      e Zd ZdZdS )ChevronErrorz$Custom exception for Chevron errors.N)__name__
__module____qualname____doc__     Z/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/langchain_core/utils/mustache.pyr   r   "   s        ....r   r   templatestrl_delreturntuple[str, str]c                    	 |                      |d          \  }} t          |                    d          z  an# t          $ r | dfcY S w xY w|| fS )zParse a literal from the template.

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

    Returns:
        tuple[str, str]: The literal and the template.
    r   
 )split_CURRENT_LINEcount
ValueError)r   r   literals      r   grab_literalr'   +   sq    $NN5!44t,,,    "~ Xs   69 A
	A
r&   is_standaloneboolc                    |                     d          dk    s|r5|                    d          d         }|                                p|dk    S dS )a  Do 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:
        bool: Whether the tag could be a standalone.
    r    r!   F)findr"   isspace)r   r&   r(   paddings       r   
l_sa_checkr/   D   sV      ||DR=--%%b)
   1GrM15r   tag_typec                    |r=|dvr9|                      dd          }|d                                         p|d          S dS )a  Do 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:
        bool: Whether the tag could be a standalone.
    )variable	no escaper    r   r   F)r"   r-   )r   r0   r(   
on_newlines       r   
r_sa_checkr5   ^   sU       <)BBB^^D!,,
 !}$$&&;jm*;; 5r   r_deltuple[tuple[str, str], str]c           	        ddddddddd	}	 |                      |d
          \  }} n,# t          $ r}dt           }t          |          |d}~ww xY w|                    |d         d          }|dk    r
|d
d         }|dk    r;|                    d          rd}|dd         }nLdt           }t          |          |dk    r-|dk    r'|dk    r!|                     d          r| d
d         } d}||                                f| fS )aW  Parse a tag from a template.

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

    Returns:
        tuple[tuple[str, str], str]: 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?r3   )!#^/>={&r   zunclosed tag at line Nr   r2   rD   set delimiterr+   z#unclosed set delimiter tag
at line {{}}})r"   r%   r#   r   getendswith
startswithstrip)r   r   r6   	tag_typestagemsgr0   s           r   	parse_tagrS   x   sn     	 	I' ua00XX ' ' '5m553Q&'
 }}SVZ00H :!""g ###<< 	$&Hcrc(CC IHHCs### 	L   TMMTMM$$  ABB< syy{{#X..s   ' 
AAArH   rI   def_ldeldef_rdelIterator[tuple[str, str]]c              #  n  K   da dad}g }|}|}| rt          | |          \  }} | sd|fV  nbt          | ||          }t	          | ||          \  }} |\  }	}
|	dk    r8|
                                                    d          }|d         |d         }}n|	d	v r|                    |
           t           anv|	d
k    rp	 |                                }n2# t          $ r%}d|
 dt           dz    }t          |          |d}~ww xY w|
|k    r"d|
 d| dt           dz    }t          |          t          | |	|          }|r7|                     dd          d         } |	dk    r|                    d          }|dk    rd|fV  |	dvr|	|
fV  | |r"d|d          dt           }t          |          dS )aY  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)

    Returns:
        A generator of 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.
    r   NTr&   rG    r   r+   r:   r;   r<   zTrying to close tag "z%"
Looks like it was not opened.
line z"
last open tag is "z"
line r    r=   r!   )r9   r>   zUnexpected EOF
the tag "z&" was never closed
was opened at line )r#   _LAST_TAG_LINEr'   r/   rS   rN   r"   appendpop
IndexErrorr   r5   rstrip)r   rT   rU   r(   open_sectionsr   r6   r&   rP   r0   tag_keydelslast_sectionrQ   rR   s                  r   tokenizerc      s     : MNMMEE
 H&(599  	g&&&& #8WmDD "(E599X'
 &&==??((--D7DH5EE 888  )))*NN /,0022 / / /0G 0 0)A-0 0 
 #3''Q./ ,&&0G 0 0)50 0)A-0 0 
 #3''' #8X}EE  	.~~dA..r2H 9$$!..-- b==g&&&& 888W%%%%Q  H&V   3%b)3 3"03 3 	
 3   s   ?C 
D C>>Dstringc                    dddd}|                      dd          } |                                D ]\  }}|                      ||          } | S )z!HTML escape all of these " & < >.z&quot;z&lt;z&gt;)"<rC   rF   z&amp;)replaceitems)rd   
html_codescharcodes       r   _html_escaperm   C  sg      J ^^C))F &&(( , ,
dd++Mr   keyscopeswarnkeepr	   c               P   | dk    r|d         S |D ]}	 |dv r|c S |}|                      d          D ]x}|dv r|c c S 	 t          d|          |         }$# t          t          f$ rA 	 t	          ||          }n,# t          t          f$ r |t          |                   }Y nw xY wY uw xY w	 |j        r|c S n # t          $ r |dv r|cY c S |pdcY c S w xY w# t          t          t          t          f$ r Y w xY w|rt                              d|            |r
| d|  d| S dS )z!Get a key from the current scope..r   )r   Fzdict[str, Any]r!   zCould not find key '%s'rX   )r"   r   	TypeErrorAttributeErrorgetattrint _CHEVRON_return_scope_when_falsyKeyErrorr]   r%   loggerwarning)	rn   ro   rp   rq   rT   rU   scoperesolved_scopechilds	            r   _get_keyr   R  s     czzay  % %$	 
"""N3 D D "Z//))))))D%)*:N%K%KE%RNN!>2 D D DD)0)G)G%~6 D D D)7E

)CDD, "B *))))*! , , ,!Z//))))))%++++++,* *jA 	 	 	 D	  70#666 .--S--8---2s   C C ACB))A:9B):&B#	 B)"B#	#B)&C(B))C-B98C9C	CCCCCC:9C:namepartials_dictMapping[str, str]c                6    	 ||          S # t           $ r Y dS w xY w)zLoad a partial.r!   )ry   )r   r   s     r   _get_partialr     s4    T""   rrs   
 
z dict[str, list[tuple[str, str]]]g_token_cachezMappingProxyType[str, str]
EMPTY_DICTr!   F!Union[str, list[tuple[str, str]]]dataMapping[str, Any]r.   Optional[Scopes]c	                   t          | t                    r"t          | t                    sd | D             }	n2| t          v rd t          |          D             }	nt	          |           }	d}
|g|	D ]c\  }}d         }|dk    rd= |s.t                    dk    r|dv r                    dd	           H|d
k    r|
|                    ddz             z  }
k|dk    r^t          |          }|du r|dk    rd         }t          |t                    st          |          }|
t          |          z  }
|dk    r@t          |          }t          |t                    st          |          }|
|z  }
|dk    rtt          |          }t          |          rd}g }|	D ]p}|d|fk    r ne|                    |           |\  }}|d
k    r||z  }2|dk    r| d| d z  }F|d                    ddddddddd|         |          z  }q|t          |<    ||d%fd	          }|
|z  }
t          |t          t          f          r~t          |t                    sig }d}|	D ]9}|d|fk    r|dz  }|d|fk    r|dz  }|dk     r n|                    |           :|D ]$}|gz   }t          ||          }|
|z  }
%x                    d|           |dk    r<t          |          }                    dt          d |                      |d!k    rt!          |          }|
                    d          d"         }}|                                r||z  }t          ||#          }|                                r|                    d$          }|
|z  }
e|
S )&au  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     K   | ]}|V  d S Nr   .0tokens     r   	<genexpr>zrender.<locals>.<genexpr>  s"      ,I,IuU,I,I,I,I,I,Ir   c              3     K   | ]}|V  d S r   r   r   s     r   r   zrender.<locals>.<genexpr>  s"      ==E%======r   r!   Nr   r<   r   rY   Fr&   r    r2   )rp   rq   rT   rU   Trs   r3   r:   z& rX   z	{}{} {}{}r?   r@   rA   rB   rC   rD   rF   )r9   r:   r;   r<   r=   rG   r3   r2   c                D    t          | i |r|gz   p	  	        S )N)r   r   r.   rT   rU   ro   rp   rq   )render)	r   r   rT   rU   rq   r.   r   ro   rp   s	     r   <lambda>zrender.<locals>.<lambda>7  sA     &3 '!)!)#7A6!!
1 
1 
1 r   )r   ro   r.   r   rT   rU   rp   rq   r;   zLiteral[False]r=      )r   r   rT   rU   r.   ro   rp   rq   z 	r   )
isinstancer   r   r   rc   leninsertrh   r   rm   callabler[   formatr   r   r   r   
rpartitionr-   r^   )r   r   r   r.   rT   rU   ro   rp   rq   tokensoutputrP   rn   current_scopethingr|   texttagsr   r0   r`   rendtags_with_same_key	new_scoper=   leftpart_paddingpart_outs     ```````                   r   r   r     s   R (H%% 8j3.G.G 8 -J,I,I,I,I	]	"	"==]8%<=== (Hh77F~  u uSq	 %<<q		  k	3v;;!#3#3555a''' Ickk$w777FF JV$THx  E }} q	eS)) #E

l5)))FF KV$THx  E eS)) #E

eOFF IV$THx  E  X(.0#  E,,KK&&&(-%Hg9,,![008 C Cw C C C CC 2 2$+.+.47'*+.14-0,.	 	 '	( $$! !   '+d#u
 
 
 
 
 
 
 
 
 
 
  $ EHh#788 $(ESVAWAW $( %&"# ' 'EC 000*a/*,,*a/*-11!EKK&&&& # # #E!&& 0I!!%( '&3!)!)!!	 	 	D dNFF#$ a'''' &&&V$THx  E MM!T"2I>>???? I"366G $$T**1-D"L||~~ %$  +!!$	 	 	H ||~~ 2#??511 hFMr   )r   r   r   r   r   r   )r   r   r&   r   r(   r)   r   r)   )r   r   r0   r   r(   r)   r   r)   )r   r   r   r   r6   r   r   r7   )rH   rI   )r   r   rT   r   rU   r   r   rV   )rd   r   r   r   )rn   r   ro   r   rp   r)   rq   r)   rT   r   rU   r   r   r	   )r   r   r   r   r   r   )r   r   r   r   r   r   r.   r   rT   r   rU   r   ro   r   rp   r)   rq   r)   r   r   ))r   
__future__r   loggingcollections.abcr   r   r   typesr   typingr   r	   r
   r   r   r   typing_extensionsr   	getLoggerr   rz   listr   r   __annotations__r#   rZ   SyntaxErrorr   r'   r/   r5   rS   rc   rm   r   r   r   r   r   r   r   r   <module>r      sq    
 # " " " " "  7 7 7 7 7 7 7 7 7 7 " " " " " "                 ,++++++		8	$	$ wx0'#s(2CCDE E E E E / / / / /; / / /   2   4   4D/ D/ D/ D/Z :>w  w  w  w  w ~   ? ? ? ?D    35 4 4 4 4)9)9")=)=
 = = = = 35('1#p p p p p p pr   