o ãÄd~jã@s„ddlZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z m Z ddlmZmZmZdZdZdZe e¡Zejee e fZd/d d „Zd d „Zd d„Zdd„Z d0dd„Zdd„Zdd„Z dd„Z!Gdd„dƒZ"Gdd„dƒZ#Gdd„dƒZ$Gdd „d ƒZ%Gd!d"„d"ƒZ&Gd#d$„d$ƒZ'Gd%d&„d&ƒZ(Gd'd(„d(e)ƒZ*Gd)d*„d*ƒZ+Gd+d,„d,e+ƒZ,Gd-d.„d.ƒZ-dS)1éN)Ú defaultdict)ÚIncompleteReadErrorÚReadTimeoutError)Ú SOCKET_ERRORÚ fallocateÚ rename_filei'liPécCsd dd„t|ƒDƒ¡S)NÚcss�|] }t tj¡VqdS©N)ÚrandomÚchoiceÚstringÚ hexdigits)Ú.0Ú_©rúk/private/var/folders/v1/_jykv66s6qd26_69j1njbrl80000gr/T/pip-target-p1gutpg6/lib/python/s3transfer/utils.pyÚ .s€z(random_file_extension..)ÚjoinÚrange)Z num_digitsrrrÚrandom_file_extension-srcKó*|dvrt|jdƒr|j ¡dSdSdS)N©Z PutObjectZ UploadPartÚsignal_not_transferring)ÚhasattrÚbodyr©ÚrequestZoperation_nameÚkwargsrrrr1ó ÿýrcKr)NrÚsignal_transferring)rrr rrrrr 8rr cCstt |t|ƒ¡ƒSr )ÚintÚmathÚceilÚfloat)ÚsizeÚ part_sizerrrÚcalculate_num_parts?sr'cCsN||}||dkrd}|durt|dƒ}n||d}d|›d|›�}|S)aÐCalculate the range parameter for multipart downloads/copies :type part_size: int :param part_size: The size of the part :type part_index: int :param part_index: The index for which this parts starts. This index starts at zero :type num_parts: int :param num_parts: The total number of parts in the transfer :returns: The value to use for Range parameter on downloads or the CopySourceRange parameter for copies ér Nzbytes=ú-)Ústr)r&Z part_indexÚ num_partsÚ total_sizeZ start_rangeZ end_rangeZ range_paramrrrÚcalculate_range_parameterCs  € r-cCsDg}|jjjD]}d|}t||ƒr| tjt||ƒ|d�¡q|S)a Retrieves callbacks from a subscriber :type transfer_future: s3transfer.futures.TransferFuture :param transfer_future: The transfer future the subscriber is associated to. :type callback_type: str :param callback_type: The type of callback to retrieve from the subscriber. Valid types include: * 'queued' * 'progress' * 'done' :returns: A list of callbacks for the type specified. All callbacks are preinjected with the transfer future. Zon_)Úfuture)ÚmetaZ call_argsZ subscribersrÚappendÚ functoolsÚpartialÚgetattr)Ztransfer_futureZ callback_typeÚ callbacksÚ subscriberZ callback_namerrrÚ get_callbacksas  ÿÿ€r6cCs |r |D] }||d�qdSdS)aŸCalls all progress callbacks :param callbacks: A list of progress callbacks to invoke :param bytes_transferred: The number of bytes transferred. This is passed to the callbacks. If no bytes were transferred the callbacks will not be invoked because no progress was achieved. It is also possible to receive a negative amount which comes from retrying a transfer request. ©Úbytes_transferredNr)r4r8ÚcallbackrrrÚinvoke_progress_callbacks~s   þr:cCs*i}| ¡D] \}}||vr|||<q|S)aoGets a dictionary filtered by whitelisted keys :param original_dict: The original dictionary of arguments to source keys and values. :param whitelisted_key: A list of keys to include in the filtered dictionary. :returns: A dictionary containing key/values from the original dictionary whose key was included in the whitelist )Úitems)Z original_dictZwhitelisted_keysZ filtered_dictÚkeyÚvaluerrrÚget_filtered_dictŽs  €r>c@seZdZdd„ZdS)ÚCallArgscKs"| ¡D] \}}t|||ƒqdS)zÞA class that records call arguments The call arguments must be passed as keyword arguments. It will set each keyword argument as an attribute of the object along with its associated value. N)r;Úsetattr)ÚselfrÚargr=rrrÚ__init__¡sÿzCallArgs.__init__N)Ú__name__Ú __module__Ú __qualname__rCrrrrr? s r?c@s(eZdZdZdd„Zdd„Zdd„ZdS) ÚFunctionContainerz¢An object that contains a function and any args or kwargs to call it When called the provided function will be called with provided args and kwargs. cOó||_||_||_dSr ©Ú_funcÚ_argsÚ_kwargs)rAÚfuncÚargsrrrrrC³s zFunctionContainer.__init__cCsd |j|j|j¡S)Nz'Function: {} with args {} and kwargs {})ÚformatrJrKrL©rArrrÚ__repr__¸s ÿzFunctionContainer.__repr__cCs|j|ji|j¤ŽSr rIrPrrrÚ__call__½szFunctionContainer.__call__N)rDrErFÚ__doc__rCrQrRrrrrrG¬s  rGc@s<eZdZdZdd„Zedd„ƒZdd„Zdd „Zd d „Z d S) ÚCountCallbackInvokerz‘An abstraction to invoke a callback when a shared count reaches zero :param callback: Callback invoke when finalized count reaches zero cCs t ¡|_||_d|_d|_dS)NrF)Ú threadingÚLockÚ_lockÚ _callbackÚ_countÚ _is_finalized©rAr9rrrrCÇs  zCountCallbackInvoker.__init__cCó0|j� |jWdƒS1swYdSr ©rWrYrPrrrÚ current_countÍs$ÿz"CountCallbackInvoker.current_countcCsH|j�|jr tdƒ‚|jd7_WdƒdS1swYdS)zIncrement the count by onez;Counter has been finalized it can no longer be incremented.r(N)rWrZÚ RuntimeErrorrYrPrrrÚ incrementÒsÿ"úzCountCallbackInvoker.incrementcCs„|j�5|jdkr tdƒ‚|jd8_|jr(|jdkr0| ¡WdƒdSWdƒdSWdƒdS1s;wYdS)zDecrement the count by onerz,Counter is at zero. It cannot dip below zeror(N)rWrYr_rZrXrPrrrÚ decrementÜs ÿ ùú"úzCountCallbackInvoker.decrementcCsT|j�d|_|jdkr| ¡WdƒdSWdƒdS1s#wYdS)z›Finalize the counter Once finalized, the counter never be incremented and the callback can be invoked once the count reaches zero TrN)rWrZrYrXrPrrrÚfinalizeçs  ý"þzCountCallbackInvoker.finalizeN) rDrErFrSrCÚpropertyr^r`rarbrrrrrTÁs  rTc@s\eZdZdZdd„Zdd„Z ddd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z dS)ÚOSUtilséÿcCs tj |¡Sr )ÚosÚpathÚgetsize©rAÚfilenamerrrÚ get_file_sizeöó zOSUtils.get_file_sizecCstj||||dd�S)NF)Úenable_callbacks)Ú ReadFileChunkÚ from_filename)rArjÚ start_byter%r4rrrÚopen_file_chunk_readerùs ÿzOSUtils.open_file_chunk_readerNcCst||||d|d�S)NF)r4rmÚclose_callbacks)rn)rAÚfileobjÚ chunk_sizeÚfull_file_sizer4rrrrrÚ#open_file_chunk_reader_from_fileobjþsúz+OSUtils.open_file_chunk_reader_from_fileobjcCs t||ƒSr )Úopen)rArjÚmoderrrrwó z OSUtils.opencCs&zt |¡WdStyYdSw)z+Remove a file, noop if file does not exist.N)rfÚremoveÚOSErrorrirrrÚ remove_files  ÿzOSUtils.remove_filecCst||ƒdSr )r)rAZcurrent_filenameZ new_filenamerrrrszOSUtils.rename_filecCsXtj |¡sdSt |¡j}t |¡rdSt |¡rdSt |¡r#dSt |¡r*dSdS)aChecks to see if a file is a special UNIX file. It checks if the file is a character special device, block special device, FIFO, or socket. :param filename: Name of the file :returns: True if the file is a special file. False, if is not. FT) rfrgÚexistsÚstatÚst_modeÚS_ISCHRÚS_ISBLKÚS_ISFIFOÚS_ISSOCK)ÚclsrjrxrrrÚis_special_files      zOSUtils.is_special_filecCsLtjtƒ}tj |¡}tj |¡}|d|jt|ƒ…|}tj ||¡Sr ) rfÚextseprrgÚdirnameÚbasenameÚ_MAX_FILENAME_LENÚlenr)rArjÚsuffixrgÚnameZ temp_filenamerrrÚget_temp_filename;s   zOSUtils.get_temp_filenamecCs\z | |d¡�}t||ƒWdƒWdS1swYWdSty-| |¡‚w)NÚwb)rwrr{r|)rArjr%ÚfrrrÚallocateBs &ÿ  þzOSUtils.allocater ) rDrErFr‰rkrqrvrwr|rr…r�r�rrrrrdós ú  rdc@sleZdZddefdd„Zdd„Zedd„ƒZdd d „Zd d „Z ddd„Z dd„Z dd„Z dd„Z dd„Zd S)ÚDeferredOpenFilerÚrbcCs"||_d|_||_||_||_dS)aóA class that defers the opening of a file till needed This is useful for deferring opening of a file till it is needed in a separate thread, as there is a limit of how many open files there can be in a single thread for most operating systems. The file gets opened in the following methods: ``read()``, ``seek()``, and ``__enter__()`` :type filename: str :param filename: The name of the file to open :type start_byte: int :param start_byte: The byte to seek to when the file is opened. :type mode: str :param mode: The mode to use to open the file :type open_function: function :param open_function: The function to use to open the file N)Ú _filenameÚ_fileobjÚ _start_byteÚ_modeÚ_open_function)rArjrprxZ open_functionrrrrCLs  zDeferredOpenFile.__init__cCs@|jdur| |j|j¡|_|jdkr|j |j¡dSdSdS©Nr)r”r—r“r–r•ÚseekrPrrrÚ_open_if_neededgs  ýz DeferredOpenFile._open_if_neededcCó|jSr )r“rPrrrrŒmszDeferredOpenFile.nameNcCs| ¡|j |¡Sr )ršr”Úread)rAÚamountrrrrœqs zDeferredOpenFile.readcCs| ¡|j |¡dSr )ršr”Úwrite)rAÚdatarrrržuszDeferredOpenFile.writecCs| ¡|j ||¡dSr )ršr”r™)rAÚwhereÚwhencerrrr™yszDeferredOpenFile.seekcCs|jdur|jS|j ¡Sr )r”r•ÚtellrPrrrr¢}s  zDeferredOpenFile.tellcCs|jr |j ¡dSdSr )r”ÚcloserPrrrr£‚sÿzDeferredOpenFile.closecCs | ¡|Sr )ršrPrrrÚ __enter__†szDeferredOpenFile.__enter__cOó | ¡dSr ©r£©rArNrrrrÚ__exit__ŠrlzDeferredOpenFile.__exit__r ©r)rDrErFrwrCršrcrŒrœržr™r¢r£r¤r¨rrrrr‘Ks    r‘c@sšeZdZ   d"dd„Ze  d#dd„ƒZdd„Zd$d d „Zd d „Zd d„Z dd„Z dd„Z d%dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zd d!„ZdS)&rnNTcCsf||_|j ¡|_|j|j||j|d�|_d|_||_|dur"g|_||_||_|dur1||_dSdS)a€ Given a file object shown below:: |___________________________________________________| 0 | | full_file_size |----chunk_size---| f.tell() :type fileobj: file :param fileobj: File like object :type chunk_size: int :param chunk_size: The max chunk size to read. Trying to read pass the end of the chunk size will behave like you've reached the end of the file. :type full_file_size: int :param full_file_size: The entire content length associated with ``fileobj``. :type callbacks: A list of function(amount_read) :param callbacks: Called whenever data is read from this object in the order provided. :type enable_callbacks: boolean :param enable_callbacks: True if to run callbacks. Otherwise, do not run callbacks :type close_callbacks: A list of function() :param close_callbacks: Called when close is called. The function should take no arguments. )Úrequested_sizerpÚactual_file_sizerN) r”r¢r•Ú_calculate_file_sizeÚ_sizeÚ _amount_readÚ _callbacksÚ_callbacks_enabledÚ_close_callbacks)rArsrtrur4rmrrrrrrC�s"* ü ÿzReadFileChunk.__init__cCs4t|dƒ}| |¡t | ¡¡j}||||||ƒS)a[Convenience factory function to create from a filename. :type start_byte: int :param start_byte: The first byte from which to start reading. :type chunk_size: int :param chunk_size: The max chunk size to read. Trying to read pass the end of the chunk size will behave like you've reached the end of the file. :type full_file_size: int :param full_file_size: The entire content length associated with ``fileobj``. :type callbacks: function(amount_read) :param callbacks: Called whenever data is read from this object. :type enable_callbacks: bool :param enable_callbacks: Indicate whether to invoke callback during read() calls. :rtype: ``ReadFileChunk`` :return: A new instance of ``ReadFileChunk`` r’)rwr™rfÚfstatÚfilenoÚst_size)r„rjrprtr4rmr�Ú file_sizerrrroÌs " zReadFileChunk.from_filenamecCs||}t||ƒSr )Úmin)rArsrªrpr«Zmax_chunk_sizerrrr¬ós z"ReadFileChunk._calculate_file_sizecCslt|j|jdƒ}|dur|}nt||ƒ}|j |¡}|jt|ƒ7_|jdur4|jr4t |jt|ƒƒ|Sr˜) Úmaxr­r®r¶r”rœrŠr¯r°r:)rAr�Z amount_leftZamount_to_readrŸrrrrœùs  zReadFileChunk.readcCó&| ¡t|jdƒr|j ¡dSdS)Nr )Úenable_callbackrr”r rPrrrr ó ÿz!ReadFileChunk.signal_transferringcCr¸)Nr)Údisable_callbackrr”rrPrrrr rºz%ReadFileChunk.signal_not_transferringcCó d|_dS©NT©r°rPrrrr¹ryzReadFileChunk.enable_callbackcCr¼)NFr¾rPrrrr»ryzReadFileChunk.disable_callbackrcCs¾|dvr td|›d�ƒ‚||j7}|dkr||j7}n |dkr$||j7}|j t||jƒ¡|jdurT|jrTtt ||j|jƒdƒ}t |j|jƒ}||}t |j|d�t||jdƒ|_dS)N)rr(ézinvalid whence (z, should be 0, 1 or 2)r(r¿rr7) Ú ValueErrorr•r®r­r”r™r·r¯r°r¶r:)rAr r¡Z bounded_whereZbounded_amount_readr�rrrr™s    ÿzReadFileChunk.seekcCs0|jdur|jr|jD]}|ƒq |j ¡dSr )r±r°r”r£r[rrrr£-s zReadFileChunk.closecCr›r )r®rPrrrr¢3szReadFileChunk.tellcCr›r )r­rPrrrÚ__len__6szReadFileChunk.__len__cCs|Sr rrPrrrr¤>szReadFileChunk.__enter__cOr¥r r¦r§rrrr¨ArlzReadFileChunk.__exit__cCstgƒSr )ÚiterrPrrrÚ__iter__DszReadFileChunk.__iter__)NTNr½r r©)rDrErFrCÚ classmethodror¬rœr rr¹r»r™r£r¢rÁr¤r¨rÃrrrrrnŽs, ù= ú&    rnc@s"eZdZdZddd„Zdd„ZdS)ÚStreamReaderProgresszr?rGrTrdr‘rnrÅÚ ExceptionrÈrÉrÙrèrrrrÚsT   ü  ÿ 2XC@%b