B 劇c*K@sddlZddlZddlmZddlmZddlmZddlm Z ddl m Z dd l m Z eeZGd d d ZGd d d ZGdddZdS)N) xform_name) merge_dicts) docstring) BatchAction)create_request_parameters)ResourceHandlerc@sXeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ dS)ResourceCollectiona Represents a collection of resources, which can be iterated through, optionally with filtering. Collections automatically handle pagination for you. See :ref:`guide_collections` for a high-level overview of collections, including when remote service requests are performed. :type model: :py:class:`~boto3.resources.model.Collection` :param model: Collection model :type parent: :py:class:`~boto3.resources.base.ServiceResource` :param parent: The collection's parent resource :type handler: :py:class:`~boto3.resources.response.ResourceHandler` :param handler: The resource response handler used to create resource instances cKs0||_||_t|jj|_||_t||_ dS)N) _model_parentrrequest operation_py_operation_name_handlercopydeepcopy_params)selfmodelparenthandlerkwargsru/private/var/folders/8c/hx9_v10d5x38qmnzt13b7b8j1k3n5b/T/pip-target-x6xd5gna/lib/python/boto3/resources/collection.py__init__.s zResourceCollection.__init__cCs(d|jj|jd|jjj|jjjS)Nz {}({}, {})z{}.{}) format __class____name__r meta service_namer resourcetype)rrrr__repr__5s zResourceCollection.__repr__ccsX|jdd}d}x@|D]4}x.|D]&}|V|d7}|dk r&||kr&dSq&WqWdS)a A generator which yields resource instances after doing the appropriate service operation calls and handling any pagination on your behalf. Page size, item limit, and filter parameters are applied if they have previously been set. >>> bucket = s3.Bucket('boto3') >>> for obj in bucket.objects.all(): ... print(obj.key) 'key1' 'key2' limitNrr)rgetpages)rr$countpageitemrrr__iter__>s zResourceCollection.__iter__cKs6t|j}t||dd|j|j|j|jf|}|S)aO Create a clone of this collection. This is used by the methods below to provide a chainable interface that returns copies rather than the original. This allows things like: >>> base = collection.filter(Param1=1) >>> query1 = base.filter(Param2=2) >>> query2 = base.filter(Param3=3) >>> query1.params {'Param1': 1, 'Param2': 2} >>> query2.params {'Param1': 1, 'Param3': 3} :rtype: :py:class:`ResourceCollection` :return: A clone of this resource collection T) append_lists)rrrrrr r r)rrparamsclonerrr_clone[s  zResourceCollection._clonec cs2|jjj}|j}|dd}|dd}t|j|jj}t ||dd| |j rt d|jjj|j |||j }|jfd||di|}n,t d |jjj|j |t||j f|g}d }xf|D]^} g} x<||j|| D](} | | |d 7}|dk r||krPqW| V|dk r||krPqWdS) a A generator which yields pages of resource instances after doing the appropriate service operation calls and handling any pagination on your behalf. Non-paginated calls will return a single page of items. Page size, item limit, and filter parameters are applied if they have previously been set. >>> bucket = s3.Bucket('boto3') >>> for page in bucket.objects.pages(): ... for obj in page: ... print(obj.key) 'key1' 'key2' :rtype: list(:py:class:`~boto3.resources.base.ServiceResource`) :return: List of resource instances r$N page_sizeT)r+zCalling paginated %s:%s with %rZPaginationConfig)ZMaxItemsZPageSizezCalling %s:%s with %rrr)r rclientrrpoprr r rZ can_paginaterloggerdebugr Z get_paginatorZpaginategetattrrappend) rr0Zcleaned_paramsr$r/r,Z paginatorr&r'r(Z page_itemsr)rrrr&ssB          zResourceCollection.pagescCs|S)a Get all items from the collection, optionally with a custom page size and item count limit. This method returns an iterable generator which yields individual resource instances. Example use:: # Iterate through items >>> for queue in sqs.queues.all(): ... print(queue.url) 'https://url1' 'https://url2' # Convert to list >>> queues = list(sqs.queues.all()) >>> len(queues) 2 )r.)rrrrallszResourceCollection.allcKs |jf|S)a Get items from the collection, passing keyword arguments along as parameters to the underlying service operation, which are typically used to filter the results. This method returns an iterable generator which yields individual resource instances. Example use:: # Iterate through items >>> for queue in sqs.queues.filter(Param='foo'): ... print(queue.url) 'https://url1' 'https://url2' # Convert to list >>> queues = list(sqs.queues.filter(Param='foo')) >>> len(queues) 2 :rtype: :py:class:`ResourceCollection` )r.)rrrrrfilterszResourceCollection.filtercCs |j|dS)a Return at most this many resources. >>> for bucket in s3.buckets.limit(5): ... print(bucket.name) 'bucket1' 'bucket2' 'bucket3' 'bucket4' 'bucket5' :type count: int :param count: Return no more than this many items :rtype: :py:class:`ResourceCollection` )r$)r.)rr'rrrr$szResourceCollection.limitcCs |j|dS)a3 Fetch at most this many resources per service request. >>> for obj in s3.Bucket('boto3').objects.page_size(100): ... print(obj.key) :type count: int :param count: Fetch this many items per request :rtype: :py:class:`ResourceCollection` )r/)r.)rr'rrrr/s zResourceCollection.page_sizeN) r __module__ __qualname____doc__rr#r*r.r&r6r7r$r/rrrrr s Ir c@seZdZdZeZddZddZddZdd Z ej je _d d Z ej je _d d Z ej je _ddZ ej je _ddZ ej je _dS)CollectionManagera A collection manager provides access to resource collection instances, which can be iterated and filtered. The manager exposes some convenience functions that are also found on resource collections, such as :py:meth:`~ResourceCollection.all` and :py:meth:`~ResourceCollection.filter`. Get all items:: >>> for bucket in s3.buckets.all(): ... print(bucket.name) Get only some items via filtering:: >>> for queue in sqs.queues.filter(QueueNamePrefix='AWS'): ... print(queue.url) Get whole pages of items: >>> for page in s3.Bucket('boto3').objects.pages(): ... for obj in page: ... print(obj.key) A collection manager is not iterable. You **must** call one of the methods that return a :py:class:`ResourceCollection` before trying to iterate, slice, or convert to a list. See the :ref:`guide_collections` guide for a high-level overview of collections, including when remote service requests are performed. :type collection_model: :py:class:`~boto3.resources.model.Collection` :param model: Collection model :type parent: :py:class:`~boto3.resources.base.ServiceResource` :param parent: The collection's parent resource :type factory: :py:class:`~boto3.resources.factory.ResourceFactory` :param factory: The resource factory to create new resources :type service_context: :py:class:`~boto3.utils.ServiceContext` :param service_context: Context about the AWS service cCs8||_|jjj}||_|jj}t|||j||d|_dS)N) search_pathfactoryZresource_modelservice_contextoperation_name)r r rr r!pathr r)rcollection_modelrr=r>r?r<rrrr8s zCollectionManager.__init__cCs(d|jj|jd|jjj|jjjS)Nz {}({}, {})z{}.{}) rrrr rr r r!r")rrrrr#Fs zCollectionManager.__repr__cKs|j|j|j|jf|S)z Get a resource collection iterator from this manager. :rtype: :py:class:`ResourceCollection` :return: An iterable representing the collection of resources )_collection_clsr r r)rrrrriteratorOszCollectionManager.iteratorcCs|S)N)rC)rrrrr6[szCollectionManager.allcKs |jf|S)N)rC)rrrrrr7`szCollectionManager.filtercCs |j|dS)N)r$)rC)rr'rrrr$eszCollectionManager.limitcCs |j|dS)N)r/)rC)rr'rrrr/jszCollectionManager.page_sizecCs |S)N)rCr&)rrrrr&oszCollectionManager.pagesN)rr8r9r:r rBrr#rCr6r7r$r/r&rrrrr; s*      r;c@s0eZdZdZddZddZddZdd Zd S) CollectionFactoryz A factory to create new :py:class:`CollectionManager` and :py:class:`ResourceCollection` subclasses from a :py:class:`~boto3.resources.model.Collection` model. These subclasses include methods to perform batch operations. c Csi}|j}|||||j||j||||j|td|j|krPd|j|}nd|j||}tt|tf|}|j||||j|t d||d<|d7}tt|t f|S)a Loads a collection from a model, creating a new :py:class:`CollectionManager` subclass with the correct properties and methods, named based on the service and resource name, e.g. ec2.InstanceCollectionManager. It also creates a new :py:class:`ResourceCollection` subclass which is used by the new manager class. :type resource_name: string :param resource_name: Name of the resource to look up. For services, this should match the ``service_name``. :type service_context: :py:class:`~boto3.utils.ServiceContext` :param service_context: Context about the AWS service :type event_emitter: :py:class:`~botocore.hooks.HierarchialEmitter` :param event_emitter: An event emitter :rtype: Subclass of :py:class:`CollectionManager` :return: The collection class. )attrs resource_namerA service_model event_emitter base_classz{}.{}Collectionz{}.{}.{}CollectionrBManager) name_load_batch_actionsrG#_load_documented_collection_methodsr r rr"strr;) rrFrAr>rHrEZcollection_namecls_nameZcollection_clsrrrload_from_definition}s>   z&CollectionFactory.load_from_definitionc Cs6x0|jD]&}t|j}|||||||||<qWdS)zv Batch actions on the collection become methods on both the collection manager and iterators. N)Z batch_actionsrrK_create_batch_action)rrErFrArGrH action_model snake_casedrrrrLs  z%CollectionFactory._load_batch_actionsc sfdd}tj|d|||dd|_||d<fdd}tj|d|||dd|_||d<fd d } tj|d |||dd| _| |d <fd d } tj|d|||dd| _| |d<dS)Ncs |S)N)r6)r)rIrrr6szBCollectionFactory._load_documented_collection_methods..allr6F)rFZ action_namerHrArGinclude_signaturecsj|f|S)N)r7)rr)rIrrr7szECollectionFactory._load_documented_collection_methods..filterr7cs ||S)N)r$)rr')rIrrr$szDCollectionFactory._load_documented_collection_methods..limitr$cs ||S)N)r/)rr')rIrrr/szHCollectionFactory._load_documented_collection_methods..page_sizer/)rZCollectionMethodDocstringr:) factory_selfrErFrArGrHrIr6r7r$r/r)rIrrMsH        z5CollectionFactory._load_documented_collection_methodscs:t|fdd}t||_tj|||||dd|_|S)zs Creates a new method which makes a batch operation request to the underlying service API. cs|f||S)Nr)rargsr)actionrr batch_action0sz.batch_actionF)rFrHZbatch_action_modelrGrArT)rrNrrZBatchActionDocstringr:)rUrFrSrRrArGrHrXr)rWrrQ!s    z&CollectionFactory._create_batch_actionN)rr8r9r:rPrLrMrQrrrrrDus FGrD)rloggingZbotocorerZbotocore.utilsrdocsrrWrr,rresponser getLoggerrr2r r;rDrrrrs       nl