
     hfC                         d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ 	 dd	lZdd
lmZ n# e$ r d	xZZY nw xY wdZ edd          Z ee          Z G d de          Zd	S )z"AWS DynamoDB result store backend.    )
namedtuple)sleeptime)
_parse_url)ImproperlyConfigured)
get_logger   )KeyValueStoreBackendN)ClientError)DynamoDBBackendDynamoDBAttributename	data_typec                   &    e Zd ZdZdZdZdZdZdZdZ	dZ
 edd          Z ed	d
          Z edd          Z edd          ZdZd! fd	Zd!dZd Zd Zd Zd Zd Zd Zd Zd"dZd Zd Zd Zed             Zd Z d Z!d Z"d  Z# xZ$S )#r   zAWS DynamoDB result backend.

    Raises:
        celery.exceptions.ImproperlyConfigured:
            if module :pypi:`boto3` is not available.
    celeryr	   NTidSr   resultB	timestampNttlc                     t                      j        |i | || _        |p| j        | _        t          st          d          d}d }d }|{t          |          \  }}	}
}}}}|}|}|d u}|d u}||k    rt          d          |}|	dk    rDd|
 | _        d| _        t          
                    d                    | j                             n|	| _        | j        j        j        } |d          }|r|| _        t          |                    d	| j                            | _        t          |                    d
| j                            | _        |                    d| j                  }|rF	 t          |          | _        n0# t&          $ r#}t                              d|           |d }~ww xY w|p| j        | _        | j        | j        | j        f| _        d | _        |r|                     ||           d S d S )NzBYou need to install the boto3 library to use the DynamoDB backend.Fz6You need to specify both the Access Key ID and Secret.	localhostzhttp://localhost:z	us-east-1z*Using local-only DynamoDB endpoint URL: {}dynamodb_endpoint_urlreadwritettl_secondsz!TTL must be a number; got "{ttl}")exc_info)access_key_idsecret_access_key)super__init__url
table_nameboto3r   	parse_urlendpoint_url
aws_regionloggerwarningformatappconfgetintread_capacity_unitswrite_capacity_unitstime_to_live_seconds
ValueErrorerror
_key_field_value_field_timestamp_field_available_fields_client_get_client)selfr%   r&   argskwargsaws_credentials_givenaws_access_key_idaws_secret_access_keyschemeregionportusernamepasswordtablequeryaccess_key_givensecret_key_given_getconfig_endpoint_urlr   e	__class__s                        T/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/celery/backends/dynamodb.pyr$   zDynamoDBBackend.__init__>   s   $)&)))$7 	%&$% % % !&  $?# CFFD(HeU !)$,!0<4D@#333*"# # # %5!$$$>$>$>!"-@GG)     #) 8=$D"&$'>"?"?" 8$7!'*		, ( (D$ ),		- ) )D% ))M4+DEEC 03CD--!   LL;!" !    G $6tDO O!"
   	/"7      	 	s   7F 
F9F44F9c                 T   | j         d| j        i}||                    ||d           | j        
| j        |d<   t	          j        	 di || _         |                                  |                                 (|                                  | 	                                 | j         S )zGet client connection.Nregion_name)rA   rB   r)   dynamodb)rS   )
r;   r*   updater)   r'   client_get_or_create_table_has_ttl_validate_ttl_methods_set_table_ttl)r=   r!   r"   client_parameterss       rP   r<   zDynamoDBBackend._get_client   s    <t! (!(()6->* *   
  ,484E!.1 < # DL %%'''}}***,,,##%%%|    c                     | j         j        | j         j        dg| j        | j         j        ddg| j        | j        ddS )z=Get the boto3 structure describing the DynamoDB table schema.)AttributeNameAttributeTypeHASH)r]   KeyType)ReadCapacityUnitsWriteCapacityUnits)AttributeDefinitions	TableName	KeySchemaProvisionedThroughput)r7   r   r   r&   r2   r3   r=   s    rP   _get_table_schemaz!DynamoDBBackend._get_table_schema   sm    
 &*_%9%)_%> %  &*_%9%  &*%=&*&?& &
 
 	
r[   c                    |                                  }	  | j        j        d
i |}t                              d                    | j                             |                     d           t                              d                    | j                             |S # t          $ rS}|j	        d         
                    dd          }|dk    r%| j                            | j                  cY d	}~S |d	}~ww xY w)z=Create table if not exists, otherwise return the description.z*DynamoDB Table {} did not exist, creating.ACTIVEz#DynamoDB Table {} is now available.ErrorCodeUnknownResourceInUseExceptionrd   N )rh   r;   create_tabler+   infor-   r&   _wait_for_table_statusr   responser0   describe_table)r=   table_schematable_descriptionrN   
error_codes        rP   rV   z$DynamoDBBackend._get_or_create_table   s:   --//	 9 9 I IL I IKK<CCO    ''111KK5<<O   
 %$ 		 		 		G,00CCJ 555|22"o 3         		s%   BB# #
D -AC;3D 9C;;D c                 *    | j         dn
| j         dk    S )zReturn the desired Time to Live config.

        - True:  Enable TTL on the table; use expiry.
        - False: Disable TTL on the table; don't use expiry.
        - None:  Ignore TTL on the table; don't use expiry.
        Nr   )r4   rg   s    rP   rW   zDynamoDBBackend._has_ttl   s!     08tt*a/	0r[   c                 x   d}g }t          |          D ],}t          | j        |          s|                    |           -|rwt                              d                    d                    |                               t          d                    d                    |                              dS )z:Verify boto support for the DynamoDB Time to Live methods.)update_time_to_livedescribe_time_to_livezdboto3 method(s) {methods} not found; ensure that boto3>=1.9.178 and botocore>=1.12.178 are installed,)methodsz#boto3 method(s) {methods} not foundN)	listhasattrr;   appendr+   r6   r-   joinAttributeError)r=   required_methodsmissing_methodsmethods       rP   rX   z%DynamoDBBackend._validate_ttl_methods   s    
 +,, 	/ 	/F4<00 /&&v... 	LLJ&HH_55   	   !5<<HH_55 =    	 	r[   c                 @    | j         |                                 |ddS )zBGet the boto3 structure describing the DynamoDB TTL specification.)Enabledr]   )rd   TimeToLiveSpecification)r&   rW   )r=   ttl_attr_names     rP   _get_ttl_specificationz&DynamoDBBackend._get_ttl_specification  s0     ==??!.( (
 
 	
r[   c                 `   	 | j                             | j                  }n# t          $ r~}|j        d                             dd          }|j        d                             dd          }t                              d                    | j        ||                     |d }~ww xY w|S )Nro   rk   rl   rm   MessagezJError describing Time to Live on DynamoDB table {table}: {code}: {message})rH   codemessage)	r;   r|   r&   r   rt   r0   r+   r6   r-   )r=   descriptionrN   rx   error_messages        rP   _get_table_ttl_descriptionz*DynamoDBBackend._get_table_ttl_description  s    	,<</ =  KK  	 	 	G,00CCJJw/33IyIIMLL$fo%      G	 s    # 
B+A9B&&B+c           	         |                                  }|d         d         }|dv rq|d         d         }|                                 rN|| j        j        k    r>t                              d                    |dk    rdnd| j        	                     |S n|d
v rS|                                 s>t                              d                    |dk    rdnd| j        	                     |S n4t                              d                    || j                             |dk    r|n| j        j        }	  | j	        j
        di |                     |          }t                              d                    | j        |                                 | j        j                             |S # t          $ r}|j        d                             dd          }|j        d                             dd          }t                              d                    |                                 rdnd| j        ||                     |d}~ww xY w)z,Enable or disable Time to Live on the table.TimeToLiveDescriptionTimeToLiveStatus)ENABLEDENABLINGr]   z5DynamoDB Time to Live is {situation} on table {table}r   zalready enabledzcurrently being enabled)	situationrH   )DISABLED	DISABLINGr   zalready disabledzcurrently being disabledzWUnknown DynamoDB Time to Live status {status} on table {table}. Attempting to continue.)statusrH   )r   zUDynamoDB table Time to Live updated: table={table} enabled={enabled} attribute={attr})rH   enabledattrrk   rl   rm   r   zHError {action} Time to Live on DynamoDB table {table}: {code}: {message}enabling	disabling)actionrH   r   r   Nrp   )r   rW   
_ttl_fieldr   r+   debugr-   r&   r,   r;   r{   r   rr   r   rt   r0   r6   )	r=   r   r   cur_attr_name	attr_namespecificationrN   rx   r   s	            rP   rY   zDynamoDBBackend._set_table_ttl+  s    5577456HI,,,34_E }} ' DO$888 LL+f!Y.. #4"36"o	      '&000==?? # '&++ 103/	      #"#$ NN<fo     L $y00MMdo6J 		<DL<  --"+ .   M
 KKG&/ MMOO-   		 	 	 !  	 	 	G,00CCJJw/33IyIIMLL$f%)]]__Ezz+o%	      G	s   8A8F1 1
I;BIIrj   c                    d}|sz| j                             | j                  }t                              d                    | j        |                     |d         d         }||k    }t          d           |xdS dS )z#Poll for the expected table status.Fro   z+Waiting for DynamoDB table {} to become {}.TableTableStatusr	   N)rU   ru   r&   r+   r   r-   r   )r=   expectedachieved_staterw   current_statuss        rP   rs   z&DynamoDBBackend._wait_for_table_status  s      	 $ : :/ !; ! ! LL=DDO    /w7FN+x7N!HHH ! 	 	 	 	 	r[   c                 F    | j         | j        j        | j        j        |iidS )z0Construct the item retrieval request parameters.)rd   Key)r&   r7   r   r   )r=   keys     rP   _prepare_get_requestz$DynamoDBBackend._prepare_get_request  s4     $O-s'
 
 	
r[   c           
         t                      }| j        | j        j        | j        j        |i| j        j        | j        j        |i| j        j        | j        j        t          |          iid}|                                 rU|d         	                    | j
        j        | j
        j        t          t          || j        z                       ii           |S )z/Construct the item creation request parameters.)rd   Itemr   )r   r&   r7   r   r   r8   r9   strrW   rT   r   r1   r4   )r=   r   valuer   put_requests        rP   _prepare_put_requestz$DynamoDBBackend._prepare_put_request  s    FF	$O-s' !&%/) %*)3S^^-

 
 ==?? 	&&$O-C	D,E EFFGG'(    r[   c                 6    dvri S fd| j         D             S )z1Convert get_item() response to field-value pairs.r   c                 X    i | ]&}|j         d          |j                  |j                 'S )r   r   ).0fieldraw_responses     rP   
<dictcomp>z1DynamoDBBackend._item_to_dict.<locals>.<dictcomp>  s@     
 
 
 JV,UZ8I
 
 
r[   )r:   )r=   r   s    `rP   _item_to_dictzDynamoDBBackend._item_to_dict  sA    %%I
 
 
 
/
 
 
 	
r[   c                 *    |                                  S )N)r<   rg   s    rP   rU   zDynamoDBBackend.client  s    !!!r[   c                     t          |          }|                     |          } | j        j        di |}|                     |          }|                    | j        j                  S Nrp   )r   r   rU   get_itemr   r0   r8   r   )r=   r   request_parametersitem_responseitems        rP   r0   zDynamoDBBackend.get  sg    #hh!66s;;,,BB/ABB!!-00xx).///r[   c                 t    t          |          }|                     ||          } | j        j        di | d S r   )r   r   rU   put_item)r=   r   r   r   s       rP   setzDynamoDBBackend.set  sE    #hh!66sEBB22122222r[   c                        fd|D             S )Nc                 :    g | ]}                     |          S rp   )r0   )r   r   r=   s     rP   
<listcomp>z(DynamoDBBackend.mget.<locals>.<listcomp>  s#    ...#...r[   rp   )r=   keyss   ` rP   mgetzDynamoDBBackend.mget  s    ........r[   c                 r    t          |          }|                     |          } | j        j        di | d S r   )r   r   rU   delete_item)r=   r   r   s      rP   deletezDynamoDBBackend.delete  sC    #hh!66s;;55"455555r[   )NN)rj   )%__name__
__module____qualname____doc__r&   r2   r3   r*   r)   r4   supports_autoexpirer   r7   r8   r9   r   r:   r$   r<   rh   rV   rW   rX   r   r   rY   rs   r   r   r   propertyrU   r0   r   r   r   __classcell__)rO   s   @rP   r   r      s         J   J L   ""<<<J$$(cBBBL((kSIII""===JW W W W W Wr   6
 
 
,  :0 0 0  :
 
 
  *n n n`   "	
 	
 	
  4
 
 
 " " X"0 0 03 3 3
/ / /6 6 6 6 6 6 6r[   r   )r   collectionsr   r   r   kombu.utils.urlr   r(   celery.exceptionsr   celery.utils.logr   baser
   r'   botocore.exceptionsr   ImportError__all__r   r   r+   r   rp   r[   rP   <module>r      s4   ( ( " " " " " "         3 3 3 3 3 3 2 2 2 2 2 2 ' ' ' ' ' ' & & & & & &LLL///////   EKKK  J24IJJ 	H		U6 U6 U6 U6 U6* U6 U6 U6 U6 U6s   
5 	A A