o
    ܀cX                     @   s   d dl Z d dlmZ ddlmZ ddlmZ ddlmZm	Z	 ddl
mZmZ dd	lmZ dd
lmZ ddlmZmZ e eZG dd dZdS )    N)partial   )	docstring)ResourceLoadException   )ServiceActionWaiterAction)ResourceMetaServiceResource)CollectionFactory)ResourceModel)ResourceHandlerbuild_identifiersc                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Z	"d&d#d$Zd%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).
    c                 C   s   t  | _|| _d S N)r   _collection_factory_emitter)selfemitter r   B/usr/local/lib/python3.10/dist-packages/boto3/resources/factory.py__init__%   s   
zResourceFactory.__init__c           
      C   s   t d|j| t|||j}d}|jr|j|j}|| t	|j|d}d|i}| j
||||d | j||||d | j|||||d | j|||d | j||||d | j||||d |}|j|krjd	}|jd
 | }tg}	| jdur| jjd| ||	|d tt|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_modelmeta)attrsr   resource_namer   )r   r   r   service_context)r   r   r   r   r   )r   r   r   r
   .zcreating-resource-class.)class_attributesbase_classesr   )loggerdebugservice_namer   resource_json_definitionsshapeservice_model	shape_forload_rename_mapr	   _load_identifiers_load_actions_load_attributes_load_collections_load_has_relations_load_waitersr
   r   emittypestrtuple)
r   r   single_resource_json_definitionr   r   r$   r   r   cls_namer   r   r   r   load_from_definition)   s   
	

z$ResourceFactory.load_from_definitionc                 C   s0   |j D ]}|j |j | ||||j< qdS )z
        Populate required identifiers. These are arguments without which
        the resource cannot be used. Identifiers become arguments for
        operations on the resource.
        N)identifiersappendname_create_identifier)r   r   r   r   r   
identifierr   r   r   r(      s   
z!ResourceFactory._load_identifiersc                 C   sP   |j r| j|j ||dd|d< |d |d< |jD ]}| j|||d||j< qdS )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_modelr   r   is_loadloadreload)r:   r   r   N)r<   _create_actionactionsr7   )r   r   r   r   r   actionr   r   r   r)      s   

zResourceFactory._load_actionsc                 C   s   |j sdS |j|j }dd |jD }||}| D ]$\}	\}
}|	|v r3| j|||	 ||d}n
| j||
|	||d}|||	< qdS )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``.
        Nc                 S   s   i | ]	}|j r|j |qS r   )member_name.0ir   r   r   
<dictcomp>   s    z4ResourceFactory._load_attributes.<locals>.<dictcomp>)r   r9   member_modelr   )r   r7   snake_casedrF   r   )r$   r%   r&   r5   get_attributesitems_create_identifier_alias_create_autoload_property)r   r   r   r   r   r   r$   r5   
attributesr7   	orig_namememberpropr   r   r   r*      s0   	

z ResourceFactory._load_attributesc                 C   s(   |j D ]}| j|j||d||j< qdS )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.
        )r   collection_modelr   N)collections_create_collectionr7   )r   r   r   r   rP   r   r   r   r+      s   
z!ResourceFactory._load_collectionsc                 C   sV   |j D ]}| j|||d||j< q|jD ]}| j|||d||j< q| ||j dS )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_modelr   r   )subresource_modelr   r   N)
references_create_referencer7   subresources_create_class_partial&_create_available_subresources_command)r   r   r   r   r   	referencesubresourcer   r   r   r,      s   

z#ResourceFactory._load_has_relationsc                    s.   dd |D  t    fdd}||d< d S )Nc                 S   s   g | ]}|j qS r   r7   )rC   r[   r   r   r   
<listcomp>  s    zJResourceFactory._create_available_subresources_command.<locals>.<listcomp>c                    s    S )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_subresourcesr   r   get_available_subresources   s   	zZResourceFactory._create_available_subresources_command.<locals>.get_available_subresourcesra   )sorted)r   r   rW   ra   r   r_   r   rY     s   z6ResourceFactory._create_available_subresources_commandc                 C   s&   |j D ]}| j|||d||j< qdS )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_modelr   r   N)waiters_create_waiterr7   )r   r   r   r   r   waiterr   r   r   r-   -  s   
zResourceFactory._load_waitersc                    s2    fdd}t  j|_tj| dd|_t|S )zI
        Creates a read-only property for identifier attributes.
        c                       t | d j d S N_getattrr7   r   r9   r   r   get_identifierA  s   z:ResourceFactory._create_identifier.<locals>.get_identifierF)r   identifier_modelinclude_signature)r0   r7   __name__r   IdentifierDocstring__doc__property)r^   r9   r   rn   r   rm   r   r8   <  s   	z"ResourceFactory._create_identifierc                    s>    fdd}t  j|_tj|j| j| j|dd|_t|S )zJ
        Creates a read-only property that aliases an identifier.
        c                    rg   rh   rj   rl   rm   r   r   rn   Z  s   z@ResourceFactory._create_identifier_alias.<locals>.get_identifierFr"   r   	attr_nameevent_emitter
attr_modelrp   )	r0   rA   rq   r   AttributeDocstringr"   r   rs   rt   )r^   r   r9   rF   r   rn   r   rm   r   rJ   S  s   	z(ResourceFactory._create_identifier_aliasc                    s:    fdd}t ||_tj|j||| j|dd|_t|S )z
        Creates a new property on the resource to lazy-load its value
        via the resource's ``load`` method (if it exists).
        c                    s@   | j jd u rt| dr|   n	t| jj d| j j S )Nr<   z has no load method)r   datahasattrr<   r   	__class__rq   getrl   r\   r   r   property_loadery  s   

zBResourceFactory._create_autoload_property.<locals>.property_loaderFru   )r0   rq   r   ry   r"   r   rs   rt   )r^   r   r7   rG   rF   r   r~   r   r\   r   rK   i  s   
	z)ResourceFactory._create_autoload_propertyc                    sH   t ||jd  fdd}t|j|_tj|| j|j||jdd|_	|S )zx
        Creates a new wait method for each resource where both a waiter and
        resource model is defined.
        )waiter_resource_namec                    s    | g|R i | d S r   r   )r   argskwargsrf   r   r   	do_waiter  s   z1ResourceFactory._create_waiter.<locals>.do_waiterF)r   rw   r%   rc   service_waiter_modelrp   )
r   r7   r0   rq   r   ResourceWaiterDocstringr   r%   r   rs   )r^   rc   r   r   r   r   r   r   re     s   zResourceFactory._create_waiterc                    sL   j j|jd  fdd}tj|_tjdd|_t	|S )zS
        Creates a new property on the resource to lazy-load a collection.
        )r   rP   r   rw   c                    s    | dS )N)rP   parentfactoryr   r   rl   clsrP   r^   r   r   r   get_collection  s   z:ResourceFactory._create_collection.<locals>.get_collectionF)rP   rp   )
r   r4   r   r0   r7   rq   r   CollectionDocstringrs   rt   )r^   r   rP   r   r   r   r   r   rR     s   z"ResourceFactory._create_collectionc                    s^   t |jj| |j|d tdd |jjD  fdd}t|j|_tj	|dd|_
t|S )zR
        Creates a new property on the resource to lazy-load a reference.
        )search_pathr   r   r   c                 s   s    | ]}|j d kV  qdS )rz   N)sourcerB   r   r   r   	<genexpr>  s    

z4ResourceFactory._create_reference.<locals>.<genexpr>c                    s2   r| j jd u rt| dr|    | i | j jS Nr<   )r   rz   r{   r<   rl   handler
needs_datar   r   get_reference  s   z8ResourceFactory._create_reference.<locals>.get_referenceF)rS   rp   )r   resourcepathanyr5   r0   r7   rq   r   ReferenceDocstringrs   rt   )r^   rS   r   r   r   r   r   r   rV     s   	
z!ResourceFactory._create_referencec                    s>   j j fdd}t|_tj|jdd|_|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           	         st   g }j i } j|d}jj}|d ur(t|| D ]	\}}|| qt|g|R d| jj	i|i |S )N)r   r2   r   client)
r#   r}   r4   r   r5   r   r6   r   r   r   )	r   r   r   positional_argsjson_defresource_clsr5   r9   valuer^   r7   r   rT   r   r   create_resource  s,   z>ResourceFactory._create_class_partial.<locals>.create_resourceF)r   sub_resource_modelr%   rp   )r   r/   r0   rq   r   SubResourceDocstringr%   rs   )r^   rT   r   r   r   r   r   r   rX     s   
z%ResourceFactory._create_class_partialFc                    sv   t || |d |r fdd}tj|j|| j||jdd}n fdd}tj|| j||jdd}t|j|_||_	|S )zc
        Creates a new method which makes a request to the underlying
        AWS service.
        )r   r   c                    s"    | g|R i |}|| j _d S r   )r   rz   r   r   r   responser@   r   r   	do_action3  s   z1ResourceFactory._create_action.<locals>.do_actionF)action_namer   rw   
load_modelr%   rp   c                    s,    | g|R i |}t | drd | j_|S r   )r{   r   rz   r   r   r   r   r   C  s   
)r   rw   r:   r%   rp   )
r   r   LoadReloadDocstringr7   r   r%   ActionDocstringr0   rq   rs   )r^   r:   r   r   r;   r   lazy_docstringr   r   r   r>     s0   zResourceFactory._create_actionN)F)rq   
__module____qualname__rs   r   r4   r(   r)   r*   r+   r,   rY   r-   r8   rJ   rK   re   rR   rV   rX   r>   r   r   r   r   r      s(    w&$')3r   )logging	functoolsr   docsr   
exceptionsr   r@   r   r   baser	   r
   
collectionr   modelr   r   r   r   	getLoggerrq   r    r   r   r   r   r   <module>   s   
