U
    <ca                     @   sX   d dl Zd dlmZ d dlmZ d dlZd dlmZ G dd deZ	G dd deZ
dS )	    N)glob)cast)Storagec                   @   s   e Zd Zdd Zdd ZdS )_HasStoragec                 C   s
   || _ d S NZ_storage)selfstorage r
   C/tmp/pip-unpacked-wheel-gikjz4vx/torch/package/_directory_reader.py__init__
   s    z_HasStorage.__init__c                 C   s   | j S r   r   )r   r
   r
   r   r	      s    z_HasStorage.storageN)__name__
__module____qualname__r   r	   r
   r
   r
   r   r   	   s   r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )DirectoryReaderar  
    Class to allow PackageImporter to operate on unzipped packages. Methods
    copy the behavior of the internal PyTorchFileReader class (which is used for
    accessing packages in all other cases).

    N.B.: ScriptObjects are not depickleable or accessible via this DirectoryReader
    class due to ScriptObjects requiring an actual PyTorchFileReader instance.
    c                 C   s
   || _ d S r   )	directory)r   r   r
   r
   r   r      s    zDirectoryReader.__init__c              
   C   s:   | j  d| }t|d}| W  5 Q R  S Q R X d S )N/rb)r   openread)r   namefilenamefr
   r
   r   
get_record   s    zDirectoryReader.get_recordc                 C   s>   | j  d| }tj|| }tttj}t|j||dS )Nr   )r   nbytes)	r   torch_utilsZ_element_sizer   r   ZUntypedStorager   	from_file)r   r   ZnumelZdtyper   r   r	   r
   r
   r   get_storage_from_record#   s    z'DirectoryReader.get_storage_from_recordc                 C   s   t j| j|}t j|S r   )ospathjoinr   isfile)r   r    	full_pathr
   r
   r   
has_record)   s    zDirectoryReader.has_recordc                 C   sJ   g }t | j dddD ],}tj|s||t| jd d   q|S )Nz/**T)	recursive   )r   r   r   r    isdirappendlen)r   filesr   r
   r
   r   get_all_records-   s
    zDirectoryReader.get_all_recordsN)	r   r   r   __doc__r   r   r   r$   r+   r
   r
   r
   r   r      s   	r   )os.pathr   r   typingr   r   Ztorch.typesr   objectr   r   r
   r
   r
   r   <module>   s   