U
    O8c:                     @   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d3d4d5d6gZ G d7d8 d8Z!dS )9    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   s
   || _ d S N)_valuesselfvalues r   =/tmp/pip-unpacked-wheel-hml_76kf/boto3/dynamodb/conditions.py__init__   s    zConditionBase.__init__c                 C   s   t |tstd|t| |S NAND)
isinstancer   r   Andr   otherr   r   r   __and__"   s    

zConditionBase.__and__c                 C   s   t |tstd|t| |S NOR)r   r   r   Orr   r   r   r   __or__'   s    

zConditionBase.__or__c                 C   s   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   |  | S r   r$   r   r   r   r   __ne__<   s    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   s
   || _ d S r   )name)r   r,   r   r   r   r   A   s    zAttributeBase.__init__c                 C   s   t d| d S r   r   r   valuer   r   r   r   D   s    zAttributeBase.__and__c                 C   s   t d| d S r   r-   r.   r   r   r   r   G   s    zAttributeBase.__or__c                 C   s   t d| d S )NNOTr-   r   r   r   r   r   J   s    zAttributeBase.__invert__c                 C   s
   t | |S )zCreates a condition where the attribute is equal to the value.

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

        :param value: The value that the attribute is less than.
        )LessThanr.   r   r   r   ltT   s    zAttributeBase.ltc                 C   s
   t | |S )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.
        )LessThanEqualsr.   r   r   r   lte[   s    zAttributeBase.ltec                 C   s
   t | |S )zCreates a condition where the attribute is greater than the value.

        :param value: The value that the attribute is greater than.
        )GreaterThanr.   r   r   r   gtc   s    zAttributeBase.gtc                 C   s
   t | |S )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.
        )GreaterThanEqualsr.   r   r   r   gtej   s    zAttributeBase.gtec                 C   s
   t | |S )zCreates a condition where the attribute begins with the value.

        :param value: The value that the attribute begins with.
        )
BeginsWithr.   r   r   r   begins_withr   s    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#   r,   r   r   r   r   r$      s    zAttributeBase.__eq__c                 C   s   |  | S r   r%   r   r   r   r   r&      s    zAttributeBase.__ne__N)r'   r(   r)   r   r   r   r   r2   r4   r6   r8   r:   r<   r>   r$   r&   r   r   r   r   r+   @   s   	r+   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| f|  t| |d j d S )Nr   )r   r   r+   r,   r
   r   r   r   r      s    zConditionAttributeBase.__init__c                 C   s   t | |ot| |S r   )r   r$   r+   r   r   r   r   r$      s     zConditionAttributeBase.__eq__c                 C   s   |  | S r   r%   r   r   r   r   r&      s    zConditionAttributeBase.__ne__N)r'   r(   r)   __doc__r   r$   r&   r   r   r   r   r?      s   r?   c                   @   s   e Zd ZdZdS )ComparisonConditionz{0} {operator} {1}N)r'   r(   r)   r    r   r   r   r   rA      s   rA   c                   @   s   e Zd ZdZdS )r1   =Nr'   r(   r)   r!   r   r   r   r   r1      s   r1   c                   @   s   e Zd ZdZdS )	NotEqualsz<>NrC   r   r   r   r   rD      s   rD   c                   @   s   e Zd ZdZdS )r3   <NrC   r   r   r   r   r3      s   r3   c                   @   s   e Zd ZdZdS )r5   z<=NrC   r   r   r   r   r5      s   r5   c                   @   s   e Zd ZdZdS )r7   >NrC   r   r   r   r   r7      s   r7   c                   @   s   e Zd ZdZdS )r9   z>=NrC   r   r   r   r   r9      s   r9   c                   @   s   e Zd ZdZdZdS )InINTN)r'   r(   r)   r!   r*   r   r   r   r   rG      s   rG   c                   @   s   e Zd ZdZdZdS )r=   ZBETWEENz{0} {operator} {1} AND {2}Nr'   r(   r)   r!   r    r   r   r   r   r=      s   r=   c                   @   s   e Zd ZdZdZdS )r;   r<   {operator}({0}, {1})NrI   r   r   r   r   r;      s   r;   c                   @   s   e Zd ZdZdZdS )ContainscontainsrJ   NrI   r   r   r   r   rK      s   rK   c                   @   s   e Zd ZdZdZdS )Sizesize{operator}({0})NrI   r   r   r   r   rM      s   rM   c                   @   s   e Zd ZdZdZdS )AttributeTypeattribute_typerJ   NrI   r   r   r   r   rP      s   rP   c                   @   s   e Zd ZdZdZdS )AttributeExistsZattribute_existsrO   NrI   r   r   r   r   rR      s   rR   c                   @   s   e Zd ZdZdZdS )AttributeNotExistsZattribute_not_existsrO   NrI   r   r   r   r   rS      s   rS   c                   @   s   e Zd ZdZdZdS )r   r   ({0} {operator} {1})NrI   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdZdS )r   r   rT   NrI   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdZdS )r   r0   z({operator} {0})NrI   r   r   r   r   r      s   r   c                   @   s   e Zd ZdS )KeyN)r'   r(   r)   r   r   r   r   rU      s   rU   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   s
   t | |S )zCreates a condition where the attribute is not equal to the value

        :param value: The value that the attribute is not equal to.
        )rD   r.   r   r   r   ne   s    zAttr.nec                 C   s
   t | |S )zCreates a condition where the attribute is in the value,

        :type value: list
        :param value: The value that the attribute is in.
        )rG   r.   r   r   r   is_in   s    z
Attr.is_inc                 C   s   t | S )z/Creates a condition where the attribute exists.)rR   r   r   r   r   exists  s    zAttr.existsc                 C   s   t | S )z7Creates a condition where the attribute does not exist.)rS   r   r   r   r   
not_exists  s    zAttr.not_existsc                 C   s
   t | |S )z}Creates a condition where the attribute contains the value.

        :param value: The value the attribute contains.
        )rK   r.   r   r   r   rL     s    zAttr.containsc                 C   s   t | S )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.
        )rM   r   r   r   r   rN     s    z	Attr.sizec                 C   s
   t | |S )zfCreates a condition for the attribute type.

        :param value: The type of the attribute.
        )rP   r.   r   r   r   rQ     s    zAttr.attribute_typeN)r'   r(   r)   r@   rW   rX   rY   rZ   rL   rN   rQ   r   r   r   r   rV      s   rV   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   s   d| j  t| j S )N#)rd   strrb   r   r   r   r   _get_name_placeholder:  s    z0ConditionExpressionBuilder._get_name_placeholderc                 C   s   d| j  t| j S )N:)re   rg   rc   r   r   r   r   _get_value_placeholder=  s    z1ConditionExpressionBuilder._get_value_placeholderc                 C   s   d| _ d| _dS )z&Resets the placeholder name and valuesr   N)rb   rc   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_condition)r\   r]   r^   )r   r   r   _build_expressionr[   )r   	conditionrl   r]   r^   r\   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_componentr*   appendr   )	r   rn   r]   r^   rl   Zexpression_dictZreplaced_valuesr/   Zreplaced_valuer   r   r   rm   h  s     z,ConditionExpressionBuilder._build_expressionc                 C   sl   t |tr| ||||S t |trZ|rNt |tsNtd|j dt| d| ||S | 	|||S d S )NzAttribute object z is of type zD. KeyConditionExpression only supports Attribute objects of type Key)
r   r   rm   r+   rU   r   r,   r#   _build_name_placeholder_build_value_placeholder)r   r/   r]   r^   r*   rl   r   r   r   rp     s*    

	
   z6ConditionExpressionBuilder._build_expression_componentc           	      C   s^   |j }t|}td|}g }|D ],}|  }|  jd7  _|| |||< q$|t| S )Nz%s   )r,   ATTR_NAME_REGEXfindallsubrh   rb   rq   tuple)	r   r/   r]   Zattribute_nameZattribute_name_partsZplaceholder_formatZstr_format_argspartZname_placeholderr   r   r   rr     s    


z2ConditionExpressionBuilder._build_name_placeholderc                 C   sr   |rLg }|D ],}|   }|  jd7  _|| |||< qdd| d S |   }|  jd7  _|||< |S d S )Nrt   (z, ))rj   rc   rq   join)r   r/   r^   r*   Zplaceholder_listra   Zvalue_placeholderr   r   r   rs     s    

z3ConditionExpressionBuilder._build_value_placeholderN)F)F)r'   r(   r)   r@   r   rh   rj   rk   ro   rm   rp   rr   rs   r   r   r   r   r_   1  s   
#% r_   )"recollectionsr   Zboto3.exceptionsr   r   r   compileru   r   r+   r?   rA   r1   rD   r3   r5   r7   r9   rG   r=   r;   rK   rM   rP   rR   rS   r   r   r   rU   rV   r[   r_   r   r   r   r   <module>   sD   
'I1
