o ckF[@s|ddlmZddlmZddlmZddlmZddlm Z m Z ddl m Z m Z mZmZGdddeZGd d d eZd S) )Layer1)Table)Schema)Item) BatchListBatchWriteList)get_dynamodb_type DynamizerLossyFloatDynamizerNonBooleanDynamizerc@sXeZdZdZddZeddZeddZedd Zed d Z d d Z ddZ dS)TableGeneratora This is an object that wraps up the table_generator function. The only real reason to have this is that we want to be able to accumulate and return the ConsumedCapacityUnits element that is part of each response. :ivar last_evaluated_key: A sequence representing the key(s) of the item last evaluated, or None if no additional results are available. :ivar remaining: The remaining quantity of results requested. :ivar table: The table to which the call was made. cCsL||_||_|dur dn||_||_||_d|_d|_d|_d|_d|_ dS)Nr) tablecallable remaining item_classkwargs_consumed_unitslast_evaluated_key_count_scanned_count _response)selfrrrrrr6/usr/lib/python3/dist-packages/boto/dynamodb/layer2.py__init__0s zTableGenerator.__init__cC |j|jS)z The total number of items retrieved thus far. This value changes with iteration and even when issuing a call with count=True, it is necessary to complete the iteration to assert an accurate count value. )responserrrrrcount<szTableGenerator.countcCr)z As above, but representing the total number of items scanned by DynamoDB, without regard to any filters. )rrrrrr scanned_countFszTableGenerator.scanned_countcCr)zU Returns a float representing the ConsumedCapacityUnits accumulated. )rrrrrrconsumed_unitsOszTableGenerator.consumed_unitscCs|jdur |S|jS)zA The current response to the call from DynamoDB. N)r next_responserrrrrWszTableGenerator.responsecCs|jd}|jdkr|dus||jkr|j|jd<|jd i|j|_||jd<|j|jdd7_|j|jdd7_|j|jdd7_d|jvry|jd}|jj |}||jd <|d f}d |vrs||d f7}||_ |jSd|_ |jS) z Issue a call and return the result. You can invoke this method while iterating over the TableGenerator in order to skip to the next "page" of results. limitrNConsumedCapacityUnitsrCount ScannedCountLastEvaluatedKeyexclusive_start_keyHashKeyElementRangeKeyElementr) rgetrrrrrrrlayer2dynamize_last_evaluated_keyr)rr$lekesklektuplerrrr#^s&       zTableGenerator.next_responseccs|jdkrD|j}|dgD] }|jd8_|j|j|dV|jdkr(n||jur/n q|jdur:|qdS||jurBqdSdS)NrItems)attrs)rrr,rrrrr#)rritemrrr__iter__{s&     zTableGenerator.__iter__N) __name__ __module__ __qualname____doc__rpropertyr r!r"rr#r6rrrrr s     r c @s@eZdZddddddddddedf ddZd;ddZd d Zd d Zd dZddZ ddZ ddZ dLayer2NTrc Cs,t||||||||| | | d |_| |_dS)N)validate_certs profile_name)rlayer1 dynamizer) raws_access_key_idaws_secret_access_key is_secureportproxy proxy_portdebugsecurity_tokenregionr=r@r>rrrrs zLayer2.__init__FcCs|rt|_dSt|_dS)a Use the ``decimal.Decimal`` type for encoding/decoding numeric types. By default, ints/floats are used to represent numeric types ('N', 'NS') received from DynamoDB. Using the ``Decimal`` type is recommended to prevent loss of precision. N)r r r@)r use_booleanrrr use_decimalss zLayer2.use_decimalscCsJi}|D]}||\}}|durd|i||<q||j|d||<q|S)zf Convert a set of pending item updates into the structure required by Layer1. NAction)rLValuer@encode)rpending_updatesd attr_nameactionvaluerrrdynamize_attribute_updatess   z!Layer2.dynamize_attribute_updatescCs&i}|D] }|j||||<q|SNrN)rr5rQrRrrr dynamize_itemszLayer2.dynamize_itemcCs|S)zo Convert a layer2 range_key_condition parameter into the structure required by Layer1. to_dict)rrange_key_conditionrrrdynamize_range_key_conditionz#Layer2.dynamize_range_key_conditioncCs.d}|ri}|D] }||}|||<q|S)zg Convert a layer2 scan_filter parameter into the structure required by Layer1. NrX)r scan_filterrQrR conditionrrrdynamize_scan_filterszLayer2.dynamize_scan_filtercCsfd}|r1i}|D](}||}|durddi}n|dur ddi}n |j||}d|i}|||<q|S)zj Convert an expected_value parameter into the data structure required for Layer1. NTExistsFrMrN)rexpected_valuerQrR attr_valuevalrrrdynamize_expected_values   zLayer2.dynamize_expected_valuecCsDd}|r |d}d|j|i}d|vr |d}|j||d<|S)zm Convert a last_evaluated_key parameter into the data structure required for Layer1. Nr*r+rN)rrrQhash_key range_keyrrrr.sz"Layer2.dynamize_last_evaluated_keycCsi}|j|}t|d|jkrd|j}t|||d<|durB|j|}t|d|jkr>d|j}t|||d<|S)ax Build a Key structure to be used for accessing items in Amazon DynamoDB. This method takes the supplied hash_key and optional range_key and validates them against the schema. If there is a mismatch, a TypeError is raised. Otherwise, a Python dict version of a Amazon DynamoDB Key data structure is returned. :type hash_key: int|float|str|unicode|Binary :param hash_key: The hash key of the item you are looking for. The type of the hash key should match the type defined in the schema. :type range_key: int|float|str|unicode|Binary :param range_key: The range key of the item your are looking for. This should be supplied only if the schema requires a range key. The type of the range key should match the type defined in the schema. rzHashkey must be of type: %sr*NzRangeKey must be of type: %sr+)r@rOlistkeys hash_key_type TypeErrorrange_key_type)rschemarerf dynamodb_keydynamodb_valuemsgrrrbuild_key_from_valuess    zLayer2.build_key_from_valuescCt|S)z\ Return a new, empty :class:`boto.dynamodb.batch.BatchList` object. )rrrrrnew_batch_listr\zLayer2.new_batch_listcCrq)za Return a new, empty :class:`boto.dynamodb.batch.BatchWriteList` object. )rrrrrnew_batch_write_listr\zLayer2.new_batch_write_listcCsg}d}|r t||kr?d}|r|t|}t|d}|jj||d}||dg|dd}|s7 |S|r t||ks |S)z Return a list of the names of all tables associated with the current account and region. :type limit: int :param limit: The maximum number of tables to return. Nd)r$ start_table TableNamesLastEvaluatedTableName)lenminr? list_tablesextendr,)rr$tablesruthis_round_limitresultrrrrz%s    zLayer2.list_tablescCs |j|S)z Retrieve information about an existing table. :type name: str :param name: The name of the desired table. )r?describe_table)rnamerrrr;s zLayer2.describe_tablecCst|||S)a Create a Table object from a schema. This method will create a Table object without making any API calls. If you know the name and schema of the table, you can use this method instead of ``get_table``. Example usage:: table = layer2.table_from_schema( 'tablename', Schema.create(hash_key=('foo', 'N'))) :type name: str :param name: The name of the table. :type schema: :class:`boto.dynamodb.schema.Schema` :param schema: The schema associated with the table. :rtype: :class:`boto.dynamodb.table.Table` :return: A Table object representing the table. )rcreate_from_schema)rrrlrrrtable_from_schemaEszLayer2.table_from_schemacCs|j|}t||S)z Retrieve the Table object for an existing table. :type name: str :param name: The name of the desired table. :rtype: :class:`boto.dynamodb.table.Table` :return: A Table object representing the table. )r?rr)rrrrrr get_table`s zLayer2.get_tablecCs"|j||j||d}t||S)aX Create a new Amazon DynamoDB table. :type name: str :param name: The name of the desired table. :type schema: :class:`boto.dynamodb.schema.Schema` :param schema: The Schema object that defines the schema used by this table. :type read_units: int :param read_units: The value for ReadCapacityUnits. :type write_units: int :param write_units: The value for WriteCapacityUnits. :rtype: :class:`boto.dynamodb.table.Table` :return: A Table object representing the new Amazon DynamoDB table. ReadCapacityUnitsWriteCapacityUnits)r? create_tabledictr)rrrl read_units write_unitsrrrrros  zLayer2.create_tablecCs$|j|j||d}||dS)a Update the ProvisionedThroughput for the Amazon DynamoDB Table. :type table: :class:`boto.dynamodb.table.Table` :param table: The Table object whose throughput is being updated. :type read_units: int :param read_units: The new value for ReadCapacityUnits. :type write_units: int :param write_units: The new value for WriteCapacityUnits. rN)r? update_tablerupdate_from_response)rrrrrrrrupdate_throughputs zLayer2.update_throughputcCs|j|j}||dS)a  Delete this table and all items in it. After calling this the Table objects status attribute will be set to 'DELETING'. :type table: :class:`boto.dynamodb.table.Table` :param table: The Table object that is being deleted. N)r? delete_tablerr)rrrrrrrszLayer2.delete_tablecCs6|t|f}|r|dur|t|f}nd}t||S)a Create a Schema object used when creating a Table. :type hash_key_name: str :param hash_key_name: The name of the HashKey for the schema. :type hash_key_proto_value: int|long|float|str|unicode|Binary :param hash_key_proto_value: A sample or prototype of the type of value you want to use for the HashKey. Alternatively, you can also just pass in the Python type (e.g. int, float, etc.). :type range_key_name: str :param range_key_name: The name of the RangeKey for the schema. This parameter is optional. :type range_key_proto_value: int|long|float|str|unicode|Binary :param range_key_proto_value: A sample or prototype of the type of value you want to use for the RangeKey. Alternatively, you can also pass in the Python type (e.g. int, float, etc.) This parameter is optional. N)rrcreate)r hash_key_namehash_key_proto_valuerange_key_namerange_key_proto_valuererfrrr create_schemas   zLayer2.create_schemac CsT||j||}|jj|j||||jjd}|||||d} d|vr(|d| _| S)a Retrieve an existing item from the table. :type table: :class:`boto.dynamodb.table.Table` :param table: The Table object from which the item is retrieved. :type hash_key: int|long|float|str|unicode|Binary :param hash_key: The HashKey of the requested item. The type of the value must match the type defined in the schema for the table. :type range_key: int|long|float|str|unicode|Binary :param range_key: The optional RangeKey of the requested item. The type of the value must match the type defined in the schema for the table. :type attributes_to_get: list :param attributes_to_get: A list of attribute names. If supplied, only the specified attribute names will be returned. Otherwise, all attributes will be returned. :type consistent_read: bool :param consistent_read: If True, a consistent read request is issued. Otherwise, an eventually consistent request is issued. :type item_class: Class :param item_class: Allows you to override the class used to generate the items. This should be a subclass of :class:`boto.dynamodb.item.Item`  object_hookrr%)rprlr?get_itemrr@decoder") rrrerfattributes_to_getconsistent_readrkeyrr5rrrrs"  zLayer2.get_itemcC|}|jj||jjdS)a Return a set of attributes for a multiple items in multiple tables using their primary keys. :type batch_list: :class:`boto.dynamodb.batch.BatchList` :param batch_list: A BatchList object which consists of a list of :class:`boto.dynamoddb.batch.Batch` objects. Each Batch object contains the information about one batch of objects that you wish to retrieve in this request. r)rYr?batch_get_itemr@rr batch_list request_itemsrrrrs zLayer2.batch_get_itemcCr)a Performs multiple Puts and Deletes in one batch. :type batch_list: :class:`boto.dynamodb.batch.BatchWriteList` :param batch_list: A BatchWriteList object which consists of a list of :class:`boto.dynamoddb.batch.BatchWrite` objects. Each Batch object contains the information about one batch of objects that you wish to put or delete. r)rYr?batch_write_itemr@rrrrrrs zLayer2.batch_write_itemcCsD||}|jj|jj|||||jjd}d|vr |d|_|S)a Store a new item or completely replace an existing item in Amazon DynamoDB. :type item: :class:`boto.dynamodb.item.Item` :param item: The Item to write to Amazon DynamoDB. :type expected_value: dict :param expected_value: A dictionary of name/value pairs that you expect. This dictionary should have name/value pairs where the name is the name of the attribute and the value is either the value you are expecting or False if you expect the attribute not to exist. :type return_values: str :param return_values: Controls the return of attribute name-value pairs before then were changed. Possible values are: None or 'ALL_OLD'. If 'ALL_OLD' is specified and the item is overwritten, the content of the old item is returned. rr%) rdr?put_itemrrrWr@rr")rr5ra return_valuesrrrrr s   zLayer2.put_itemcCsl||}||jj|j|j}||j}|jj |jj |||||j j d}|j d|vr4|d|_|S)aS Commit pending item updates to Amazon DynamoDB. :type item: :class:`boto.dynamodb.item.Item` :param item: The Item to update in Amazon DynamoDB. It is expected that you would have called the add_attribute, put_attribute and/or delete_attribute methods on this Item prior to calling this method. Those queued changes are what will be updated. :type expected_value: dict :param expected_value: A dictionary of name/value pairs that you expect. This dictionary should have name/value pairs where the name is the name of the attribute and the value is either the value you are expecting or False if you expect the attribute not to exist. :type return_values: str :param return_values: Controls the return of attribute name/value pairs before they were updated. Possible values are: None, 'ALL_OLD', 'UPDATED_OLD', 'ALL_NEW' or 'UPDATED_NEW'. If 'ALL_OLD' is specified and the item is overwritten, the content of the old item is returned. If 'ALL_NEW' is specified, then all the attributes of the new version of the item are returned. If 'UPDATED_NEW' is specified, the new versions of only the updated attributes are returned. rr%)rdrprrlrerfrU_updatesr? update_itemrr@rclearr")rr5rarr attr_updatesrrrrr,s     zLayer2.update_itemcCs>||}||jj|j|j}|jj|jj||||j j dS)aU Delete the item from Amazon DynamoDB. :type item: :class:`boto.dynamodb.item.Item` :param item: The Item to delete from Amazon DynamoDB. :type expected_value: dict :param expected_value: A dictionary of name/value pairs that you expect. This dictionary should have name/value pairs where the name is the name of the attribute and the value is either the value you are expecting or False if you expect the attribute not to exist. :type return_values: str :param return_values: Controls the return of attribute name-value pairs before then were changed. Possible values are: None or 'ALL_OLD'. If 'ALL_OLD' is specified and the item is overwritten, the content of the old item is returned. )expectedrr) rdrprrlrerfr? delete_itemrr@r)rr5rarrrrrrVs  zLayer2.delete_itemc  Csn|r||} nd} | r|j|jg| R} nd} |j|j|| ||| ||| |jjd }t||jj || |S)a5 Perform a query on the table. :type table: :class:`boto.dynamodb.table.Table` :param table: The Table object that is being queried. :type hash_key: int|long|float|str|unicode|Binary :param hash_key: The HashKey of the requested item. The type of the value must match the type defined in the schema for the table. :type range_key_condition: :class:`boto.dynamodb.condition.Condition` :param range_key_condition: A Condition object. Condition object can be one of the following types: EQ|LE|LT|GE|GT|BEGINS_WITH|BETWEEN The only condition which expects or will accept two values is 'BETWEEN', otherwise a single value should be passed to the Condition constructor. :type attributes_to_get: list :param attributes_to_get: A list of attribute names. If supplied, only the specified attribute names will be returned. Otherwise, all attributes will be returned. :type request_limit: int :param request_limit: The maximum number of items to retrieve from Amazon DynamoDB on each request. You may want to set a specific request_limit based on the provisioned throughput of your table. The default behavior is to retrieve as many results as possible per request. :type max_results: int :param max_results: The maximum number of results that will be retrieved from Amazon DynamoDB in total. For example, if you only wanted to see the first 100 results from the query, regardless of how many were actually available, you could set max_results to 100 and the generator returned from the query method will only yeild 100 results max. :type consistent_read: bool :param consistent_read: If True, a consistent read request is issued. Otherwise, an eventually consistent request is issued. :type scan_index_forward: bool :param scan_index_forward: Specified forward or backward traversal of the index. Default is forward (True). :type count: bool :param count: If True, Amazon DynamoDB returns a total number of items for the Query operation, even if the operation has no matching items for the assigned filter. If count is True, the actual items are not returned and the count is accessible as the ``count`` attribute of the returned object. :type exclusive_start_key: list or tuple :param exclusive_start_key: Primary key of the item from which to continue an earlier query. This would be provided as the LastEvaluatedKey in that query. :type item_class: Class :param item_class: Allows you to override the class used to generate the items. This should be a subclass of :class:`boto.dynamodb.item.Item` :rtype: :class:`boto.dynamodb.layer2.TableGenerator` N) table_namehash_key_valuerange_key_conditionsrr$r rscan_index_forwardr)r) r[rprlrr@rOrr r?query)rrrerZr request_limit max_resultsrrr)rr rkcr0rrrrrss,K     z Layer2.queryc CsR|r |j|jg|R} nd} |j|||||| |jjd} t||jj||| S)a Perform a scan of DynamoDB. :type table: :class:`boto.dynamodb.table.Table` :param table: The Table object that is being scanned. :type scan_filter: A dict :param scan_filter: A dictionary where the key is the attribute name and the value is a :class:`boto.dynamodb.condition.Condition` object. Valid Condition objects include: * EQ - equal (1) * NE - not equal (1) * LE - less than or equal (1) * LT - less than (1) * GE - greater than or equal (1) * GT - greater than (1) * NOT_NULL - attribute exists (0, use None) * NULL - attribute does not exist (0, use None) * CONTAINS - substring or value in list (1) * NOT_CONTAINS - absence of substring or value in list (1) * BEGINS_WITH - substring prefix (1) * IN - exact match in list (N) * BETWEEN - >= first value, <= second value (2) :type attributes_to_get: list :param attributes_to_get: A list of attribute names. If supplied, only the specified attribute names will be returned. Otherwise, all attributes will be returned. :type request_limit: int :param request_limit: The maximum number of items to retrieve from Amazon DynamoDB on each request. You may want to set a specific request_limit based on the provisioned throughput of your table. The default behavior is to retrieve as many results as possible per request. :type max_results: int :param max_results: The maximum number of results that will be retrieved from Amazon DynamoDB in total. For example, if you only wanted to see the first 100 results from the query, regardless of how many were actually available, you could set max_results to 100 and the generator returned from the query method will only yeild 100 results max. :type count: bool :param count: If True, Amazon DynamoDB returns a total number of items for the Scan operation, even if the operation has no matching items for the assigned filter. If count is True, the actual items are not returned and the count is accessible as the ``count`` attribute of the returned object. :type exclusive_start_key: list or tuple :param exclusive_start_key: Primary key of the item from which to continue an earlier query. This would be provided as the LastEvaluatedKey in that query. :type item_class: Class :param item_class: Allows you to override the class used to generate the items. This should be a subclass of :class:`boto.dynamodb.item.Item` :rtype: :class:`boto.dynamodb.layer2.TableGenerator` N)rr]rr$r r)r) rprlrr_r@rr r?scan) rrr]rrrr)rr r0rrrrrs E   z Layer2.scan)FrV)NN)!r7r8r9r rrKrUrWr[r_rdr.rprrrsrzrrrlookuprrrrrrrrrrrrrrrrrr<s\    "     +   * ar<N)boto.dynamodb.layer1rboto.dynamodb.tablerboto.dynamodb.schemarboto.dynamodb.itemrboto.dynamodb.batchrrboto.dynamodb.typesrr r r objectr r<rrrrs    o