o
    DDiK                     @   s  U d dl mZmZmZmZmZ d dlZd dlm	Z	 dZ
eed< dZeed< eejejejejejejejejejejejejf Zded	efd
dZded	ee fddZdedee d	efddZG dd dejZG dd dej Z!dej"deeef d	ej"fddZ#e$ Z%ej$ed< e%fdedej$ded	ej&fddZ'e%fdedej$ded	eej(ej)f fddZ*e%fdedej$ded	ejfd d!Z+dS )"    )DictMappingOptionalSetUnionN)ensure_type__LIBCST_MANGLED_NAME_TEMPLATE_PREFIX_EMAN_DELGNAM_TSCBIL__TEMPLATE_SUFFIXvarreturnc                 C   s   t  |  t S N)r	   r   )r    r   Q/var/www/Datamplify/venv/lib/python3.10/site-packages/libcst/helpers/_template.pymangled_name    s   r   c                 C   s@   t | v rt| v r| t d\}}|td\}}|s|s|S d S )N   )r	   r   split)r   prefixname_and_suffixnamesuffixr   r   r   unmangled_name$   s   r   templatetemplate_varsc                 C   sZ   t | v st| v rtd|D ]}d| d}|| vr"td| d| |t|} q| S )Nz3Cannot parse a template containing reserved strings{}z+Template string is missing a reference to "z" referred to in kwargs)r	   r   	Exceptionreplacer   )r   r   r   originalr   r   r   mangle_template.   s   
r    c                   @   s  e Zd Zdeeef ddfddZdejdejdej	fdd	Z
dejdejdejfd
dZdejdejdejfddZdejdejdejfddZdejdejdejfddZdejdejdejfddZdejdejdejfddZdejdejdejfddZdejdejdejfddZdejdejdejfddZdej dej dej!fddZ"dej#dej#dej#fddZ$dej%dej%dej%fd d!Z&dS )"TemplateTransformertemplate_replacementsr   Nc                    s  dd |  D | _dd |  D | _dd |  D | _dd |  D | _dd |  D | _dd |  D | _dd |  D | _d	d |  D | _d
d |  D | _	dd |  D | _
dd |  D | _dd |  D | _h dd | jD dd | jD dd | jD dd | jD dd | jD dd | jD dd | jD dd | jD dd | j	D dd | j
D dd | jD dd | jD   fdd|D }|rtdtt| dd S )Nc                 S   "   i | ]\}}t |tjr||qS r   )
isinstancecstBaseExpression.0r   valuer   r   r   
<dictcomp>@       
z0TemplateTransformer.__init__.<locals>.<dictcomp>c                 S   r#   r   )r$   r%   
Annotationr'   r   r   r   r*   E   r+   c                 S   r#   r   )r$   r%   AssignTargetr'   r   r   r   r*   J   r+   c                 S   r#   r   )r$   r%   Paramr'   r   r   r   r*   O   r+   c                 S   r#   r   )r$   r%   
Parametersr'   r   r   r   r*   T   r+   c                 S   r#   r   )r$   r%   Argr'   r   r   r   r*   Y   r+   c                 S   r#   r   )r$   r%   BaseSmallStatementr'   r   r   r   r*   ^   r+   c                 S   r#   r   )r$   r%   BaseStatementr'   r   r   r   r*   c   r+   c                 S   r#   r   )r$   r%   	BaseSuiter'   r   r   r   r*   h   r+   c                 S   r#   r   )r$   r%   SubscriptElementr'   r   r   r   r*   m   r+   c                 S   r#   r   )r$   r%   	BaseSlicer'   r   r   r   r*   r   r+   c                 S   r#   r   )r$   r%   	Decoratorr'   r   r   r   r*   w   r+   c                 S      g | ]}|qS r   r   r(   r   r   r   r   
<listcomp>       z0TemplateTransformer.__init__.<locals>.<listcomp>c                 S   r7   r   r   r8   r   r   r   r9      r:   c                 S   r7   r   r   r8   r   r   r   r9      r:   c                 S   r7   r   r   r8   r   r   r   r9      r:   c                 S   r7   r   r   r8   r   r   r   r9      r:   c                 S   r7   r   r   r8   r   r   r   r9      r:   c                 S   r7   r   r   r8   r   r   r   r9      r:   c                 S   r7   r   r   r8   r   r   r   r9      r:   c                 S   r7   r   r   r8   r   r   r   r9      r:   c                 S   r7   r   r   r8   r   r   r   r9      r:   c                 S   r7   r   r   r8   r   r   r   r9      r:   c                 S   r7   r   r   r8   r   r   r   r9      r:   c                    s   h | ]}| vr|qS r   r   r8   supported_varsr   r   	<setcomp>   s    z/TemplateTransformer.__init__.<locals>.<setcomp>zTemplate replacement for "z" is unsupported)itemssimple_replacementsannotation_replacementsassignment_replacementsparam_replacementsparameters_replacementsarg_replacementssmall_statement_replacementsstatement_replacementssuite_replacementssubscript_element_replacementssubscript_index_replacementsdecorator_replacementsr   nextiter)selfr"   unsupported_varsr   r;   r   __init__=   s   	

zTemplateTransformer.__init__original_nodeupdated_nodec                 C   s.   t |j}|d u s|| jvr|S | j|  S r   )r   r)   r?   
deep_clonerM   rP   rQ   var_namer   r   r   
leave_Name   s   
zTemplateTransformer.leave_Namec                 C   8   |j }t|tjrt|j}|| jv r| j|  S |S r   )
annotationr$   r%   Namer   r)   r@   rR   )rM   rP   rQ   rW   rT   r   r   r   leave_Annotation      

z$TemplateTransformer.leave_Annotationc                 C   rV   r   )targetr$   r%   rX   r   r)   rA   rR   )rM   rP   rQ   r[   rT   r   r   r   leave_AssignTarget   rZ   z&TemplateTransformer.leave_AssignTargetc                 C   s(   t |jj}|| jv r| j|  S |S r   )r   r   r)   rB   rR   rS   r   r   r   leave_Param   s   
zTemplateTransformer.leave_Paramc                 C   s~   t |jdkr=|jtjjkr=t |jdkr=|jd u r=t |jdkr=|j	tjjkr=t
|jd jj}|| jv r=| j|  S |S Nr   r   )lenparamsstar_argr%   MaybeSentinelDEFAULTkwonly_params
star_kwargposonly_paramsposonly_indr   r   r)   rC   rR   rS   r   r   r   leave_Parameters   s   

z$TemplateTransformer.leave_Parametersc                 C   8   |j }t|tjrt|j }|| jv r| j|  S |S r   )r)   r$   r%   rX   r   rD   rR   )rM   rP   rQ   argrT   r   r   r   	leave_Arg   s   

zTemplateTransformer.leave_Argc                 C   \   t |jdkr,|jd }t|tjr,|j}t|tjr,t|j}|| jv r,| j| 	 S |S r^   )
r_   bodyr$   r%   Exprr)   rX   r   rF   rR   rM   rP   rQ   	body_node	name_noderT   r   r   r   leave_SimpleStatementLine      	


z-TemplateTransformer.leave_SimpleStatementLinec                 C   ri   r   )r)   r$   r%   rX   r   rE   rR   )rM   rP   rQ   rq   rT   r   r   r   
leave_Expr   s   

zTemplateTransformer.leave_Exprc                 C   rl   r^   )
r_   rm   r$   r%   rn   r)   rX   r   rG   rR   ro   r   r   r   leave_SimpleStatementSuite  rs   z.TemplateTransformer.leave_SimpleStatementSuitec                 C   s   t |jdkr>|jd }t|tjr>t |jdkr>|jd }t|tjr>|j}t|tjr>t|j}|| j	v r>| j	| 
 S |S r^   )r_   rm   r$   r%   SimpleStatementLinern   r)   rX   r   rG   rR   )rM   rP   rQ   statement_noderp   rq   rT   r   r   r   leave_IndentedBlock  s   	




z'TemplateTransformer.leave_IndentedBlockc                 C   ri   r   )r)   r$   r%   rX   r   rI   rR   )rM   rP   rQ   exprrT   r   r   r   leave_Index.  rZ   zTemplateTransformer.leave_Indexc                 C   sJ   |j }t|tjr#|j}t|tjr#t|j}|| jv r#| j|  S |S r   )	slicer$   r%   Indexr)   rX   r   rH   rR   )rM   rP   rQ   indexry   rT   r   r   r   leave_SubscriptElement;  s   


z*TemplateTransformer.leave_SubscriptElementc                 C   rV   r   )	decoratorr$   r%   rX   r   r)   rJ   rR   )rM   rP   rQ   r   rT   r   r   r   leave_DecoratorN  s   

z#TemplateTransformer.leave_Decorator)'__name__
__module____qualname__r   strValidReplacementTyperO   r%   rX   r&   rU   r,   rY   r-   r\   r.   r]   r/   rh   r0   rk   rv   r2   rr   rn   r1   rt   SimpleStatementSuiter3   ru   IndentedBlockrx   r|   r5   rz   r4   r~   r6   r   r   r   r   r   r!   <   s    

X
	




	





r!   c                   @   s6   e Zd Zdee ddfddZdejddfddZdS )	TemplateCheckerr   r   Nc                 C   s
   || _ d S r   )r   )rM   r   r   r   r   rO   [  s   
zTemplateChecker.__init__nodec                 C   s.   | j D ]}|jt|krtd| dqd S )NzTemplate variable "z" was not replaced properly)r   r)   r   r   )rM   r   r   r   r   r   
visit_Name^  s
   
zTemplateChecker.visit_Name)	r   r   r   r   r   rO   r%   rX   r   r   r   r   r   r   Z  s    r   treer"   c                 C   s   t |}t| |tjS r   )r!   r   visitr%   CSTNode)r   r"   	unmanglerr   r   r   unmangle_nodesd  s   r   _DEFAULT_PARTIAL_PARSER_CONFIGconfigc                 K   N   t | dd |D }t||}tt||tj}|tdd |D  |S )a  
    Accepts an entire python module template, including all leading and trailing
    whitespace. Any :class:`~libcst.CSTNode` provided as a keyword argument to
    this function will be inserted into the template at the appropriate location
    similar to an f-string expansion. For example::

      module = parse_template_module("from {mod} import Foo\n", mod=Name("bar"))

    The above code will parse to a module containing a single
    :class:`~libcst.FromImport` statement, referencing module ``bar`` and importing
    object ``Foo`` from it. Remember that if you are parsing a template as part
    of a substitution inside a transform, its considered
    :ref:`best practice <libcst-config_best_practice>` to pass in a ``config``
    from the current module under transformation.

    Note that unlike :func:`~libcst.parse_module`, this function does not support
    bytes as an input. This is due to the fact that it is processed as a template
    before parsing as a module.
    c                 S      h | ]}|qS r   r   r8   r   r   r   r=     r:   z(parse_template_module.<locals>.<setcomp>c                 S   r   r   r   r8   r   r   r   r=     r:   )r    r%   parse_moduler   r   Moduler   r   )r   r   r"   sourcemodule
new_moduler   r   r   parse_template_moduleo  s
   r   c                 K   sl   t | dd |D }t||}t||}t|tjtjfs(td|jj	 d|
tdd |D  |S )a<  
    Accepts a statement template followed by a trailing newline. If a trailing
    newline is not provided, one will be added. Any :class:`~libcst.CSTNode`
    provided as a keyword argument to this function will be inserted into the
    template at the appropriate location similar to an f-string expansion. For
    example::

      statement = parse_template_statement("assert x > 0, {msg}", msg=SimpleString('"Uh oh!"'))

    The above code will parse to an assert statement checking that some variable
    ``x`` is greater than zero, or providing the assert message ``"Uh oh!"``.

    Remember that if you are parsing a template as part of a substitution inside
    a transform, its considered :ref:`best practice <libcst-config_best_practice>`
    to pass in a ``config`` from the current module under transformation.
    c                 S   r   r   r   r8   r   r   r   r=     r:   z+parse_template_statement.<locals>.<setcomp>zExpected a statement but got a !c                 S   r   r   r   r8   r   r   r   r=     r:   )r    r%   parse_statementr   r$   rv   BaseCompoundStatementr   	__class__r   r   r   )r   r   r"   r   	statementnew_statementr   r   r   parse_template_statement  s   
r   c                 K   r   )u&  
    Accepts an expression template on a single line. Leading and trailing whitespace
    is not valid (there’s nowhere to store it on the expression node). Any
    :class:`~libcst.CSTNode` provided as a keyword argument to this function will
    be inserted into the template at the appropriate location similar to an
    f-string expansion. For example::

      expression = parse_template_expression("x + {foo}", foo=Name("y")))

    The above code will parse to a :class:`~libcst.BinaryOperation` expression
    adding two names (``x`` and ``y``) together.

    Remember that if you are parsing a template as part of a substitution inside
    a transform, its considered :ref:`best practice <libcst-config_best_practice>`
    to pass in a ``config`` from the current module under transformation.
    c                 S   r   r   r   r8   r   r   r   r=     r:   z,parse_template_expression.<locals>.<setcomp>c                 S   r   r   r   r8   r   r   r   r=     r:   )r    r%   parse_expressionr   r   r&   r   r   )r   r   r"   r   
expressionnew_expressionr   r   r   parse_template_expression  s   r   ),typingr   r   r   r   r   libcstr%   libcst.helpers.commonr   r	   r   __annotations__r   r&   r,   r-   r.   r/   r0   r2   r1   r3   r5   r4   r6   r   r   r   r    CSTTransformerr!   
CSTVisitorr   r   r   PartialParserConfigr   r   r   rv   r   r   r   r   r   r   r   <module>   s   
   



"
%