
    Vhl                        d dl mZ d dlZd dlZd dlZd dlmZ d dlZd dlm	Z	 d dl
Z
d dlZd dlmZ d dlmZ ddlmZ  ej        e          Zd	 Zeddededefd            Zej        dfdedefdZdedej        fdZdefdZdS )    )contextmanagerN)Path)rmtree)BaseContainer)	OmegaConf   )fatalc                    dd l }t          | t                    rd |                                 D             S t          | t          t
          f          rd | D             S t          | |j                  r8|                                                                 	                                S t          | t                    rt          |           S | (t          | t          t          t          t          f          r| S t          | t                    rt!          j        |           S t%          t'          |            d          )Nr   c                 4    i | ]\  }}|t          |          S  jsonable).0kvs      P/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/dora/utils.py
<dictcomp>zjsonable.<locals>.<dictcomp>   s$    99941a8A;;999    c                 ,    g | ]}t          |          S r   r   )r   r   s     r   
<listcomp>zjsonable.<locals>.<listcomp>    s    ++++++r   z is not jsonable.)torch
isinstancedictitemslisttupleTensordetachcputolistr   strintfloatboolr   r   to_container
ValueErrorrepr)valuer   s     r   r   r      s   LLL% <995;;==9999	ED%=	)	) <++U++++	E5<	(	( 	<||~~!!##**,,,	E4	 	  <5zz	*US%d,CDD	E=	)	) <%e,,,DKK:::;;;r   wb.tmppathmodesuffixc              #      K   t          |           |z   }t          ||          5 }|V  ddd           n# 1 swxY w Y   t          j        ||            dS )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_renamer4   -   s       4yy6!H	h		               Ihs   6::rbc                     	  |t          | |                    S # t          t          j        t          t
          f$ r'}t                              d| |           Y d}~dS d}~ww xY w)zt
    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_loadr@   <   sy    
tDt$$%%%V+\8D   Z#	 	 	 ttttts    "A$AA$module_namereturnc                     	 t          j        |           S # t          $ r4 t                              d| d           t          d|  d           Y d S w xY w)NzCould not import module %sT)exc_infozFailed to import module .)	importlibimport_moduleImportErrorr<   infor	   )rA   s    r   import_or_fatalrJ   K   so    9&{333 9 9 90+MMM77778888889s    :AAc                 J   dfd}t          | |           rq|                                 sJ t          j        | j        | j        dz   d          }t                              d|  d|            |                     |           d	S |                                 rJ d	S )
zReliably delete the given folder, trying to remove while ignoring errors,
    and if any files remain, renaming to some trash folder.Fc                 F    dt                               d|            d S )NTzError deleting file )r<   r=   )func
error_pathrD   errors      r   	_on_errorz"reliable_rmtree.<locals>._on_errorX   s)    :j::;;;;;r   )onerror__trash)dirprefixr-   zDeletion of z failed, moving to N)	r   existstempfilemkdtempparentnamer<   r=   r1   )r+   rP   target_namerO   s      @r   reliable_rmtreer\   S   s     E< < < < <
 4#### !{{}}}&4;ty3W_```LdLL{LLMMMK     ;;==     r   )r)   r*   )
contextlibr   rF   loggingr0   pathlibr   r8   shutilr   rW   typingtpomegaconf.basecontainerr   	omegaconfr   logr	   	getLogger__name__r<   r   r!   r4   r>   r@   AnyrJ   r\   r   r   r   <module>ri      s   & % % % % %      				                   1 1 1 1 1 1            		8	$	$< < <&  4 s 3     %kt  4     9 9 9 9 9 9!$ ! ! ! ! ! !r   