o
    rMfD                     @   s   d dl Z d dlmZ d dlmZ d dlmZmZmZ d dl	m
Z
 d dlmZmZ d dlZdZde Zd	e Zd
ZG dd deZdS )    N)UploadArchiveErrorJob)compute_hashes_from_fileobjresume_file_uploadWriter)ConcurrentUploader)minimum_part_sizeDEFAULT_PART_SIZEi   i   i@  i'  c                   @   s   e Zd ZeZde ZdZd&ddZdd Z	dd	 Z
d&d
dZdd ZedfddZ		d'ddZedd Z		d(ddZdd Z		d(ddZ				d)ddZdd Zdd Zd d! Zd(d"d#Zd$d% ZdS )*Vaultd   ))	VaultNamenameN)VaultARNarnN)CreationDatecreation_dateN)LastInventoryDatelast_inventory_dateN)SizeInBytessizer   )NumberOfArchivesnumber_of_archivesr   Nc                 C   sV   || _ |r| jD ]\}}}|| }t| || qd S | jD ]\}}}t| || qd S N)layer1ResponseDataElementssetattr)selfr   response_dataresponse_name	attr_namedefaultvalue r#   T/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/boto/glacier/vault.py__init__5   s   zVault.__init__c                 C   s
   d| j  S )NzVault("%s"))r   r   r#   r#   r$   __repr__?   s   
zVault.__repr__c                 C   s   | j | j dS )z/
        Delete's this vault. WARNING!
        N)r   delete_vaultr   r&   r#   r#   r$   deleteB   s   zVault.deletec                 C   s,   t j|| jkr| j||dS | ||S )ar  
        Adds an archive to a vault. For archives greater than 100MB the
        multipart upload will be used.

        :type file: str
        :param file: A filename to upload

        :type description: str
        :param description: An optional description for the archive.

        :rtype: str
        :return: The archive id of the newly created archive
        )description)ospathgetsizeSingleOperationThresholdcreate_archive_from_file _upload_archive_single_operation)r   filenamer*   r#   r#   r$   upload_archiveH   s   zVault.upload_archivec                 C   sd   t |d!}t|\}}|d | j| j||||}W d   |d S 1 s)w   Y  |d S )ai  
        Adds an archive to a vault in a single operation. It's recommended for
        archives less than 100MB

        :type file: str
        :param file: A filename to upload

        :type description: str
        :param description: A description for the archive.

        :rtype: str
        :return: The archive id of the newly created archive
        rbr   N	ArchiveId)openr   seekr   r2   r   )r   r1   r*   fileobjlinear_hash	tree_hashresponser#   r#   r$   r0   Z   s   

z&Vault._upload_archive_single_operationc                 C   s$   | j | j||}t| |d |dS )a  
        Create a new archive and begin a multi-part upload to it.
        Returns a file-like object to which the data for the archive
        can be written. Once all the data is written the file-like
        object should be closed, you can then call the get_archive_id
        method on it to get the ID of the created archive.

        :type part_size: int
        :param part_size: The part size for the multipart upload.

        :type description: str
        :param description: An optional description for the archive.

        :rtype: :class:`boto.glacier.writer.Writer`
        :return: A Writer object that to which the archive data
            should be written.
        UploadId)	part_size)r   initiate_multipart_uploadr   r   )r   r<   r*   r:   r#   r#   r$   create_archive_writerp   s
   
zVault.create_archive_writerc           	      C   s   | j }|s#tj|}zt||}W n ty   tdw t|d}| j||d}|r1||j	 	 |
|}|s:n|| q2|  | S )a  
        Create a new archive and upload the data from the given file
        or file-like object.

        :type filename: str
        :param filename: A filename to upload

        :type file_obj: file
        :param file_obj: A file-like object to upload

        :type description: str
        :param description: An optional description for the archive.

        :type upload_id_callback: function
        :param upload_id_callback: if set, call with the upload_id as the
            only parameter when it becomes known, to enable future calls
            to resume_archive_from_file in case resume is needed.

        :rtype: str
        :return: The archive id of the newly created archive
        zAFile size of %s bytes exceeds 40,000 GB archive limit of Glacier.r3   )r*   r<   )DefaultPartSizer+   r,   r-   r	   
ValueErrorr   r5   r>   	upload_idreadwritecloseget_archive_id)	r   r1   file_objr*   upload_id_callbackr<   	file_sizewriterdatar#   r#   r$   r/      s.   



zVault.create_archive_from_filec                 C   st   dd |  dD \}}|d }|| }||d kr&|d8 }|d8 }|d8 }|| r.J d||ks6J d|| S )Nc                 S   s   g | ]}t |qS r#   )int).0r"   r#   r#   r$   
<listcomp>   s    z5Vault._range_string_to_part_index.<locals>.<listcomp>-   z0upload part start byte is not on a part boundaryz$upload part is bigger than part size)split)range_stringr<   start
inside_endendlengthr#   r#   r$   _range_string_to_part_index   s   
z!Vault._range_string_to_part_indexc           
      C   sj   |  |}|d }i }|d D ]}| |d |}t|d d}	|	||< q|s-t|d}t| ||||S )a'  Resume upload of a file already part-uploaded to Glacier.

        The resumption of an upload where the part-uploaded section is empty
        is a valid degenerate case that this function can handle.

        One and only one of filename or file_obj must be specified.

        :type upload_id: str
        :param upload_id: existing Glacier upload id of upload being resumed.

        :type filename: str
        :param filename: file to open for resume

        :type fobj: file
        :param fobj: file-like object containing local data to resume. This
            must read from the start of the entire upload, not just from the
            point being resumed. Use fobj.seek(0) to achieve this if necessary.

        :rtype: str
        :return: The archive id of the newly created archive

        PartSizeInBytesPartsRangeInBytesSHA256TreeHash	hex_codecr3   )list_all_partsrV   codecsdecoder5   r   )
r   rA   r1   rF   part_list_responser<   part_hash_map	part_desc
part_indexpart_tree_hashr#   r#   r$   resume_archive_from_file   s   



zVault.resume_archive_from_filec                 K   s&   t | j| jfi |}|||}|S )a  
        Create a new archive from a file and upload the given
        file.

        This is a convenience method around the
        :class:`boto.glacier.concurrent.ConcurrentUploader`
        class.  This method will perform a multipart upload
        and upload the parts of the file concurrently.

        :type filename: str
        :param filename: A filename to upload

        :param kwargs: Additional kwargs to pass through to
            :py:class:`boto.glacier.concurrent.ConcurrentUploader`.
            You can pass any argument besides the ``api`` and
            ``vault_name`` param (these arguments are already
            passed to the ``ConcurrentUploader`` for you).

        :raises: `boto.glacier.exception.UploadArchiveError` is an error
            occurs during the upload process.

        :rtype: str
        :return: The archive id of the newly created archive

        )r   r   r   upload)r   r1   r*   kwargsuploader
archive_idr#   r#   r$   #concurrent_create_archive_from_file   s   z)Vault.concurrent_create_archive_from_filec                 C   sH   d|d}|dur||d< |dur||d< | j | j|}| |d S )a  
        Initiate a archive retrieval job to download the data from an
        archive. You will need to wait for the notification from
        Amazon (via SNS) before you can actually download the data,
        this takes around 4 hours.

        :type archive_id: str
        :param archive_id: The id of the archive

        :type description: str
        :param description: An optional description for the job.

        :type sns_topic: str
        :param sns_topic: The Amazon SNS topic ARN where Amazon Glacier
            sends notification when the job is completed and the output
            is ready for you to download.

        :rtype: :class:`boto.glacier.job.Job`
        :return: A Job object representing the retrieval job.
        zarchive-retrieval)Typer4   NSNSTopicDescriptionJobId)r   initiate_jobr   get_job)r   rh   	sns_topicr*   job_datar:   r#   r#   r$   retrieve_archive  s   zVault.retrieve_archivec           
      C   s   ddi}|dur||d< |dur||d< |dur||d< |dus(|dus(|durLi }|dur5| d|d< |dur@| d|d	< |durH||d
< ||d< | j| j|}	|	d S )a  
        Initiate a inventory retrieval job to list the items in the
        vault. You will need to wait for the notification from
        Amazon (via SNS) before you can actually download the data,
        this takes around 4 hours.

        :type description: str
        :param description: An optional description for the job.

        :type sns_topic: str
        :param sns_topic: The Amazon SNS topic ARN where Amazon Glacier
            sends notification when the job is completed and the output
            is ready for you to download.

        :type byte_range: str
        :param byte_range: Range of bytes to retrieve.

        :type start_date: DateTime
        :param start_date: Beginning of the date range to query.

        :type end_date: DateTime
        :param end_date: End of the date range to query.

        :type limit: int
        :param limit: Limits the number of results returned.

        :rtype: str
        :return: The ID of the job
        rj   zinventory-retrievalNrk   rl   RetrievalByteRangez%Y-%m-%dT%H:%M:%S%Z	StartDateEndDateLimitInventoryRetrievalParametersrm   )strftimer   rn   r   )
r   rp   r*   
byte_range
start_dateend_datelimitrq   rparamsr:   r#   r#   r$   retrieve_inventory-  s$   !zVault.retrieve_inventoryc                 K   s   | j di |}| |S )a  
        Identical to ``retrieve_inventory``, but returns a ``Job`` instance
        instead of just the job ID.

        :type description: str
        :param description: An optional description for the job.

        :type sns_topic: str
        :param sns_topic: The Amazon SNS topic ARN where Amazon Glacier
            sends notification when the job is completed and the output
            is ready for you to download.

        :type byte_range: str
        :param byte_range: Range of bytes to retrieve.

        :type start_date: DateTime
        :param start_date: Beginning of the date range to query.

        :type end_date: DateTime
        :param end_date: End of the date range to query.

        :type limit: int
        :param limit: Limits the number of results returned.

        :rtype: :class:`boto.glacier.job.Job`
        :return: A Job object representing the retrieval job.
        Nr#   )r~   ro   )r   rf   job_idr#   r#   r$   retrieve_inventory_jobd  s   
zVault.retrieve_inventory_jobc                 C   s   | j | j|S )z
        This operation deletes an archive from the vault.

        :type archive_id: str
        :param archive_id: The ID for the archive to be deleted.
        )r   delete_archiver   )r   rh   r#   r#   r$   r     s   zVault.delete_archivec                 C   s   | j | j|}t| |S )z
        Get an object representing a job in progress.

        :type job_id: str
        :param job_id: The ID of the job

        :rtype: :class:`boto.glacier.job.Job`
        :return: A Job object representing the job.
        )r   describe_jobr   r   )r   r   r   r#   r#   r$   ro     s   

zVault.get_jobc                    s(    j  j||} fdd|d D S )a  
        Return a list of Job objects related to this vault.

        :type completed: boolean
        :param completed: Specifies the state of the jobs to return.
            If a value of True is passed, only completed jobs will
            be returned.  If a value of False is passed, only
            uncompleted jobs will be returned.  If no value is
            passed, all jobs will be returned.

        :type status_code: string
        :param status_code: Specifies the type of job status to return.
            Valid values are: InProgress|Succeeded|Failed.  If not
            specified, jobs with all status codes are returned.

        :rtype: list of :class:`boto.glacier.job.Job`
        :return: A list of Job objects related to this vault.
        c                    s   g | ]}t  |qS r#   r   )rL   jdr&   r#   r$   rM     s    z#Vault.list_jobs.<locals>.<listcomp>JobList)r   	list_jobsr   )r   	completedstatus_coder   r#   r&   r$   r     s   zVault.list_jobsc                 C   sZ   | j | j|}|d }|r'| j j| j||d}|d |d  |d }|sd|d< |S )zAutomatically make and combine multiple calls to list_parts.

        Call list_parts as necessary, combining the results in case multiple
        calls were required to get data on all available parts.

        Marker)markerrX   N)r   
list_partsr   extend)r   rA   resultr   additional_resultr#   r#   r$   r\     s   zVault.list_all_partsr   )NNNN)NN)NNNNNN)__name__
__module____qualname__r
   r?   	_MEGABYTEr.   r   r%   r'   r)   r2   r0   r>   r/   staticmethodrV   rd   ri   rr   r~   r   r   ro   r   r\   r#   r#   r#   r$   r   )   sD    




-

(
 
7	
r   )r]   boto.glacier.exceptionsr   boto.glacier.jobr   boto.glacier.writerr   r   r   boto.glacier.concurrentr   boto.glacier.utilsr	   r
   os.pathr+   r   	_GIGABYTEMAXIMUM_ARCHIVE_SIZEMAXIMUM_NUMBER_OF_PARTSobjectr   r#   r#   r#   r$   <module>   s   