o
    ƒôj(  ã                   @   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 )ÚStoragezÍAbstract 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   úi/var/www/html/academy/tools/voices/clone/cosy-venv/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'   Z	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   r3   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+   Nr0   )r   r   r   r   r3   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   r9   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?   r@   ÚtextrA   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Únamer1   Úremove©r   r   r'   r   r   r   r9   Š   ó   €
zHTTPStorage.as_local_pathr   rB   c                 C   ó   t dƒ‚)Nz&write is not supported by HTTP Storage©ÚNotImplementedError)r   r   rB   r   r   r   r   ¥   s   zHTTPStorage.writer   r   c                 C   rO   )Nz+write_text is not supported by HTTP StoragerP   )r   r   rB   r   r   r   r   r   ¨   ó   ÿzHTTPStorage.write_textr   )r   r   r   r   r   r   r:   r;   r   r   r   r   r9   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   rO   )Nz3OSSStorage.__init__ to be implemented in the futurerP   )r   Zoss_config_filer   r   r   Ú__init__³   s   ÿzOSSStorage.__init__c                 C   rO   )Nz/OSSStorage.read to be implemented in the futurerP   r   r   r   r   r   ¸   ó   ÿzOSSStorage.readr   c                 C   rO   )Nz4OSSStorage.read_text to be implemented in the futurerP   )r   r   r   r   r   r   r   ¼   rU   zOSSStorage.read_textr   r   c              	   c   rD   )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
        FrE   NrG   rM   r   r   r   r9   À   rN   zOSSStorage.as_local_pathr   c                 C   rO   )Nz0OSSStorage.write to be implemented in the futurerP   r   r   r   r   r   Û   rU   zOSSStorage.writer   c                 C   rO   )Nz5OSSStorage.write_text to be implemented in the futurerP   r   r   r   r   r   ß   rR   zOSSStorage.write_textr
   r   )r   r   r   r   rT   r   r   r:   r;   r   r   r   r   r9   r    r   r   r   r   r   r   rS   °   s,    

ÿÿýÿ
þýýrS   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)Z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://rY   zUnsupported uri z, valid prefixs: )	Ú
isinstancer   ÚtypeÚsplitrV   rZ   ÚlistÚkeysÚ
G_STORAGES)ÚuriÚstorage_typeÚprefixÚ_r   r   r   Ú_get_storageò   s"   ÿÿÿÿzFile._get_storagera   r   c                 C   s   t  | ¡}| | ¡S )r#   )rV   re   r   )ra   Ústorager   r   r   r     s   


z	File.readr   r   c                 C   s   t  | ¡}| | ¡S )r)   )rV   re   r   )ra   r   rf   r   r   r   r     s   

zFile.read_textr   Nc                 C   s   t  |¡}| | |¡S )r,   )rV   re   r   )r   ra   rf   r   r   r   r   #  s   
z
File.writec                 C   s   t  |¡}| | |¡S )r6   )rV   re   r   )r   ra   r   rf   r   r   r   r   2  s   
zFile.write_textc                 c   sB   � t  | ¡}| | ¡�}|V  W d  ƒ dS 1 sw   Y  dS r8   )rV   re   r9   )ra   rf   Ú
local_pathr   r   r   r9   C  s
   €
"ÿzFile.as_local_pathr   )r   r   r   rS   r<   r"   rZ   ÚdictÚ__annotations__Ústaticmethodre   r   r    r   r   r   r   r   r   r:   r;   r   r9   r   r   r   r   rV   ê   s$   
 ü
" *rV   )r:   r1   rH   Úabcr   r   Úpathlibr   Útypingr   r   r>   r   r"   r<   rS   r`   ÚobjectrV   r   r   r   r   Ú<module>   s   S47