<s> from collections import namedtuple <EOL> import functools <EOL> import re <EOL> from boto3 . exceptions import DynamoDBOperationNotSupportedError <EOL> from boto3 . exceptions import DynamoDBNeedsConditionError <EOL> from boto3 . exceptions import DynamoDBNeedsKeyConditionError <EOL> ATTR_NAME_REGEX = re . compile ( r'<STR_LIT>' ) <EOL> class ConditionBase ( object ) : <EOL> expression_format = '<STR_LIT>' <EOL> expression_operator = '<STR_LIT>' <EOL> has_grouped_values = False <EOL> def __init__ ( self , * values ) : <EOL> self . _values = values <EOL> def __and__ ( self , other ) : <EOL> if not isinstance ( other , ConditionBase ) : <EOL> raise DynamoDBOperationNotSupportedError ( '<STR_LIT>' , other ) <EOL> return And ( self , other ) <EOL> def __or__ ( self , other ) : <EOL> if not isinstance ( other , ConditionBase ) : <EOL> raise DynamoDBOperationNotSupportedError ( '<STR_LIT>' , other ) <EOL> return Or ( self , other ) <EOL> def __invert__ ( self ) : <EOL> return Not ( self ) <EOL> def get_expression ( self ) : <EOL> return { '<STR_LIT>' : self . expression_format , <EOL> '<STR_LIT>' : self . expression_operator , <EOL> '<STR_LIT>' : self . _values } <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , type ( self ) ) : <EOL> if self . _values == other . _values : <EOL> return True <EOL> return False <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> class AttributeBase ( object ) : <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def __and__ ( self , value ) : <EOL> raise DynamoDBOperationNotSupportedError ( '<STR_LIT>' , self ) <EOL> def __or__ ( self , value ) : <EOL> raise DynamoDBOperationNotSupportedError ( '<STR_LIT>' , self ) <EOL> def __invert__ ( self ) : <EOL> raise DynamoDBOperationNotSupportedError ( '<STR_LIT>' , self ) <EOL> def eq ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return Equals ( self , value ) <EOL> def lt ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return LessThan ( self , value ) <EOL> def lte ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return LessThanEquals ( self , value ) <EOL> def gt ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return GreaterThan ( self , value ) <EOL> def gte ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return GreaterThanEquals ( self , value ) <EOL> def begins_with ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return BeginsWith ( self , value ) <EOL> def between ( self , low_value , high_value ) : <EOL> """<STR_LIT>""" <EOL> return Between ( self , low_value , high_value ) <EOL> class ConditionAttributeBase ( ConditionBase , AttributeBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * values ) : <EOL> ConditionBase . __init__ ( self , * values ) <EOL> AttributeBase . __init__ ( self , values [ <NUM_LIT:0> ] . name ) <EOL> class ComparisonCondition ( ConditionBase ) : <EOL> expression_format = '<STR_LIT>' <EOL> class Equals ( ComparisonCondition ) : <EOL> expression_operator = '<STR_LIT:=>' <EOL> class NotEquals ( ComparisonCondition ) : <EOL> expression_operator = '<STR_LIT>' <EOL> class LessThan ( ComparisonCondition ) : <EOL> expression_operator = '<STR_LIT:<>' <EOL> class LessThanEquals ( ComparisonCondition ) : <EOL> expression_operator = '<STR_LIT>' <EOL> class GreaterThan ( ComparisonCondition ) : <EOL> expression_operator = '<STR_LIT:>>' <EOL> class GreaterThanEquals ( ComparisonCondition ) : <EOL> expression_operator = '<STR_LIT>' <EOL> class In ( ComparisonCondition ) : <EOL> expression_operator = '<STR_LIT>' <EOL> has_grouped_values = True <EOL> class Between ( ConditionBase ) : <EOL> expression_operator = '<STR_LIT>' <EOL> expression_format = '<STR_LIT>' <EOL> class BeginsWith ( ConditionBase ) : <EOL> expression_operator = '<STR_LIT>' <EOL> expression_format = '<STR_LIT>' <EOL> class Contains ( ConditionBase ) : <EOL> expression_operator = '<STR_LIT>' <EOL> expression_format = '<STR_LIT>' <EOL> class Size ( ConditionAttributeBase ) : <EOL> expression_operator = '<STR_LIT:size>' <EOL> expression_format = '<STR_LIT>' <EOL> class AttributeType ( ConditionBase ) : <EOL> expression_operator = '<STR_LIT>' <EOL> expression_format = '<STR_LIT>' <EOL> class AttributeExists ( ConditionBase ) : <EOL> expression_operator = '<STR_LIT>' <EOL> expression_format = '<STR_LIT>' <EOL> class AttributeNotExists ( ConditionBase ) : <EOL> expression_operator = '<STR_LIT>' <EOL> expression_format = '<STR_LIT>' <EOL> class And ( ConditionBase ) : <EOL> expression_operator = '<STR_LIT>' <EOL> expression_format = '<STR_LIT>' <EOL> class Or ( ConditionBase ) : <EOL> expression_operator = '<STR_LIT>' <EOL> expression_format = '<STR_LIT>' <EOL> class Not ( ConditionBase ) : <EOL> expression_operator = '<STR_LIT>' <EOL> expression_format = '<STR_LIT>' <EOL> class Key ( AttributeBase ) : <EOL> pass <EOL> class Attr ( AttributeBase ) : <EOL> """<STR_LIT>""" <EOL> def ne ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return NotEquals ( self , value ) <EOL> def is_in ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return In ( self , value ) <EOL> def exists ( self ) : <EOL> """<STR_LIT>""" <EOL> return AttributeExists ( self ) <EOL> def not_exists ( self ) : <EOL> """<STR_LIT>""" <EOL> return AttributeNotExists ( self ) <EOL> def contains ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return Contains ( self , value ) <EOL> def size ( self ) : <EOL> """<STR_LIT>""" <EOL> return Size ( self ) <EOL> def attribute_type ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return AttributeType ( self , value ) <EOL> BuiltConditionExpression = namedtuple ( <EOL> '<STR_LIT>' , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> ) <EOL> class ConditionExpressionBuilder ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _name_count = <NUM_LIT:0> <EOL> self . _value_count = <NUM_LIT:0> <EOL> self . _name_placeholder = '<STR_LIT:n>' <EOL> self . _value_placeholder = '<STR_LIT:v>' <EOL> def _get_name_placeholder ( self ) : <EOL> return '<STR_LIT:#>' + self . _name_placeholder + str ( self . _name_count ) <EOL> def _get_value_placeholder ( self ) : <EOL> return '<STR_LIT::>' + self . _value_placeholder + str ( self . _value_count ) <EOL> def reset ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _name_count = <NUM_LIT:0> <EOL> self . _value_count = <NUM_LIT:0> <EOL> def build_expression ( self , condition , is_key_condition = False ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( condition , ConditionBase ) : <EOL> raise DynamoDBNeedsConditionError ( condition ) <EOL> attribute_name_placeholders = { } <EOL> attribute_value_placeholders = { } <EOL> condition_expression = self . _build_expression ( <EOL> condition , attribute_name_placeholders , <EOL> attribute_value_placeholders , is_key_condition = is_key_condition ) <EOL> return BuiltConditionExpression ( <EOL> condition_expression = condition_expression , <EOL> attribute_name_placeholders = attribute_name_placeholders , <EOL> attribute_value_placeholders = attribute_value_placeholders <EOL> ) <EOL> def _build_expression ( self , condition , attribute_name_placeholders , <EOL> attribute_value_placeholders , is_key_condition ) : <EOL> expression_dict = condition . get_expression ( ) <EOL> replaced_values = [ ] <EOL> for value in expression_dict [ '<STR_LIT>' ] : <EOL> replaced_value = self . _build_expression_component ( <EOL> value , attribute_name_placeholders , <EOL> attribute_value_placeholders , condition . has_grouped_values , <EOL> is_key_condition ) <EOL> replaced_values . append ( replaced_value ) <EOL> return expression_dict [ '<STR_LIT>' ] . format ( <EOL> * replaced_values , operator = expression_dict [ '<STR_LIT>' ] ) <EOL> def _build_expression_component ( self , value , attribute_name_placeholders , <EOL> attribute_value_placeholders , <EOL> has_grouped_values , is_key_condition ) : <EOL> if isinstance ( value , ConditionBase ) : <EOL> return self . _build_expression ( <EOL> value , attribute_name_placeholders , <EOL> attribute_value_placeholders , is_key_condition ) <EOL> elif isinstance ( value , AttributeBase ) : <EOL> if is_key_condition and not isinstance ( value , Key ) : <EOL> raise DynamoDBNeedsKeyConditionError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( value . name , type ( value ) ) ) <EOL> return self . _build_name_placeholder ( <EOL> value , attribute_name_placeholders ) <EOL> else : <EOL> return self . _build_value_placeholder ( <EOL> value , attribute_value_placeholders , has_grouped_values ) <EOL> def _build_name_placeholder ( self , value , attribute_name_placeholders ) : <EOL> attribute_name = value . name <EOL> attribute_name_parts = ATTR_NAME_REGEX . findall ( attribute_name ) <EOL> placeholder_format = ATTR_NAME_REGEX . sub ( '<STR_LIT:%s>' , attribute_name ) <EOL> str_format_args = [ ] <EOL> for part in attribute_name_parts : <EOL> name_placeholder = self . _get_name_placeholder ( ) <EOL> self . _name_count += <NUM_LIT:1> <EOL> str_format_args . append ( name_placeholder ) <EOL> attribute_name_placeholders [ name_placeholder ] = part <EOL> return placeholder_format % tuple ( str_format_args ) <EOL> def _build_value_placeholder ( self , value , attribute_value_placeholders , <EOL> has_grouped_values = False ) : <EOL> if has_grouped_values : <EOL> placeholder_list = [ ] <EOL> for v in value : <EOL> value_placeholder = self . _get_value_placeholder ( ) <EOL> self . _value_count += <NUM_LIT:1> <EOL> placeholder_list . append ( value_placeholder ) <EOL> attribute_value_placeholders [ value_placeholder ] = v <EOL> return '<STR_LIT:(>' + '<STR_LIT:U+002CU+0020>' . join ( placeholder_list ) + '<STR_LIT:)>' <EOL> else : <EOL> value_placeholder = self . _get_value_placeholder ( ) <EOL> self . _value_count += <NUM_LIT:1> <EOL> attribute_value_placeholders [ value_placeholder ] = value <EOL> return value_placeholder </s>
