B 劇cX@sddlZddlmZddlmZddlmZddlmZm Z ddl m Z m Z dd l mZdd lmZdd lmZmZeeZGd d d ZdS)N)partial) docstring)ResourceLoadException) ServiceAction WaiterAction) ResourceMetaServiceResource)CollectionFactory) ResourceModel)ResourceHandlerbuild_identifiersc@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZddZddZd d!Zd&d#d$Zd%S)'ResourceFactoryaW A factory to create new :py:class:`~boto3.resources.base.ServiceResource` classes from a :py:class:`~boto3.resources.model.ResourceModel`. There are two types of lookups that can be done: one on the service itself (e.g. an SQS resource) and another on models contained within the service (e.g. an SQS Queue resource). cCst|_||_dS)N)r _collection_factory_emitter)selfZemitterrr/private/var/folders/8c/hx9_v10d5x38qmnzt13b7b8j1k3n5b/T/pip-target-x6xd5gna/lib/python/boto3/resources/factory.py__init__%szResourceFactory.__init__c Cs"td|j|t|||j}d}|jr6|j|j}||t |j|d}d|i}|j ||||d|j ||||d|j |||||d|j |||d|j||||d|j||||d|}|j|krd }|jd |}tg} |jdk r|jjd ||| |d tt|t| |S) a{ Loads a resource from a model, creating a new :py:class:`~boto3.resources.base.ServiceResource` subclass with the correct properties and methods, named based on the service and resource name, e.g. EC2.Instance. :type resource_name: string :param resource_name: Name of the resource to look up. For services, this should match the ``service_name``. :type single_resource_json_definition: dict :param single_resource_json_definition: The loaded json of a single service resource or resource definition. :type service_context: :py:class:`~boto3.utils.ServiceContext` :param service_context: Context about the AWS service :rtype: Subclass of :py:class:`~boto3.resources.base.ServiceResource` :return: The service or resource class. z Loading %s:%sN)resource_modelmeta)attrsr resource_namer)rrrservice_context)rrrrr)rrrr .zcreating-resource-class.)Zclass_attributes base_classesr)loggerdebug service_namer resource_json_definitionsshape service_model shape_forZload_rename_mapr _load_identifiers _load_actions_load_attributes_load_collections_load_has_relations _load_waitersr remittypestrtuple) rrsingle_resource_json_definitionrrr!rrcls_namerrrrload_from_definition)sp     z$ResourceFactory.load_from_definitioncCs4x.|jD]$}|j|j|||||j<qWdS)z Populate required identifiers. These are arguments without which the resource cannot be used. Identifiers become arguments for operations on the resource. N) identifiersappendname_create_identifier)rrrrr identifierrrrr$s z!ResourceFactory._load_identifierscCsT|jr*|j|j||dd|d<|d|d<x$|jD]}|j|||d||j<q2WdS)z Actions on the resource become methods, with the ``load`` method being a special case which sets internal data for attributes, and ``reload`` is an alias for ``load``. T) action_modelrris_loadloadreload)r6rrN)r8_create_actionactionsr3)rrrrractionrrrr%s   zResourceFactory._load_actionsc Cs|js dS|j|j}dd|jD}||}xT|D]H\} \} } | |krh|j||| | |d} n|j|| | | |d} | || <qsz4ResourceFactory._load_attributes..)rr5 member_modelr)rr3 snake_casedrAr)r!r"r#r1get_attributesitems_create_identifier_alias_create_autoload_property) rrrrrrr!r1 attributesr3 orig_namememberproprrrr&s(    z ResourceFactory._load_attributescCs,x&|jD]}|j|j||d||j<qWdS)a Load resource collections from the model. Each collection becomes a :py:class:`~boto3.resources.collection.CollectionManager` instance on the resource instance, which allows you to iterate and filter through the collection's items. )rcollection_modelrN) collections_create_collectionr3)rrrrrKrrrr's  z!ResourceFactory._load_collectionscCs^x$|jD]}|j|||d||j<qWx$|jD]}|j|||d||j<q.W|||jdS)a Load related resources, which are defined via a ``has`` relationship but conceptually come in two forms: 1. A reference, which is a related resource instance and can be ``None``, such as an EC2 instance's ``vpc``. 2. A subresource, which is a resource constructor that will always return a resource instance which shares identifiers/data with this resource, such as ``s3.Bucket('name').Object('key')``. )reference_modelrr)subresource_modelrrN)Z references_create_referencer3 subresources_create_class_partial&_create_available_subresources_command)rrrrr reference subresourcerrrr(s  z#ResourceFactory._load_has_relationscs.dd|Dtfdd}||d<dS)NcSsg|] }|jqSr)r3)r>rUrrr szJResourceFactory._create_available_subresources_command..csS)z Returns a list of all the available sub-resources for this Resource. :returns: A list containing the name of each sub-resource for this resource :rtype: list of str r) factory_self) _subresourcesrrget_available_subresources s zZResourceFactory._create_available_subresources_command..get_available_subresourcesrY)sorted)rrrQrYr)rXrrSs z6ResourceFactory._create_available_subresources_commandcCs*x$|jD]}|j|||d||j<qWdS)z Load resource waiters from the model. Each waiter allows you to wait until a resource reaches a specific state by polling the state of the resource. )resource_waiter_modelrrN)waiters_create_waiterr3)rrrrrwaiterrrrr)-s  zResourceFactory._load_waiterscs2fdd}tj|_tj|dd|_t|S)zI Creates a read-only property for identifier attributes. cst|djdS)N_)getattrr3)r)r5rrget_identifierAsz:ResourceFactory._create_identifier..get_identifierF)rZidentifier_modelinclude_signature)r,r3__name__rZIdentifierDocstring__doc__property)rWr5rrar)r5rr4<s  z"ResourceFactory._create_identifiercs>fdd}tj|_tj|j|j|j|dd|_t|S)zJ Creates a read-only property that aliases an identifier. cst|djdS)Nr_)r`r3)r)r5rrraZsz@ResourceFactory._create_identifier_alias..get_identifierF)rr attr_name event_emitter attr_modelrb) r,r=rcrAttributeDocstringrrrdre)rWrr5rArrar)r5rrESs   z(ResourceFactory._create_identifier_aliascs:fdd}t||_tj|j|||j|dd|_t|S)z Creates a new property on the resource to lazy-load its value via the resource's ``load`` method (if it exists). cs@|jjdkr2t|dr |nt|jjd|jjS)Nr8z has no load method)rdatahasattrr8r __class__rcget)r)r3rrproperty_loaderys    zBResourceFactory._create_autoload_property..property_loaderF)rrrfrgrhrb)r,rcrrirrrdre)rWrr3rBrArrnr)r3rrFis  z)ResourceFactory._create_autoload_propertycsHt||jdfdd}t|j|_tj||j|j||jdd|_ |S)zx Creates a new wait method for each resource where both a waiter and resource model is defined. )Zwaiter_resource_namecs|f||dS)Nr)rargskwargs)r^rr do_waitersz1ResourceFactory._create_waiter..do_waiterF)rrgr"r[service_waiter_modelrb) rr3r,rcrZResourceWaiterDocstringrr"rrrd)rWr[rrrqr)r^rr]s    zResourceFactory._create_waitercsLjj|jdfdd}tj|_tjdd|_t |S)zS Creates a new property on the resource to lazy-load a collection. )rrKrrgcs|dS)N)rKparentfactoryrr)r)clsrKrWrrrget_collections z:ResourceFactory._create_collection..get_collectionF)rKrb) rr0rr,r3rcrZCollectionDocstringrdre)rWrrKrrvr)rurKrWrrrMs   z"ResourceFactory._create_collectioncs^t|jj||j|dtdd|jjDfdd}t|j|_tj |dd|_ t |S)zR Creates a new property on the resource to lazy-load a reference. ) search_pathrtrrcss|]}|jdkVqdS)rjN)source)r>r?rrr sz4ResourceFactory._create_reference..cs2r"|jjdkr"t|dr"||i|jjS)Nr8)rrjrkr8)r)handler needs_datarr get_referencesz8ResourceFactory._create_reference..get_referenceF)rNrb) r resourcepathanyr1r,r3rcrZReferenceDocstringrdre)rWrNrrr|r)rzr{rrPs   z!ResourceFactory._create_referencecs>jjfdd}t|_tj|jdd|_|S)z Creates a new method which acts as a functools.partial, passing along the instance's low-level `client` to the new resource class' constructor. c srg}ji}j|d}jj}|dk rTx t||D]\}}||q>Wt|f|d|jj i||S)N)rr.rclient) r rmr0r}r1rr2rrr) rrorpZpositional_argsZjson_defZ resource_clsr1r5value)rWr3rrOrrcreate_resourcesz>ResourceFactory._create_class_partial..create_resourceF)rZsub_resource_modelr"rb)r}r+r,rcrZSubResourceDocstringr"rd)rWrOrrrr)rWr3rrOrrRs  z%ResourceFactory._create_class_partialFcsvt|||d|r<fdd}tj|j||j||jdd}n$fdd}tj||j||jdd}t|j|_||_ |S)zc Creates a new method which makes a request to the underlying AWS service. )rtrcs|f||}||j_dS)N)rrj)rrorpresponse)r<rr do_action3sz1ResourceFactory._create_action..do_actionF)Z action_namerrgZ load_modelr"rbcs&|f||}t|dr"d|j_|S)Nr8)rkrrj)rrorpr)r<rrrCs )rrgr6r"rb) rrZLoadReloadDocstringr3rr"ZActionDocstringr,rcrd)rWr6rrr7rZlazy_docstringr)r<rr:s*     zResourceFactory._create_actionN)F)rc __module__ __qualname__rdrr0r$r%r&r'r(rSr)r4rErFr]rMrPrRr:rrrrrs$w &$')3r)logging functoolsrdocsr exceptionsrr<rrbaser r collectionr modelr rr r getLoggerrcrrrrrrs