U C^W@sddlZddlmZddlmZddlmZddlmZmZddl m Z ddl m Z dd l mZmZd d lmZd d lmZeeZGd ddeZdS)N)partial) ServiceAction) WaiterAction) ResourceMetaServiceResource)CollectionFactory) ResourceModel)build_identifiersResourceHandler)ResourceLoadException) docstringc@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|_||_dSN)r_collection_factory_emitter)selfZemitterr:/tmp/pip-install-6_kvzl1k/boto3/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.%s)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_waitersrremittypestrtuple) rrsingle_resource_json_definitionrrr"rrZcls_namerrrrload_from_definition*s    z$ResourceFactory.load_from_definitioncCs0|jD]$}|j|j|||||j<qdS)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_identifierscCsP|jr*|j|j||dd|d<|d|d<|jD]}|j|||d||j<q0dS)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}tdd|jD}||}|D]H\} \} } | |krj|j||| | |d} n|j|| | | |d} | || <q>dS)a Load resource attributes based on the resource shape. The shape name is referenced in the resource JSON, but the shape itself is defined in the Botocore service JSON, hence the need for access to the ``service_model``. Ncss|]}|jr|j|fVqdSr) member_name.0irrr sz3ResourceFactory._load_attributes..)rr5 member_modelr)rr3 snake_casedrBr) r"r#r$dictr1Zget_attributesitems_create_identifier_alias_create_autoload_property) rrrrrrr"r1 attributesr3Z orig_namememberproprrrr's2 z ResourceFactory._load_attributescCs(|jD]}|j|j||d||j<qdS)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_collectionscCsV|jD]}|j|||d||j<q|jD]}|j|||d||j<q(|||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|] }|jqSrr3)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_selfZ _subresourcesrrget_available_subresourcess zZResourceFactory._create_available_subresources_command..get_available_subresourcesrZ)sorted)rrrQrZrrYrrSs z6ResourceFactory._create_available_subresources_commandcCs&|jD]}|j|||d||j<qdS)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|djdSN_getattrr3rr5rrget_identifier sz:ResourceFactory._create_identifier..get_identifierF)rZidentifier_modelinclude_signature)r-r3__name__rZIdentifierDocstring__doc__property)rXr5rrfrrerr4s z"ResourceFactory._create_identifiercs>fdd}tj|_tj|j|j|j|dd|_t|S)zJ Creates a read-only property that aliases an identifier. cst|djdSr`rbrdrerrrf7sz@ResourceFactory._create_identifier_alias..get_identifierFr r attr_name event_emitterZ attr_modelrg) r-r=rhrAttributeDocstringr rrirj)rXrr5rBrrfrrerrF2s   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 |ntd|jj|jjS)Nr8z{0} has no load method) rdatahasattrr8r format __class__rhgetrdrVrrproperty_loaderPs   zBResourceFactory._create_autoload_property..property_loaderFrk)r-rhrrnr rrirj)rXrr3rCrBrrtrrVrrGFs  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||dSrr)rargskwargsr_rr do_waiterpsz1ResourceFactory._create_waiter..do_waiterF)rrmr#r\service_waiter_modelrg) rr3r-rhrZResourceWaiterDocstringrr#ryri)rXr\rrrxrrwrr^gs  zResourceFactory._create_waitercsLjj|jdfdd}tj|_tjdd|_t |S)zS Creates a new property on the resource to lazy-load a collection. )rrKrrmcs|dS)N)rKparentfactoryrrrdclsrKrXrrrget_collections z:ResourceFactory._create_collection..get_collectionF)rKrg) rr0rr-r3rhrZCollectionDocstringrirj)rXrrKrr~rr|rrM~s 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_pathr{rrcss|]}|jdkVqdS)roN)sourcer>rrrrAsz4ResourceFactory._create_reference..cs2r"|jjdkr"t|dr"||i|jjSNr8)rrorpr8rdhandlerZ needs_datarr get_referencesz8ResourceFactory._create_reference..get_referenceF)rNrg) r resourcepathanyr1r-r3rhrZReferenceDocstringrirj)rXrNrrrrrrrPs   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 sng}ji}j|d}jj}|dk rPt||D]\}}||qResourceFactory._create_class_partial..create_resourceF)rZsub_resource_modelr#rg)rr,r-rhrZSubResourceDocstringr#ri)rXrOrrrrrrrRs 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. )r{rcs|f||}||j_dSr)rrorrurvresponser<rr do_actionsz1ResourceFactory._create_action..do_actionF) action_namerrmZ load_modelr#rgcs&|f||}t|dr"d|j_|Sr)rprrorrrrrs )rrmr6r#rg) rrZLoadReloadDocstringr3rr#ZActionDocstringr-rhri)rXr6rrr7rZlazy_docstringrrrr:s4    zResourceFactory._create_actionN)F)rh __module__ __qualname__rirr0r%r&r'r(r)rSr*r4rFrGr^rMrPrRr:rrrrrs&c #"!'-r)logging functoolsrr<rrbaserrZ collectionrmodelr rr r exceptionsr Zdocsr getLoggerrhrobjectrrrrrs