o
    rril                     @   s   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eZd	d
 ZeddededefddZejdfdedefddZdedejfddZdefddZdS )    )contextmanagerN)Path)rmtree)BaseContainer)	OmegaConf   )fatalc                 C   s   dd l }t| trdd |  D S t| ttfr dd | D S t| |jr.|   	 S t| t
r7t| S | d u sDt| ttttfrF| S t| trPt| S tt|  d)Nr   c                 S   s   i | ]	\}}|t |qS  jsonable).0kvr	   r	   >/home/ubuntu/.local/lib/python3.10/site-packages/dora/utils.py
<dictcomp>   s    zjsonable.<locals>.<dictcomp>c                 S   s   g | ]}t |qS r	   r
   )r   r   r	   r	   r   
<listcomp>    s    zjsonable.<locals>.<listcomp>z is not jsonable.)torch
isinstancedictitemslisttupleTensordetachcputolistr   strintfloatboolr   r   to_container
ValueErrorrepr)valuer   r	   r	   r   r      s   



r   wb.tmppathmodesuffixc                 c   sN    t | | }t||}|V  W d   n1 sw   Y  t||  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fr	   r	   r   write_and_rename-   s   	r.   rbc                 C   sP   z|t | |W S  ttjttfy' } ztd| | W Y d}~dS d}~w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'   excr	   r	   r   try_load<   s   r9   module_namereturnc                 C   sB   zt | W S  ty    tjd| dd td|  d Y d S w )NzCould not import module %sT)exc_infozFailed to import module .)	importlibimport_moduleImportErrorr5   infor   )r:   r	   r	   r   import_or_fatalK   s   rB   c                    sx   d  fdd}t | |d  r4|  sJ tj| j| jd d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                    s   d t d|  d S )NTzError deleting file )r5   r6   )func
error_pathr<   errorr	   r   	_on_errorX   s   z"reliable_rmtree.<locals>._on_error)onerror__trash)dirprefixr(   zDeletion of z failed, moving to N)	r   existstempfilemkdtempparentnamer5   r6   r+   )r&   rG   target_namer	   rE   r   reliable_rmtreeS   s   rS   )r$   r%   )
contextlibr   r>   loggingr*   pathlibr   r1   shutilr   rN   typingtpomegaconf.basecontainerr   	omegaconfr   logr   	getLogger__name__r5   r   r   r.   r7   r9   AnyrB   rS   r	   r	   r	   r   <module>   s&   
