
    ]il                     ,   S SK Jr  S SKrS SKrS SKrS SKJr  S SKrS SKJ	r	  S SK
r
S SKrS SKJr  S SKJr  SSKJr  \R&                  " \5      rS	 r\SS
\S\S\4S jj5       r\R2                  S4S
\S\4S jjrS\S\R6                  4S jrS
\4S jrg)    )contextmanagerN)Path)rmtree)BaseContainer)	OmegaConf   )fatalc                    SS K n[        U [        5      (       a/  U R                  5        VVs0 s H  u  p#U[	        U5      _M     snn$ [        U [
        [        45      (       a  U  Vs/ s H  n[	        U5      PM     sn$ [        XR                  5      (       a,  U R                  5       R                  5       R                  5       $ [        U [        5      (       a  [        U 5      $ U b%  [        U [        [        [        [        45      (       a  U $ [        U [         5      (       a  ["        R$                  " U 5      $ ['        [)        U 5       S35      es  snnf s  snf )Nr   z is not jsonable.)torch
isinstancedictitemsjsonablelisttupleTensordetachcputolistr   strintfloatboolr   r   to_container
ValueErrorrepr)valuer   kvs       G/mnt/rpi/tmp/demucs-venv-sys/lib/python3.13/site-packages/dora/utils.pyr   r      s   %+0;;=9=418A;=99	ED%=	)	)%*+UU++	E<<	(	(||~!!#**,,	E4	 	 5z	*US%d,CDD	E=	)	)%%e,,DK=(9:;; :+s   E(Epathmodesuffixc              #      #    [        U 5      U-   n[        X15       nUv   SSS5        [        R                  " X05        g! , (       d  f       N%= f7f)a'  
Write to a temporary file with the given suffix, then rename it
to the right filename. As renaming a file is usually much faster
than writing it, this removes (or highly limits as far as I understand NFS)
the likelihood of leaving a half-written checkpoint behind, if killed
at the wrong time.
N)r   openosrename)r!   r"   r#   tmp_pathfs        r    write_and_renamer*   -   s=      4y6!H	h	 
IIh 
	s   AA A 
A
Arbc                      U" [        X5      5      $ ! [        [        R                  [        [
        4 a   n[        R                  SX5         SnAgSnAff = f)zh
Try to load from a path using torch.load, and handles various failure cases.
Return None upon failure.
zLAn error happened when trying to load from %s, this file will be ignored: %rN)r%   OSErrorpickleUnpicklingErrorRuntimeErrorEOFErrorloggerwarning)r!   loadr"   excs       r    try_loadr6   <   sM    
D$%%V++\8D Z	 s    $AAAmodule_namereturnc                      [         R                  " U 5      $ ! [         a'    [        R	                  SU SS9  [        SU  S35         g f = f)NzCould not import module %sT)exc_infozFailed to import module .)	importlibimport_moduleImportErrorr2   infor	   )r7   s    r    import_or_fatalr@   K   sL    9&&{33 90+M(Q789s    .A	A	c                 F  ^ SmU4S jn[        XS9  T(       aq  U R                  5       (       d   e[        R                  " U R                  U R
                  S-   SS9n[        R                  SU  SU 35        U R                  U5        g	U R                  5       (       a   eg	)
zReliably delete the given folder, trying to remove while ignoring errors,
and if any files remain, renaming to some trash folder.Fc                 :   > Sm[         R                  SU 35        g )NTzError deleting file )r2   r3   )func
error_pathr:   errors      r    	_on_error"reliable_rmtree.<locals>._on_errorX   s    -j\:;    )onerror__trash)dirprefixr#   zDeletion of z failed, moving to N)	r   existstempfilemkdtempparentnamer2   r3   r'   )r!   rF   target_namerE   s      @r    reliable_rmtreerT   S   s     E<
 4#{{}}}&&4;;tyy3W_`dV+>{mLMK ;;==  =rH   )wbz.tmp)
contextlibr   r<   loggingr&   pathlibr   r.   shutilr   rO   typingtpomegaconf.basecontainerr   	omegaconfr   logr	   	getLogger__name__r2   r   r   r*   r4   r6   Anyr@   rT    rH   r    <module>rc      s    &   	      1  			8	$<& 4 s 3   %kkt 4  9 9 9!$ !rH   