a
    ”˜ëjêV  ã                   @  s¤  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rjddlmZ e e¡Zeeed eeef f  Zd	ed
< dadaG dd„ deƒZddddœdd„Zdddddœdd„Zdddddœdd„Z dddddœdd„Z!d;dddd d!œd"d#„Z"ddd$œd%d&„Z#dd
ddddd'd(œd)d*„Z$dd+dd,œd-d.„Z%i Z&d/ed0< e	i ƒZ'd1ed2< d3e'e'd3dddd4d4f	d5d6d+dddd7dddd8œ
d9d:„Z(dS )<zDAdapted 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                   @  s   e Zd ZdZdS )ÚChevronErrorz$Custom exception for Chevron errors.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú\/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain_core/utils/mustache.pyr   "   s   r   Ústrztuple[str, str])ÚtemplateÚl_delÚreturnc                 C  sF   z"|   |d¡\}} t| d¡7 aW n ty<   | df Y S 0 || fS )zÃParse 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   Úliteralr   r   r   Úgrab_literal+   s    r"   Úbool)r   r!   Úis_standaloner   c                 C  s8   |  d¡dks|r4|jddd�d }| ¡ p2|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   )Úmaxsplitr   F)ÚfindÚrsplitÚisspace)r   r!   r$   Úpaddingr   r   r   Ú
l_sa_checkD   s    r+   )r   Útag_typer$   r   c                 C  s2   |r.|dvr.|   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   r,   r$   Z
on_newliner   r   r   Ú
r_sa_check^   s    r/   ztuple[tuple[str, str], str])r   r   Úr_delr   c              
   C  s$  ddddddddd	œ}z|   |d
¡\}} W n8 tyb } z dt› �}t|ƒ|‚W Y d}~n
d}~0 0 | ¡ s~dt› �}t|ƒ‚| |d d¡}|dkr¢|d
d… }|dkrÚ| d¡rÆd}|dd… }ndt› �}t|ƒ‚n:|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?r.   )ú!ú#ú^ú/ú>ú=Ú{ú&r   zunclosed tag at line Nzempty tag at line r   r-   r<   úset delimiterr%   z#unclosed set delimiter tag
at line ú{{ú}}Ú})r   r    r   r   ÚstripÚgetÚendswithÚ
startswith)r   r   r0   Z	tag_typesÚtagÚeÚmsgr,   r   r   r   Ú	parse_tagx   sJ    ø
 



þûúù
rJ   r@   rA   zIterator[tuple[str, str]])r   Údef_ldelÚdef_rdelr   c              
   c  s®  da dad}g }|}|}| �rˆt| |ƒ\}} | s>d|fV  �qˆt| ||ƒ}t| ||ƒ\}} |\}	}
|	dkrŒ|
 ¡  d¡}|d |d  }}nž|	d	v r¤| |
¡ t an†|	d
k�r*z| ¡ }W nB t	yü } z*d|
› dt d › �}t
|ƒ|‚W Y d}~n
d}~0 0 |
|k�r*d|
› d|› dt d › �}t
|ƒ‚t| |	|ƒ}|�r`|  dd¡d } |	dk�r`| d¡}|dk�rtd|fV  |	dvr|	|
fV  q|�rªd|d › dt› �}t
|ƒ‚dS )ap  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!   r?   ú r   r%   >   r3   r2   r4   zTrying to close tag "z%"
Looks like it was not opened.
line z"
last open tag is "z"
line r   r5   r   >   r6   r1   zUnexpected EOF
the tag "z&" was never closed
was opened at line )r   Ú_LAST_TAG_LINEr"   r+   rJ   rC   r   ÚappendÚpopÚ
IndexErrorr   r/   Úrstrip)r   rK   rL   r$   Zopen_sectionsr   r0   r!   rG   r,   Útag_keyZdelsZlast_sectionrH   rI   r   r   r   ÚtokenizeÉ   sl    "


þÿ 
ÿþÿ



ÿþÿrT   )Ústringr   c                 C  s:   ddddœ}|   dd¡} | ¡ D ]\}}|   ||¡} q | S )zJReturn the HTML-escaped string with these characters escaped: ``" & < >``.z&quot;z&lt;z&gt;)ú"ú<r;   r>   z&amp;)ÚreplaceÚitems)rU   Z
html_codesÚcharÚcoder   r   r   Ú_html_escapeM  s    ýr\   r   )ÚkeyÚscopesÚwarnÚkeeprK   rL   r   c          
      C  s¼  | dkr|d S |D �]t}�zN|dv r0|W   S |}|   d¡D ]Ò}|dv rX|  W   S t|tƒržz|| }W n, ttfyš   d|›d�}	t|	ƒd‚Y n0 q>t|ttfƒrîz|t|ƒ }W n. tt	tfyê   d|›d�}	t	|	ƒd‚Y n0 q>d	t
|ƒj› d
t
|ƒ› �}	t|	ƒ‚q>z|j�r(|W W   S W n< t�yf   |dv �rR| Y W   S |�pZd Y W   S 0 W q ttt	ttf�yˆ   Y q0 q|�ržt d| ¡ |�r¸|› d| › d|› �S dS )z$Return a key from the current scope.Ú.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'rM   )r   Ú
isinstanceÚdictÚKeyErrorÚ	TypeErrorÚlistÚtupleÚintr    rQ   Útyper   Z _CHEVRON_return_scope_when_falsyÚAttributeErrorÚloggerÚwarning)
r]   r^   r_   r`   rK   rL   ÚscopeZresolved_scopeÚchildrI   r   r   r   Ú_get_key\  sR    


þÿ

ro   zMapping[str, str])ÚnameÚpartials_dictr   c                 C  s$   z
||  W S  t y   Y dS 0 dS )z7Load a partial.

    Returns:
        The partial.
    r   N)rd   )rp   rq   r   r   r   Ú_get_partial­  s    
rr   z dict[str, list[tuple[str, str]]]Úg_token_cachezMappingProxyType[str, str]Ú
EMPTY_DICTr   Fz!Union[str, list[tuple[str, str]]]zMapping[str, Any]zOptional[Scopes])
r   Údatarq   r*   rK   rL   r^   r_   r`   r   c	                   s¶  t | tƒr$t | tƒs$dd„ | D ƒ}	n(| tv r@dd„ t|  D ƒ}	nt| ˆ ˆƒ}	d}
ˆdu r^|g‰|	D �]L\}}ˆd }|dkr„ˆd= qb|sªtˆƒdkrª|d	v r¨ˆ dd
¡ qb|dkrÈ|
| ddˆ ¡7 }
qb|dk�r$t|ˆˆˆˆ ˆd�}|du �r|dk�rˆd }t |tƒ�st|ƒ}|
t	|ƒ7 }
qb|dk�r`t|ˆˆˆˆ ˆd�}t |tƒ�sVt|ƒ}|
|7 }
qb|dk�rt|ˆˆˆˆ ˆd�}t
|ƒ�rVd}g }|	D ]Ž}|d|fk�r¬ �q$| |¡ |\}}|dk�rÒ||7 }nN|dk�rö|ˆ › d|› dˆ› �7 }n*|d ˆ dddddddddœ| |ˆ¡7 }�q”|t|< ||d'‡ ‡‡‡‡‡‡fdd„	ƒ}|
|7 }
nºt |ttfƒ�rt |tƒ�sg }d}|	D ]L}|d|fk�r˜|d7 }|d|fk�r¾|d8 }|dk �r¾ �qÌ| |¡ �q~|D ]0}|gˆ¢}t||ˆˆˆ ˆˆˆd �}|
|7 }
�qÐnˆ d|¡ qb|d!k�rFt|ˆˆˆˆ ˆd�}ˆ dtd"| ƒ¡ qb|d#krbt|ˆƒ}|
 d¡d$ }ˆ}| ¡ �r|||7 }t|ˆˆ ˆ|ˆˆˆd%�}| ¡ �r¨| d&¡}|
|7 }
qb|
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                 s  s   | ]
}|V  qd S ©Nr   ©Ú.0Útokenr   r   r   Ú	<genexpr>î  ó    zrender.<locals>.<genexpr>c                 s  s   | ]
}|V  qd S rv   r   rw   r   r   r   rz   ð  r{   r   Nr   r4   r   >   r3   r2   Fr!   r   r-   )r_   r`   rK   rL   Tra   r.   r2   z& rM   z	{}{} {}{}r7   r8   r9   r:   r;   r<   r>   )r1   r2   r3   r4   r5   r?   r.   r-   c                   s(   t | i ˆˆˆ ˆ|r|gˆ¢pˆˆˆd�	S )N)ru   rq   r*   rK   rL   r^   r_   r`   )Úrender)r   ru   ©rK   rL   r`   r*   rq   r^   r_   r   r   Ú<lambda>T  s   ÷zrender.<locals>.<lambda>)r   r^   r*   rq   rK   rL   r_   r`   r3   zLiteral[False]r5   é   )r   rq   rK   rL   r*   r^   r_   r`   z 	)N)rb   r   r   rs   rT   ÚlenÚinsertrX   ro   r\   ÚcallablerO   Úformatr   r|   r   rr   Ú
rpartitionr)   rR   )r   ru   rq   r*   rK   rL   r^   r_   r`   ÚtokensÚoutputrG   r]   Zcurrent_scopeÚthingrm   ÚtextÚtagsry   r,   rS   ZrendZtags_with_same_keyZ	new_scoper5   ÚleftZpart_paddingZpart_outr   r}   r   r|   Â  sò    )
ÿ
ÿ

ÿ




ø	÷
ó
þ


ø
ÿ

ø


r|   )r@   rA   ))r   Ú
__future__r   ÚloggingÚcollections.abcr   r   r   Útypesr   Útypingr   r   r	   r
   r   r   Ztyping_extensionsr   Ú	getLoggerr   rk   rf   r   r   Ú__annotations__r   rN   ÚSyntaxErrorr   r"   r+   r/   rJ   rT   r\   ro   rr   rs   rt   r|   r   r   r   r   Ú<module>   sD    	
$	R ÿ Q÷