o
    ܀c:                     @   s  d dl Z d dlmZ d dlmZmZmZ e dZG dd dZ	G dd dZ
G d	d
 d
e	e
ZG dd de	ZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd de	ZG dd de	ZG dd  d e	ZG d!d" d"eZG d#d$ d$e	ZG d%d& d&e	ZG d'd( d(e	ZG d)d* d*e	ZG d+d, d,e	ZG d-d. d.e	ZG d/d0 d0e
ZG d1d2 d2e
Zed3g d4Z G d5d6 d6Z!dS )7    N)
namedtuple)DynamoDBNeedsConditionErrorDynamoDBNeedsKeyConditionError"DynamoDBOperationNotSupportedErrorz[^.\[\]]+(?![^\[]*\])c                   @   sP   e Zd ZdZdZ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 )ConditionBase Fc                 G   
   || _ d S N)_valuesselfvalues r   D/usr/local/lib/python3.10/dist-packages/boto3/dynamodb/conditions.py__init__      
zConditionBase.__init__c                 C      t |ts
td|t| |S NAND)
isinstancer   r   Andr   otherr   r   r   __and__"      


zConditionBase.__and__c                 C   r   NOR)r   r   r   Orr   r   r   r   __or__'   r   zConditionBase.__or__c                 C      t | S r	   )Notr   r   r   r   
__invert__,   s   zConditionBase.__invert__c                 C   s   | j | j| jdS )N)formatoperatorr   )expression_formatexpression_operatorr
   r!   r   r   r   get_expression/   s   zConditionBase.get_expressionc                 C   s"   t |t| r| j|jkrdS dS )NTF)r   typer
   r   r   r   r   __eq__6   s   zConditionBase.__eq__c                 C      |  | S r	   r)   r   r   r   r   __ne__<      zConditionBase.__ne__N)__name__
__module____qualname__r%   r&   has_grouped_valuesr   r   r   r"   r'   r)   r,   r   r   r   r   r      s    r   c                   @   st   e Zd Z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d Zdd Zdd Zdd Zdd ZdS )AttributeBasec                 C   r   r	   )name)r   r3   r   r   r   r   A   r   zAttributeBase.__init__c                 C   
   t d| r   r   r   valuer   r   r   r   D   r   zAttributeBase.__and__c                 C   r4   r   r5   r6   r   r   r   r   G   r   zAttributeBase.__or__c                 C   r4   )NNOTr5   r!   r   r   r   r"   J   r   zAttributeBase.__invert__c                 C   
   t | |S )zCreates a condition where the attribute is equal to the value.

        :param value: The value that the attribute is equal to.
        )Equalsr6   r   r   r   eqM      
zAttributeBase.eqc                 C   r9   )zCreates a condition where the attribute is less than the value.

        :param value: The value that the attribute is less than.
        )LessThanr6   r   r   r   ltT   r<   zAttributeBase.ltc                 C   r9   )zCreates a condition where the attribute is less than or equal to the
           value.

        :param value: The value that the attribute is less than or equal to.
        )LessThanEqualsr6   r   r   r   lte[      
zAttributeBase.ltec                 C   r9   )zCreates a condition where the attribute is greater than the value.

        :param value: The value that the attribute is greater than.
        )GreaterThanr6   r   r   r   gtc   r<   zAttributeBase.gtc                 C   r9   )zCreates a condition where the attribute is greater than or equal to
           the value.

        :param value: The value that the attribute is greater than or equal to.
        )GreaterThanEqualsr6   r   r   r   gtej   rA   zAttributeBase.gtec                 C   r9   )zCreates a condition where the attribute begins with the value.

        :param value: The value that the attribute begins with.
        )
BeginsWithr6   r   r   r   begins_withr   r<   zAttributeBase.begins_withc                 C   s   t | ||S )a3  Creates a condition where the attribute is greater than or equal
        to the low value and less than or equal to the high value.

        :param low_value: The value that the attribute is greater than or equal to.
        :param high_value: The value that the attribute is less than or equal to.
        )Between)r   	low_value
high_valuer   r   r   betweeny   s   zAttributeBase.betweenc                 C   s   t |t| o| j|jkS r	   )r   r(   r3   r   r   r   r   r)      s   zAttributeBase.__eq__c                 C   r*   r	   r+   r   r   r   r   r,      r-   zAttributeBase.__ne__N)r.   r/   r0   r   r   r   r"   r;   r>   r@   rC   rE   rG   rK   r)   r,   r   r   r   r   r2   @   s    	r2   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ConditionAttributeBasezThis base class is for conditions that can have attribute methods.

    One example is the Size condition. To complete a condition, you need
    to apply another AttributeBase method like eq().
    c                 G   s(   t j| g|R   t| |d j d S )Nr   )r   r   r2   r3   r   r   r   r   r      s   zConditionAttributeBase.__init__c                 C   s   t | |ot| |S r	   )r   r)   r2   r   r   r   r   r)      s   zConditionAttributeBase.__eq__c                 C   r*   r	   r+   r   r   r   r   r,      r-   zConditionAttributeBase.__ne__N)r.   r/   r0   __doc__r   r)   r,   r   r   r   r   rL      s
    rL   c                   @      e Zd ZdZdS )ComparisonConditionz{0} {operator} {1}N)r.   r/   r0   r%   r   r   r   r   rO          rO   c                   @   rN   )r:   =Nr.   r/   r0   r&   r   r   r   r   r:      rP   r:   c                   @   rN   )	NotEqualsz<>NrR   r   r   r   r   rS      rP   rS   c                   @   rN   )r=   <NrR   r   r   r   r   r=      rP   r=   c                   @   rN   )r?   z<=NrR   r   r   r   r   r?      rP   r?   c                   @   rN   )rB   >NrR   r   r   r   r   rB      rP   rB   c                   @   rN   )rD   z>=NrR   r   r   r   r   rD      rP   rD   c                   @      e Zd ZdZdZdS )InINTN)r.   r/   r0   r&   r1   r   r   r   r   rW          rW   c                   @   rV   )rH   BETWEENz{0} {operator} {1} AND {2}Nr.   r/   r0   r&   r%   r   r   r   r   rH      rY   rH   c                   @   rV   )rF   rG   {operator}({0}, {1})Nr[   r   r   r   r   rF      rY   rF   c                   @   rV   )Containscontainsr\   Nr[   r   r   r   r   r]      rY   r]   c                   @   rV   )Sizesize{operator}({0})Nr[   r   r   r   r   r_      rY   r_   c                   @   rV   )AttributeTypeattribute_typer\   Nr[   r   r   r   r   rb      rY   rb   c                   @   rV   )AttributeExistsattribute_existsra   Nr[   r   r   r   r   rd      rY   rd   c                   @   rV   )AttributeNotExistsattribute_not_existsra   Nr[   r   r   r   r   rf      rY   rf   c                   @   rV   )r   r   ({0} {operator} {1})Nr[   r   r   r   r   r      rY   r   c                   @   rV   )r   r   rh   Nr[   r   r   r   r   r      rY   r   c                   @   rV   )r    r8   z({operator} {0})Nr[   r   r   r   r   r       rY   r    c                   @   s   e Zd ZdS )KeyN)r.   r/   r0   r   r   r   r   ri      s    ri   c                   @   sH   e Zd Z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 )Attrz(Represents an DynamoDB item's attribute.c                 C   r9   )zCreates a condition where the attribute is not equal to the value

        :param value: The value that the attribute is not equal to.
        )rS   r6   r   r   r   ne   r<   zAttr.nec                 C   r9   )zCreates a condition where the attribute is in the value,

        :type value: list
        :param value: The value that the attribute is in.
        )rW   r6   r   r   r   is_in   rA   z
Attr.is_inc                 C   r   )z/Creates a condition where the attribute exists.)rd   r!   r   r   r   exists     zAttr.existsc                 C   r   )z7Creates a condition where the attribute does not exist.)rf   r!   r   r   r   
not_exists  rn   zAttr.not_existsc                 C   r9   )z}Creates a condition where the attribute contains the value.

        :param value: The value the attribute contains.
        )r]   r6   r   r   r   r^     r<   zAttr.containsc                 C   r   )zCreates a condition for the attribute size.

        Note another AttributeBase method must be called on the returned
        size condition to be a valid DynamoDB condition.
        )r_   r!   r   r   r   r`     s   z	Attr.sizec                 C   r9   )zfCreates a condition for the attribute type.

        :param value: The type of the attribute.
        )rb   r6   r   r   r   rc     r<   zAttr.attribute_typeN)r.   r/   r0   rM   rk   rl   rm   ro   r^   r`   rc   r   r   r   r   rj      s    rj   BuiltConditionExpressioncondition_expressionattribute_name_placeholdersattribute_value_placeholdersc                   @   s^   e Zd ZdZdd Zdd Zdd Zdd	 ZdddZdd Z	dd Z
dd Z	
dddZdS )ConditionExpressionBuilderzCThis class is used to build condition expressions with placeholdersc                 C   s   d| _ d| _d| _d| _d S )Nr   nv)_name_count_value_count_name_placeholder_value_placeholderr!   r   r   r   r   4  s   
z#ConditionExpressionBuilder.__init__c                 C      d| j  t| j S )N#)rz   strrx   r!   r   r   r   _get_name_placeholder:     z0ConditionExpressionBuilder._get_name_placeholderc                 C   r|   )N:)r{   r~   ry   r!   r   r   r   _get_value_placeholder=  r   z1ConditionExpressionBuilder._get_value_placeholderc                 C   s   d| _ d| _dS )z&Resets the placeholder name and valuesr   N)rx   ry   r!   r   r   r   reset@  s   
z ConditionExpressionBuilder.resetFc                 C   s:   t |ts	t|i }i }| j||||d}t|||dS )a  Builds the condition expression and the dictionary of placeholders.

        :type condition: ConditionBase
        :param condition: A condition to be built into a condition expression
            string with any necessary placeholders.

        :type is_key_condition: Boolean
        :param is_key_condition: True if the expression is for a
            KeyConditionExpression. False otherwise.

        :rtype: (string, dict, dict)
        :returns: Will return a string representing the condition with
            placeholders inserted where necessary, a dictionary of
            placeholders for attribute names, and a dictionary of
            placeholders for attribute values. Here is a sample return value:

            ('#n0 = :v0', {'#n0': 'myattribute'}, {':v1': 'myvalue'})
        )is_key_conditionrq   )r   r   r   _build_expressionrp   )r   	conditionr   rs   rt   rr   r   r   r   build_expressionE  s   
z+ConditionExpressionBuilder.build_expressionc           	      C   sP   |  }g }|d D ]}| ||||j|}|| q
|d j|d|d iS )Nr   r#   r$   )r'   _build_expression_componentr1   appendr#   )	r   r   rs   rt   r   expression_dictreplaced_valuesr7   replaced_valuer   r   r   r   h  s    z,ConditionExpressionBuilder._build_expressionc                 C   sh   t |tr| ||||S t |tr-|r't |ts'td|j dt| d| ||S | 	|||S )NzAttribute object z is of type zD. KeyConditionExpression only supports Attribute objects of type Key)
r   r   r   r2   ri   r   r3   r(   _build_name_placeholder_build_value_placeholder)r   r7   rs   rt   r1   r   r   r   r   r     s$   


	z6ConditionExpressionBuilder._build_expression_componentc           	      C   s^   |j }t|}td|}g }|D ]}|  }|  jd7  _|| |||< q|t| S )Nz%s   )r3   ATTR_NAME_REGEXfindallsubr   rx   r   tuple)	r   r7   rs   attribute_nameattribute_name_partsplaceholder_formatstr_format_argspartname_placeholderr   r   r   r     s   


z2ConditionExpressionBuilder._build_name_placeholderc                 C   sn   |r&g }|D ]}|   }|  jd7  _|| |||< qdd| d S |   }|  jd7  _|||< |S )Nr   (z, ))r   ry   r   join)r   r7   rt   r1   placeholder_listrw   value_placeholderr   r   r   r     s   

z3ConditionExpressionBuilder._build_value_placeholderN)F)r.   r/   r0   rM   r   r   r   r   r   r   r   r   r   r   r   r   r   ru   1  s    
#%ru   )"recollectionsr   boto3.exceptionsr   r   r   compiler   r   r2   rL   rO   r:   rS   r=   r?   rB   rD   rW   rH   rF   r]   r_   rb   rd   rf   r   r   r    ri   rj   rp   ru   r   r   r   r   <module>   s@   
'I1
