
    uj(                         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e          Z G d d	e          Z G d
 de          Z G d de          Zi Z G d de          ZdS )    N)ABCMetaabstractmethod)Path)	GeneratorUnionc            
           e Zd ZdZedefd            Zedefd            Zedede	ee
f         ddfd            Ze	 ddede	ee
f         d
eddf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                     d S N selfr
   s     f/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/modelscope/fileio/file.pyreadzStorage.read           c                     d S r   r   r   s     r   	read_textzStorage.read_text   r   r   objreturnNc                     d S r   r   r   r   r
   s      r   writezStorage.write   r   r   utf-8encodingc                     d S r   r   r   r   r
   r   s       r   
write_textzStorage.write_text!   s	    
 	r   r   )__name__
__module____qualname____doc__r   strr   r   bytesr   r   r   r   r   r   r   r	   r	      s         S    ^ #    ^  %T	*: t    ^  $+ "39- ! 04   ^  r   r	   )	metaclassc            	          e Zd ZdZdeeef         defdZ	 ddeeef         dedefdZ	dedeeef         dd	fd
Z
	 ddedeeef         dedd	fdZej        deeef         deeeef         d	d	f         fd            Zd	S )LocalStoragezLocal hard disk storager
   r   c                 ~    t          |d          5 }|                                }ddd           n# 1 swxY 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contents       r   r   zLocalStorage.read,   sx     (D!! 	QffhhG	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   266r   r   c                     t          |d|          5 }|                                }ddd           n# 1 swxY 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_bufs        r   r   zLocalStorage.read_text9   s~     (C(333 	!qI	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!s   488r   Nc                 .   t           j                            |          }|r5t           j                            |          st          j        |d           t          |d          5 }|                    |           ddd           dS # 1 swxY 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
   r>   r/   s        r   r   zLocalStorage.writeJ   s     '//(++ 	027>>'22 	0K$////(D!! 	QGGCLLL	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   'B

BBc                 2   t           j                            |          }|r5t           j                            |          st          j        |d           t          |d|          5 }|                    |           ddd           dS # 1 swxY 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'.
        Tr8   wr4   Nr;   )r   r   r
   r   r>   r/   s         r   r   zLocalStorage.write_text\   s      '//(++ 	027>>'22 	0K$////(C(333 	qGGCLLL	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   )BBBc              #      K   |V  dS z$Only for unified API and do nothing.Nr   r   s     r   as_local_pathzLocalStorage.as_local_paths   s       r   r    )r!   r"   r#   r$   r   r%   r   r&   r   r   r   r   
contextlibcontextmanagerr   rF   r   r   r   r)   r)   )   s[       !!U39- %     #* !#t),.1   " %T	*: t    * $+ "39- ! 04   . C ! " '0c4i0@$0L&M     r   r)   c            	           e Zd ZdZd Zd Zej        dede	e
eef         ddf         fd            Zded	e
eef         ddfd
Z	 dded	e
eef         deddfdZdS )HTTPStoragezHTTP and HTTPS storage.c                 `    t          j        |          }|                                 |j        S r   )requestsgetraise_for_statusr0   r   urlr3   s      r   r   zHTTPStorage.read   s+    L	yr   c                 `    t          j        |          }|                                 |j        S r   )rL   rM   rN   textrO   s      r   r   zHTTPStorage.read_text   s*    L	vr   r
   r   Nc              #   .  K   	 t          j        d          }|                    |                     |                     |                                 |j        V  t          j        |j                   dS # t          j        |j                   w xY 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namer<   remover   r
   r/   s      r   rF   zHTTPStorage.as_local_path         &	+5999AGGDIIh''(((GGIII&LLLIafBIaf   AA9 9Br   rP   c                      t          d          )Nz&write is not supported by HTTP StorageNotImplementedError)r   r   rP   s      r   r   zHTTPStorage.write   s    !"JKKKr   r   r   c                      t          d          )Nz+write_text is not supported by HTTP Storager`   )r   r   rP   r   s       r   r   zHTTPStorage.write_text   s     "9; ; 	;r   r    )r!   r"   r#   r$   r   r   rG   rH   r%   r   r   r   rF   r&   r   r   r   r   r   rJ   rJ   |   s       !!    
 $-eCI.>d.J$K   4L LU39%5 L$ L L L L $+; ;;c4i(; !; 04; ; ; ; ; ;r   rJ   c            	           e Zd ZdZddZd ZddZej        de	de
ee	ef         ddf         fd	            Zd
edee	ef         ddfdZ	 dd
e	dee	ef         de	ddfdZdS )
OSSStoragezOSS storage.Nc                      t          d          )Nz3OSSStorage.__init__ to be implemented in the futurer`   )r   oss_config_files     r   __init__zOSSStorage.__init__   s    !AC C 	Cr   c                      t          d          )Nz/OSSStorage.read to be implemented in the futurer`   r   s     r   r   zOSSStorage.read   s    !=? ? 	?r   r   c                      t          d          )Nz4OSSStorage.read_text to be implemented in the futurer`   )r   r
   r   s      r   r   zOSSStorage.read_text   s    !BD D 	Dr   r
   r   c              #   .  K   	 t          j        d          }|                    |                     |                     |                                 |j        V  t          j        |j                   dS # t          j        |j                   w xY w)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
        FrT   NrV   r\   s      r   rF   zOSSStorage.as_local_path   r]   r^   r   c                      t          d          )Nz0OSSStorage.write to be implemented in the futurer`   r   s      r   r   zOSSStorage.write   s    !>@ @ 	@r   r   c                      t          d          )Nz5OSSStorage.write_text to be implemented in the futurer`   r   s       r   r   zOSSStorage.write_text   s     "CE E 	Er   r   r    )r!   r"   r#   r$   rg   r   r   rG   rH   r%   r   r   r   rF   r&   r   r   r   r   r   rd   rd      s1       C C C C
? ? ?D D D D $-eCI.>d.J$K   4@ @%T	*: @t @ @ @ @ $+E EE"39-E !E 04E E E E E Er   rd   c            
       >   e Zd ZU eeeedZeed<   e	d             Z
e	dedefd            Ze	ddeeef         dedefd	            Ze	d
edeeef         ddfd            Ze	dd
edededdfd            Zej        dedeeeef         ddf         fd            ZdS )File)osshttphttpslocal_prefix_to_storagec                    t          | t                    sJ dt          |                        d| vrd}n|                     d          \  }}|}|t          j        v s9J d|  dt          t          j                                                               |t          vr!t	          j        |                     t          |<   t          |         S )Nz uri should be str type, but got z://rr   zUnsupported uri z, valid prefixs: )	
isinstancer%   typesplitrn   rs   listkeys
G_STORAGES)uristorage_typeprefix_s       r   _get_storagezFile._get_storage   s    #  	O 	O N499 N N	O 	O  "LL		%((IFA!Lt66666s 6 6D+0022336 6 766 z))'+'>|'L'N'NJ|$,''r   r{   r   c                 `    t                               |           }|                    |           S )r+   )rn   r   r   )r{   storages     r   r   z	File.read  s)     ##C((||C   r   r   r   c                 `    t                               |           }|                    |           S )r2   )rn   r   r   )r{   r   r   s      r   r   zFile.read_text  s+     ##C((  %%%r   r   Nc                 b    t                               |          }|                    | |          S )r7   )rn   r   r   )r   r{   r   s      r   r   z
File.write#  s+     ##C((}}S#&&&r   c                 b    t                               |          }|                    | |          S )rB   )rn   r   r   )r   r{   r   r   s       r   r   zFile.write_text2  s-     ##C((!!#s+++r   c              #      K   t                               |           }|                    |           5 }|V  ddd           dS # 1 swxY w Y   dS rE   )rn   r   rF   )r{   r   
local_paths      r   rF   zFile.as_local_pathC  s       ##C((""3'' 	:	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AAAr    )r!   r"   r#   rd   rJ   r)   rs   dict__annotations__staticmethodr   r%   r&   r   r   r   r   r   r   rG   rH   r   rF   r   r   r   rn   rn      s        	       ( ( \(( 
!# 
!% 
! 
! 
! \
! & &uS$Y' &3 &S & & & \& '5 'uS$Y/ 'D ' ' ' \' , , ,# , ,4 , , , \,  3 9U39-=tT-I#J      r   rn   )rG   r<   rW   abcr   r   pathlibr   typingr   r   rL   r	   r)   rJ   rd   rz   objectrn   r   r   r   <module>r      su       				  ' ' ' ' ' ' ' '       # # # # # # # #         8P P P P P7 P P Pf1; 1; 1; 1; 1;' 1; 1; 1;h4E 4E 4E 4E 4E 4E 4E 4En 
^ ^ ^ ^ ^6 ^ ^ ^ ^ ^r   