U
    9%e'                     @   s   d dl Z d dlZd dlmZ d dlmZmZmZ G dd deZ	de
ee
ef ddd	Ze jee
ef ee
ef d
ddZdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )    N)Mapping)AnyCallableDictc                   @   s   e Zd ZdZdS )InterpretorErrorz
    An error raised when the interpretor cannot evaluate a Python expression, due to syntax error or unsupported
    operations.
    N)__name__
__module____qualname____doc__ r   r   d/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/transformers/tools/python_interpreter.pyr      s   r   F)codetoolsc                 C   s   zt | }W n2 tk
r@ } ztd| W Y dS d}~X Y nX |dkrNi }d}t|jD ]\}}zt|||}	W nd tk
r } zFd| d}
|r|
d| d7 }
n|
d| 7 }
t|
 W Y  qW 5 d}~X Y nX |	dk	r\|	}q\|S )a@  
    Evaluate a python expression using the content of the variables stored in a state and only evaluating a given set
    of functions.

    This function will recurse through the nodes of the tree provided.

    Args:
        code (`str`):
            The code to evaluate.
        tools (`Dict[str, Callable]`):
            The functions that may be called during the evaluation. Any call to another function will fail with an
            `InterpretorError`.
        state (`Dict[str, Any]`):
            A dictionary mapping variable names to values. The `state` should contain the initial inputs but will be
            updated by this function to contain all variables as they are evaluated.
        chat_mode (`bool`, *optional*, defaults to `False`):
            Whether or not the function is called from `Agent.chat`.
    z.The code generated by the agent is not valid.
Nz'Evaluation of the code stopped at line z. before the end because of the following errorzY. Copy paste the following error message and send it back to the agent:
I get an error: ''z:
)astparseSyntaxErrorprint	enumeratebodyevaluate_astr   )r   r   stateZ	chat_mode
expressioneresultidxnodeline_resultmsgr   r   r   evaluate    s.    

r   )r   r   r   c                    s  t | tjrt|  S t | tjr0t|  S t | tjrB| jS t | tjr fdd| j	D } fdd| j
D }tt||S t | tjrt| j S t | tjrt|  S t | tjrt| j S t | tj rt|  S ttdrt | tjrt| j S t | tjr@d fdd| j
D S t | tjrd fdd| jD S t | tjr~t|  S t | tjrt|  S t| jj  dd	S )
a  
    Evaluate an absract syntax tree using the content of the variables stored in a state and only evaluating a given
    set of functions.

    This function will recurse trough the nodes of the tree provided.

    Args:
        expression (`ast.AST`):
            The code to evaluate, as an abastract syntax tree.
        state (`Dict[str, Any]`):
            A dictionary mapping variable names to values. The `state` is updated if need be when the evaluation
            encounters assignements.
        tools (`Dict[str, Callable]`):
            The functions that may be called during the evaluation. Any call to another function will fail with an
            `InterpretorError`.
    c                    s   g | ]}t | qS r   r   ).0kr   r   r   r   
<listcomp>k   s     z evaluate_ast.<locals>.<listcomp>c                    s   g | ]}t | qS r   r    r!   vr#   r   r   r$   l   s     Index c                    s   g | ]}t t| qS r   )strr   r%   r#   r   r   r$   }   s     c                    s   g | ]}t | qS r   r    )r!   eltr#   r   r   r$      s     z is not supported.N)!
isinstancer   Assignevaluate_assignCallevaluate_callConstantvaluer   keysvaluesdictzipExprr   Forevaluate_forFormattedValueIfevaluate_ifhasattrr'   	JoinedStrjoinListeltsNameevaluate_name	Subscriptevaluate_subscriptr   	__class__r   )r   r   r   r2   r3   r   r#   r   r   N   s:    r   c                 C   s   | j }t| j||}t|dkr0|||d j< nLt|t|kr^tdt| dt| dt||D ]\}}|||j< qh|S )N   r   z	Expected z values but got .)targetsr   r1   lenidr   r5   )Zassignr   r   Z	var_namesr   var_namerr   r   r   r-      s    r-   c                    s   t | jtjs,td| j dt| j d| jj}|krPtd| jj d| } fdd| jD } fdd| jD }|||S )	NzZIt is not permitted to evaluate other functions than the provided tools (tried to execute z	 of type rG   z).c                    s   g | ]}t | qS r   r    )r!   argr#   r   r   r$      s     z!evaluate_call.<locals>.<listcomp>c                    s   i | ]}|j t|j qS r   )rM   r   r1   )r!   keywordr#   r   r   
<dictcomp>   s      z!evaluate_call.<locals>.<dictcomp>)	r+   funcr   rA   r   typerJ   argskeywords)callr   r   	func_namerP   rR   kwargsr   r#   r   r/      s    r/   c                 C   s   t | j||}t | j||}t|ttfr6|t| S ||krF|| S t|trt|trt	
|t| }t|dkr||d  S td| d| dd S )Nr   zCould not index z with 'z'.)r   slicer1   r+   listtupleintr)   r   difflibget_close_matchesr2   rI   r   )Z	subscriptr   r   indexr1   close_matchesr   r   r   rD      s    rD   c                 C   sX   | j |kr|| j  S t| j t| }t|dkrB||d  S td| j  dd S )Nr   zThe variable `z` is not defined.)rJ   r[   r\   rX   r2   rI   r   )namer   r   r^   r   r   r   rB      s    

rB   c                 C   s  t | jdkrtdt| j||}| jd }t| jd ||}t|tjrT||kS t|tj	rh||kS t|tj
r|||k S t|tjr||kS t|tjr||kS t|tjr||kS t|tjr||kS t|tjr||k	S t|tj r||kS t|tjr||kS td| d S )NrF   z2Cannot evaluate conditions with multiple operatorsr   zOperator not supported: )rI   Zopsr   r   leftZcomparatorsr+   r   EqNotEqLtLtEGtGtEIsIsNotInNotIn)	conditionr   r   r`   Z
comparatorrightr   r   r   evaluate_condition   s4    
rm   c                 C   s`   d }t | j||r8| jD ]}t|||}|d k	r|}qn$| jD ]}t|||}|d k	r>|}q>|S N)rm   testr   r   Zorelse)Zif_statementr   r   r   liner   r   r   r   r;      s    

r;   c                 C   sP   d }t | j||}|D ]4}||| jj< | jD ]}t |||}|d k	r,|}q,q|S rn   )r   itertargetrJ   r   )Zfor_loopr   r   r   iteratorcounterr   r   r   r   r   r8      s    
r8   )NF)r   r[   collections.abcr   typingr   r   r   
ValueErrorr   r)   r   ASTr   r-   r/   rD   rB   rm   r;   r8   r   r   r   r   <module>   s   	.$>	 