U C^:@sddlmZddlZddlmZddlmZddlmZedZGddde Z Gd d d e Z Gd d d e e Z Gd dde Z Gddde ZGddde ZGddde ZGddde ZGddde ZGddde ZGddde ZGddde ZGdd d e ZGd!d"d"e ZGd#d$d$e ZGd%d&d&e ZGd'd(d(e ZGd)d*d*e ZGd+d,d,e ZGd-d.d.e ZGd/d0d0e ZGd1d2d2e ZGd3d4d4e Z ed5d6d7d8gZ!Gd9d:d:e Z"dS);) namedtupleN"DynamoDBOperationNotSupportedError)DynamoDBNeedsConditionError)DynamoDBNeedsKeyConditionErrorz[^.\[\]]+(?![^\[]*\])c@sPeZdZdZdZdZddZddZddZd d Z d d Z d dZ ddZ dS) ConditionBaseFcGs ||_dSN)_valuesselfvaluesrszAttributeBase.__init__cCstd|dSrrr valuerrrrAszAttributeBase.__and__cCstd|dSrrr.rrrrDszAttributeBase.__or__cCstd|dS)NNOTrrrrrrGszAttributeBase.__invert__cCs t||S)zCreates a condition where the attribute is equal to the value. :param value: The value that the attribute is equal to. )Equalsr.rrreqJszAttributeBase.eqcCs t||S)zCreates a condition where the attribute is less than the value. :param value: The value that the attribute is less than. )LessThanr.rrrltQszAttributeBase.ltcCs 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.rrrlteXszAttributeBase.ltecCs t||S)zCreates a condition where the attribute is greater than the value. :param value: The value that the attribute is greater than. ) GreaterThanr.rrrgt`szAttributeBase.gtcCs 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.rrrgtegszAttributeBase.gtecCs t||S)zCreates a condition where the attribute begins with the value. :param value: The value that the attribute begins with. ) BeginsWithr.rrr begins_withoszAttributeBase.begins_withcCs t|||S)aCreates 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. :param high_value: The value that the attribute is less than. )Between)r Z low_valueZ high_valuerrrbetweenvszAttributeBase.betweencCst|t|o|j|jkSr )rr$r-rrrrr%szAttributeBase.__eq__cCs || Sr r&rrrrr'szAttributeBase.__ne__N)r(r)r*rrrrr2r4r6r8r:r<r>r%r'rrrrr,=s r,c@s(eZdZdZddZddZddZdS) 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(). cGs&tj|f|t||djdS)Nr)rrr,r-r rrrrszConditionAttributeBase.__init__cCst||ot||Sr )rr%r,rrrrr%s  zConditionAttributeBase.__eq__cCs || Sr r&rrrrr'szConditionAttributeBase.__ne__N)r(r)r*__doc__rr%r'rrrrr?sr?c@seZdZdZdS)ComparisonConditionz{0} {operator} {1}N)r(r)r*r!rrrrrAsrAc@seZdZdZdS)r1=Nr(r)r*r"rrrrr1sr1c@seZdZdZdS) NotEqualsz<>NrCrrrrrDsrDc@seZdZdZdS)r3NrCrrrrr7sr7c@seZdZdZdS)r9z>=NrCrrrrr9sr9c@seZdZdZdZdS)InINTN)r(r)r*r"r+rrrrrGsrGc@seZdZdZdZdS)r=ZBETWEENz{0} {operator} {1} AND {2}Nr(r)r*r"r!rrrrr=sr=c@seZdZdZdZdS)r;r<{operator}({0}, {1})NrIrrrrr;sr;c@seZdZdZdZdS)ContainscontainsrJNrIrrrrrKsrKc@seZdZdZdZdS)Sizesize{operator}({0})NrIrrrrrMsrMc@seZdZdZdZdS) AttributeTypeattribute_typerJNrIrrrrrPsrPc@seZdZdZdZdS)AttributeExistsZattribute_existsrONrIrrrrrRsrRc@seZdZdZdZdS)AttributeNotExistsZattribute_not_existsrONrIrrrrrSsrSc@seZdZdZdZdS)rr({0} {operator} {1})NrIrrrrrsrc@seZdZdZdZdS)rrrTNrIrrrrrsrc@seZdZdZdZdS)rr0z({operator} {0})NrIrrrrrsrc@s eZdZdS)KeyN)r(r)r*rrrrrUsrUc@sHeZdZdZddZddZddZdd Zd d Zd d Z ddZ dS)Attrz(Represents an DynamoDB item's attribute.cCs 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. )rDr.rrrneszAttr.necCs t||S)zCreates a condition where the attribute is in the value, :type value: list :param value: The value that the attribute is in. )rGr.rrris_insz Attr.is_incCst|S)z/Creates a condition where the attribute exists.)rRrrrrexistssz Attr.existscCst|S)z7Creates a condition where the attribute does not exist.)rSrrrr not_existsszAttr.not_existscCs t||S)z}Creates a condition where the attribute contains the value. :param value: The value the attribute contains. )rKr.rrrrL sz Attr.containscCst|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. )rMrrrrrNsz Attr.sizecCs t||S)zfCreates a condition for the attribute type. :param value: The type of the attribute. )rPr.rrrrQszAttr.attribute_typeN) r(r)r*r@rWrXrYrZrLrNrQrrrrrVsrVBuiltConditionExpressioncondition_expressionattribute_name_placeholdersattribute_value_placeholdersc@s\eZdZdZddZddZddZdd Zdd d Zd dZ ddZ ddZ dddZ dS)ConditionExpressionBuilderzCThis class is used to build condition expressions with placeholderscCsd|_d|_d|_d|_dS)Nrnv) _name_count _value_count_name_placeholder_value_placeholderrrrrr*sz#ConditionExpressionBuilder.__init__cCsd|jt|jS)N#)rdstrrbrrrr_get_name_placeholder0sz0ConditionExpressionBuilder._get_name_placeholdercCsd|jt|jS)N:)rergrcrrrr_get_value_placeholder3sz1ConditionExpressionBuilder._get_value_placeholdercCsd|_d|_dS)z&Resets the placeholder name and valuesrN)rbrcrrrrreset6sz ConditionExpressionBuilder.resetFcCs:t|tst|i}i}|j||||d}t|||dS)aBuilds 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^)rrr_build_expressionr[)r conditionrlr]r^r\rrrbuild_expression;s z+ConditionExpressionBuilder.build_expressionc CsP|}g}|dD]"}|||||j|}||q|dj|d|diS)Nr rr )r#_build_expression_componentr+appendr) r rnr]r^rlZexpression_dictZreplaced_valuesr/Zreplaced_valuerrrrm[s   z,ConditionExpressionBuilder._build_expressioncCsft|tr|||||St|trT|rHt|tsHtd|jt|f|||S| |||SdS)NzeAttribute object %s is of type %s. KeyConditionExpression only supports Attribute objects of type Key) rrrmr,rUrr-r$_build_name_placeholder_build_value_placeholder)r r/r]r^r+rlrrrrpls.   z6ConditionExpressionBuilder._build_expression_componentc Cs^|j}t|}td|}g}|D],}|}|jd7_|||||<q$|t|S)Nz%s)r-ATTR_NAME_REGEXfindallsubrhrbrqtuple) r r/r]Zattribute_nameZattribute_name_partsZplaceholder_formatZstr_format_argspartZname_placeholderrrrrrs    z2ConditionExpressionBuilder._build_name_placeholdercCsr|rLg}|D],}|}|jd7_|||||<q dd|dS|}|jd7_|||<|SdS)Nrt(z, ))rjrcrqjoin)r r/r^r+Zplaceholder_listraZvalue_placeholderrrrrss  z3ConditionExpressionBuilder._build_value_placeholderN)F)F) r(r)r*r@rrhrjrkrormrprrrsrrrrr_(s r_)# collectionsrreZboto3.exceptionsrrrcompileruobjectrr,r?rAr1rDr3r5r7r9rGr=r;rKrMrPrRrSrrrrUrVr[r_rrrr sF     %I0