
    +j                        d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	m
Z
 d dlZ	 d dlmZ n# e$ r  ej        d           Y nw xY wdZe	g ef         Ze G d d	                      Zed
eee         z  de
eee         ef                  fd            Zeddfd
eee         z  dededz  dedz  deedz  edz  f         f
dZdS )    N)contextmanager)	dataclass)IOAnyCallableIterator)blake3z%WARNING: blake3 package not installedi   c                   D    e Zd ZU dZeed<   eed<   dZeed<   dZeed<   dS )HashCheckpointz9Saved state for resuming an interrupted hash computation.bytes_processedhasherr   mtime_ns	file_sizeN)	__name__
__module____qualname____doc__int__annotations__r   r   r        ;/home/wildlama/comfy/ComfyUI/app/assets/services/hashing.pyr   r      sM         CCKKKHcIsr   r   fpreturnc              #     K   t          | d          r t          | dd                       }d}|rH	 |                                 }|dk    r|                     d           n# t          j        $ r d}Y nw xY w	 | dfV  ||                     |           dS dS # ||                     |           w w xY wt          t          j        |           d          5 }|dfV  ddd           dS # 1 swxY w Y   dS )	z=Yield (file_object, is_path) with appropriate setup/teardown.readseekablec                      dS )NFr   r   r   r   <lambda>z#_open_for_hashing.<locals>.<lambda>    s    5 r   Nr   FrbT)	hasattrgetattrtellseekioUnsupportedOperationopenosfspath)r   r   orig_posfs       r   _open_for_hashingr,      sw      r6 972z==99;; 	  7799q==GGAJJJ*       	"e)OOO#!!!!! $#x#!!!! $ ")B--&& 	!T'MMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s/   /A" "A65A6:B B5C..C25C2
chunk_sizeinterrupt_check
checkpointc                    |dk    rt           }t          |           5 \  }}|+|r)|                    |j                   |j        }|j        }nt                      }d}	 |9 |            r/|rdt          ||          fcddd           S 	 ddd           dS |                    |          }|sn(|                    |           |t          |          z  }{|
                                dfcddd           S # 1 swxY w Y   dS )a  Compute BLAKE3 hash of a file, with optional checkpoint support.

    Args:
        fp: File path or file-like object
        chunk_size: Size of chunks to read at a time
        interrupt_check: Optional callable that returns True if the operation
            should be interrupted (e.g. paused or cancelled). Must be
            non-blocking so file handles are released immediately. Checked
            between chunk reads.
        checkpoint: Optional checkpoint to resume from (file paths only)

    Returns:
        Tuple of (hex_digest, None) on completion, or
        (None, checkpoint) on interruption (file paths only), or
        (None, None) on interruption of a file object
    r   NT)r   r   )NN)DEFAULT_CHUNKr,   r$   r   r   r	   r   r   updatelen	hexdigest)	r   r-   r.   r/   r+   is_pathhr   chunks	            r   compute_blake3_hashr8   3   s   , Q"
	2		 #,1g!g!FF:-...!A(8OOAO	**/@/@* (7 " " " # # # # # # # #  "!# # # # # # # #" FF:&&E HHUOOOs5zz)O	* {{}}d"/# # # # # # # # # # # # # # # # # #s   A"C<C<AC<<D D )r%   r(   
contextlibr   dataclassesr   typingr   r   r   r   loggingr	   ModuleNotFoundErrorwarningr1   boolInterruptCheckr   strbytestupler,   r   r8   r   r   r   <module>rD      s   				 				 % % % % % % ! ! ! ! ! ! . . . . . . . . . . . . = = = =GO;<<<<<=  "d(#         #5	/ huRY_7M.N    0 $-1(,	0# 0#bi0#0# $d*0# %	0#
 3:~,,-0# 0# 0# 0# 0# 0#s   - AA