o
    h:                     @   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   a/var/www/html/magazine_api/magazine_env/lib/python3.10/site-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   Z	low_valueZ
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   rI   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   rJ      s
    rJ   c                   @      e Zd ZdZdS )ComparisonConditionz{0} {operator} {1}N)r.   r/   r0   r%   r   r   r   r   rM          rM   c                   @   rL   )r:   =Nr.   r/   r0   r&   r   r   r   r   r:      rN   r:   c                   @   rL   )	NotEqualsz<>NrP   r   r   r   r   rQ      rN   rQ   c                   @   rL   )r=   <NrP   r   r   r   r   r=      rN   r=   c                   @   rL   )r?   z<=NrP   r   r   r   r   r?      rN   r?   c                   @   rL   )rB   >NrP   r   r   r   r   rB      rN   rB   c                   @   rL   )rD   z>=NrP   r   r   r   r   rD      rN   rD   c                   @      e Zd ZdZdZdS )InINTN)r.   r/   r0   r&   r1   r   r   r   r   rU          rU   c                   @   rT   )rH   ZBETWEENz{0} {operator} {1} AND {2}Nr.   r/   r0   r&   r%   r   r   r   r   rH      rW   rH   c                   @   rT   )rF   rG   {operator}({0}, {1})NrX   r   r   r   r   rF      rW   rF   c                   @   rT   )ContainscontainsrY   NrX   r   r   r   r   rZ      rW   rZ   c                   @   rT   )Sizesize{operator}({0})NrX   r   r   r   r   r\      rW   r\   c                   @   rT   )AttributeTypeattribute_typerY   NrX   r   r   r   r   r_      rW   r_   c                   @   rT   )AttributeExistsZattribute_existsr^   NrX   r   r   r   r   ra      rW   ra   c                   @   rT   )AttributeNotExistsZattribute_not_existsr^   NrX   r   r   r   r   rb      rW   rb   c                   @   rT   )r   r   ({0} {operator} {1})NrX   r   r   r   r   r      rW   r   c                   @   rT   )r   r   rc   NrX   r   r   r   r   r      rW   r   c                   @   rT   )r    r8   z({operator} {0})NrX   r   r   r   r   r       rW   r    c                   @   s   e Zd ZdS )KeyN)r.   r/   r0   r   r   r   r   rd      s    rd   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.
        )rQ   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.
        )rU   r6   r   r   r   is_in   rA   z
Attr.is_inc                 C   r   )z/Creates a condition where the attribute exists.)ra   r!   r   r   r   exists     zAttr.existsc                 C   r   )z7Creates a condition where the attribute does not exist.)rb   r!   r   r   r   
not_exists  ri   zAttr.not_existsc                 C   r9   )z}Creates a condition where the attribute contains the value.

        :param value: The value the attribute contains.
        )rZ   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.
        )r_   r6   r   r   r   r`     r<   zAttr.attribute_typeN)r.   r/   r0   rK   rf   rg   rh   rj   r[   r]   r`   r   r   r   r   re      s    re   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#)ru   strrs   r!   r   r   r   _get_name_placeholder:     z0ConditionExpressionBuilder._get_name_placeholderc                 C   rw   )N:)rv   ry   rt   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)rs   rt   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_conditionrl   )r   r   r   _build_expressionrk   )r   	conditionr   rn   ro   rm   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   rn   ro   r   Zexpression_dictZreplaced_valuesr7   Z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   rd   r   r3   r(   _build_name_placeholder_build_value_placeholder)r   r7   rn   ro   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subrz   rs   r   tuple)	r   r7   rn   Zattribute_nameZattribute_name_partsZplaceholder_formatZstr_format_argspartZ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}   rt   r   join)r   r7   ro   r1   Zplaceholder_listrr   Zvalue_placeholderr   r   r   r     s   

z3ConditionExpressionBuilder._build_value_placeholderN)F)r.   r/   r0   rK   r   rz   r}   r~   r   r   r   r   r   r   r   r   r   rp   1  s    
#%rp   )"recollectionsr   Zboto3.exceptionsr   r   r   compiler   r   r2   rJ   rM   r:   rQ   r=   r?   rB   rD   rU   rH   rF   rZ   r\   r_   ra   rb   r   r   r    rd   re   rk   rp   r   r   r   r   <module>   s@   
'I1
