o
    ߥi(                     @   s   d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	m
Z
 d dlZG dd dedZG dd	 d	eZG d
d deZG dd deZi ZG dd deZdS )    N)ABCMetaabstractmethod)Path)	GeneratorUnionc                
   @   s   e Zd ZdZedefddZedefddZedede	ee
f dd	fd
dZe	ddede	ee
f dedd	fddZd	S )StoragezAbstract class of storage.

    All backends need to implement two apis: ``read()`` and ``read_text()``.
    ``read()`` reads the file as a byte stream and ``read_text()`` reads
    the file as texts.
    filepathc                 C      d S N selfr   r   r   J/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/fileio/file.pyread      zStorage.readc                 C   r	   r
   r   r   r   r   r   	read_text   r   zStorage.read_textobjreturnNc                 C   r	   r
   r   r   r   r   r   r   r   write   r   zStorage.writeutf-8encodingc                 C   r	   r
   r   r   r   r   r   r   r   r   
write_text!   s   zStorage.write_textr   )__name__
__module____qualname____doc__r   strr   r   bytesr   r   r   r   r   r   r   r   r      s&     
r   )	metaclassc                	   @   s   e Zd ZdZdeeef defddZ	ddeeef dedefdd	Z	d
edeeef ddfddZ
	dd
edeeef deddfddZejdeeef deeeef ddf fddZdS )LocalStoragezLocal hard disk storager   r   c                 C   s8   t |d}| }W d   |S 1 sw   Y  |S )Read data from a given ``filepath`` with 'rb' mode.

        Args:
            filepath (str or Path): Path to read data.

        Returns:
            bytes: Expected bytes object.
        rbNopenr   )r   r   fcontentr   r   r   r   ,   s   	

zLocalStorage.readr   r   c                 C   s<   t |d|d}| }W d   |S 1 sw   Y  |S )>  Read data from a given ``filepath`` with 'r' mode.

        Args:
            filepath (str or Path): Path to read data.
            encoding (str): The encoding format used to open the ``filepath``.
                Default: 'utf-8'.

        Returns:
            str: Expected text reading from ``filepath``.
        rr   Nr%   )r   r   r   r'   	value_bufr   r   r   r   9   s   

zLocalStorage.read_textr   Nc                 C   sd   t j|}|rt j|st j|dd t|d}|| W d   dS 1 s+w   Y  dS ))  Write data to a given ``filepath`` with 'wb' mode.

        Note:
            ``write`` will create a directory if the directory of ``filepath``
            does not exist.

        Args:
            obj (bytes): Data to be written.
            filepath (str or Path): Path to write data.
        Texist_okwbNospathdirnameexistsmakedirsr&   r   )r   r   r   r4   r'   r   r   r   r   J   s   "zLocalStorage.writec                 C   sh   t j|}|rt j|st j|dd t|d|d}|| W d   dS 1 s-w   Y  dS )  Write data to a given ``filepath`` with 'w' mode.

        Note:
            ``write_text`` will create a directory if the directory of
            ``filepath`` does not exist.

        Args:
            obj (str): Data to be written.
            filepath (str or Path): Path to write data.
            encoding (str): The encoding format used to open the ``filepath``.
                Default: 'utf-8'.
        Tr.   wr+   Nr1   )r   r   r   r   r4   r'   r   r   r   r   \   s   "zLocalStorage.write_textc                 c   s    |V  dS z$Only for unified API and do nothing.Nr   r   r   r   r   as_local_paths   s   
zLocalStorage.as_local_pathr   )r   r   r   r   r   r   r   r    r   r   r   r   
contextlibcontextmanagerr   r:   r   r   r   r   r"   )   s<    



r"   c                	   @   s   e Zd ZdZdd Zdd Zejdede	e
eef ddf fd	d
Zdede
eef ddfddZ	ddede
eef deddfddZdS )HTTPStoragezHTTP and HTTPS storage.c                 C      t |}|  |jS r
   )requestsgetraise_for_statusr(   r   urlr*   r   r   r   r      s   
zHTTPStorage.readc                 C   r>   r
   )r?   r@   rA   textrB   r   r   r   r      s   
zHTTPStorage.read_textr   r   Nc              	   c   P    zt jdd}|| | |  |jV  W t|j dS t|j w )ah  Download a file from ``filepath``.

        ``as_local_path`` is decorated by :meth:`contextlib.contextmanager`. It
        can be called with ``with`` statement, and when exists from the
        ``with`` statement, the temporary path will be released.

        Args:
            filepath (str): Download a file from ``filepath``.

        Examples:
            >>> storage = HTTPStorage()
            >>> # After existing from the ``with`` clause,
            >>> # the path will be removed
            >>> with storage.get_local_path('http://path/to/file') as path:
            ...     # do something here
        FdeleteNtempfileNamedTemporaryFiler   r   closenamer2   remover   r   r'   r   r   r   r:         
zHTTPStorage.as_local_pathr   rC   c                 C      t d)Nz&write is not supported by HTTP StorageNotImplementedError)r   r   rC   r   r   r   r      s   zHTTPStorage.writer   r   c                 C   rP   )Nz+write_text is not supported by HTTP StoragerQ   )r   r   rC   r   r   r   r   r         zHTTPStorage.write_textr   )r   r   r   r   r   r   r;   r<   r   r   r   r   r:   r    r   r   r   r   r   r   r=   |   s*    
r=   c                	   @   s   e Zd ZdZdddZdd Zddd	Zejd
e	de
ee	ef ddf fddZded
ee	ef ddfddZ	dde	d
ee	ef de	ddfddZdS )
OSSStoragezOSS storage.Nc                 C   rP   )Nz3OSSStorage.__init__ to be implemented in the futurerQ   )r   oss_config_filer   r   r   __init__   s   zOSSStorage.__init__c                 C   rP   )Nz/OSSStorage.read to be implemented in the futurerQ   r   r   r   r   r         zOSSStorage.readr   c                 C   rP   )Nz4OSSStorage.read_text to be implemented in the futurerQ   )r   r   r   r   r   r   r      rW   zOSSStorage.read_textr   r   c              	   c   rE   )ag  Download a file from ``filepath``.

        ``as_local_path`` is decorated by :meth:`contextlib.contextmanager`. It
        can be called with ``with`` statement, and when exists from the
        ``with`` statement, the temporary path will be released.

        Args:
            filepath (str): Download a file from ``filepath``.

        Examples:
            >>> storage = OSSStorage()
            >>> # After existing from the ``with`` clause,
            >>> # the path will be removed
            >>> with storage.get_local_path('http://path/to/file') as path:
            ...     # do something here
        FrF   NrH   rN   r   r   r   r:      rO   zOSSStorage.as_local_pathr   c                 C   rP   )Nz0OSSStorage.write to be implemented in the futurerQ   r   r   r   r   r      rW   zOSSStorage.writer   c                 C   rP   )Nz5OSSStorage.write_text to be implemented in the futurerQ   r   r   r   r   r      rS   zOSSStorage.write_textr
   r   )r   r   r   r   rV   r   r   r;   r<   r   r   r   r   r:   r    r   r   r   r   r   r   rT      s,    


rT   c                
   @   s   e Zd ZU eeeedZeed< e	dd Z
e	dedefddZe	ddeeef d
edefddZe	dedeeef ddfddZe	ddeded
eddfddZejdedeeeef ddf fddZdS )File)osshttphttpslocal_prefix_to_storagec                 C   s   t | tsJ dt|  d| vrd}n	| d\}}|}|tjv s2J d|  dttj  |tvr>tj|  t|< t| S )Nz uri should be str type, but got z://r\   zUnsupported uri z, valid prefixs: )	
isinstancer   typesplitrX   r]   listkeys
G_STORAGES)uristorage_typeprefix_r   r   r   _get_storage   s"   zFile._get_storagerd   r   c                 C   s   t | }|| S )r#   )rX   rh   r   )rd   storager   r   r   r     s   


z	File.readr   r   c                 C   s   t | }|| S )r)   )rX   rh   r   )rd   r   ri   r   r   r   r     s   

zFile.read_textr   Nc                 C   s   t |}|| |S )r-   )rX   rh   r   )r   rd   ri   r   r   r   r   #  s   
z
File.writec                 C   s   t |}|| |S )r7   )rX   rh   r   )r   rd   r   ri   r   r   r   r   2  s   
zFile.write_textc                 c   sB    t | }|| }|V  W d   dS 1 sw   Y  dS r9   )rX   rh   r:   )rd   ri   
local_pathr   r   r   r:   C  s
   
"zFile.as_local_pathr   )r   r   r   rT   r=   r"   r]   dict__annotations__staticmethodrh   r   r    r   r   r   r   r   r   r;   r<   r   r:   r   r   r   r   rX      s$   
 
" *rX   )r;   r2   rI   abcr   r   pathlibr   typingr   r   r?   r   r"   r=   rT   rc   objectrX   r   r   r   r   <module>   s   S47