o
    Ni                     @   sZ   d dl Z d dlZd dlZd dlmZmZ d dlmZmZ ddl	m
Z
 eG dd dZdS )    N)asdict	dataclass)OptionalSet   )	PathOrStrc                   @   s   e Zd ZU dZeed< 	 eed< 	 eed< 	 dZeed< 	 dZ	e
e ed< 	 d	Zeed
< 	 edd	ddedede
e d
edd f
ddZdddZededd fddZededefddZdS )Metaa  
    Any resource that is downloaded to - or extracted in - the cache directory will
    have a meta JSON file written next to it, which corresponds to an instance
    of this class.

    In older versions of AllenNLP, this meta document just had two fields: 'url' and
    'etag'. The 'url' field is now the more general 'resource' field, but these old
    meta files are still compatible when a `Meta` is instantiated with the `.from_path()`
    class method.
    resourcecached_pathcreation_timer   sizeNetagFextraction_dir)r   r   returnc                C   s&   | t |t |t | |||dS )N)r	   r
   r   r   r   r   )strtimeget_resource_size)clsr	   r
   r   r    r   D/home/ubuntu/.local/lib/python3.10/site-packages/cached_path/meta.pynew5   s   	zMeta.newc                 C   sF   t | jd d}tt| | W d    d S 1 sw   Y  d S )Nz.jsonw)openr
   jsondumpr   )self	meta_filer   r   r   to_fileG   s   "zMeta.to_filepathc                 C   s   t |}t|N}t|}d|vr|d|d< d|vr)tj|d d |d< d|vr6|dr6d|d< d|vrB|d d |d< d	|vrO| 	|d |d	< W d    n1 sYw   Y  | d
i |S )Nr	   urlr   r   z-extracted.jsonTr
   r   r   )
r   r   r   loadpoposr   getmtimeendswithr   )r   r   r   datar   r   r   	from_pathK   s    

zMeta.from_pathc           	      C   s   t j| rt j| S t }d}t t| D ].\}}}|D ]&}t j||}t |j	}t j
|sE||vrE|| |t j|7 }qq|S )z6
        Get the size of a file or directory.
        r   )r#   r   isfilegetsizesetwalkr   joinstatst_inoislinkadd)	r   inodes
total_sizedirpath_	filenamesffpinoder   r   r   r   ]   s   
zMeta.get_resource_size)r   N)__name__
__module____qualname____doc__r   __annotations__floatr   intr   r   r   boolclassmethodr   r   r   r'   staticmethodr   r   r   r   r   r   
   sB   
 
r   )r   r#   r   dataclassesr   r   typingr   r   commonr   r   r   r   r   r   <module>   s    