o
    „ôjž  ã                   @   sj   d dl mZmZ d dlmZ ddlmZ ddlmZm	Z	 eƒ e	ƒ e	ƒ dœZ
ddd	„Zdd
d„Zdd„ ZdS )é    )ÚBytesIOÚStringIO)ÚPathé   )ÚFile)ÚJsonHandlerÚYamlHandler)ÚjsonÚyamlÚymlNc                 K   s  t | tƒr	t| ƒ} |du rt | tƒr|  d¡d }|tvr$td|› �ƒ‚t| }t | tƒrv|jrStt 	| ¡ƒ�}|j
|fi |¤Ž}W d  ƒ |S 1 sLw   Y  |S tt | ¡ƒ�}|j
|fi |¤Ž}W d  ƒ |S 1 sow   Y  |S t| dƒr†|j
| fi |¤Ž}|S tdƒ‚)aà  Load data from json/yaml/pickle files.

    This method provides a unified api for loading data from serialized files.

    Args:
        file (str or :obj:`Path` or file-like object): Filename or a file-like
            object.
        file_format (str, optional): If not specified, the file format will be
            inferred from the file extension, otherwise use the specified one.
            Currently supported formats include "json", "yaml/yml".

    Examples:
        >>> load('/path/of/your/file')  # file is stored in disk
        >>> load('https://path/of/your/file')  # file is stored on internet
        >>> load('oss://path/of/your/file')  # file is stored in petrel

    Returns:
        The content from the file.
    NÚ.éÿÿÿÿúUnsupported format: Úreadz."file" must be a filepath str or a file-object)Ú
isinstancer   ÚstrÚsplitÚformat_handlersÚ	TypeErrorÚ	text_moder   r   Ú	read_textÚloadr   r   Úhasattr)ÚfileÚfile_formatÚkwargsÚhandlerÚfÚobj© r   úg/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/modelscope/fileio/io.pyr      s2   


ÿ	÷	ú
ÿú
üÿr   c                 K   sV  t |tƒr	t|ƒ}|du r"t |tƒr| d¡d }n|du r"tdƒ‚|tvr-td|› �ƒ‚t| }|du r>|j| fi |¤ŽS t |tƒr–|jrnt	ƒ �}|j
| |fi |¤Ž t | ¡ |¡ W d  ƒ dS 1 sgw   Y  dS tƒ �}|j
| |fi |¤Ž t | ¡ |¡ W d  ƒ dS 1 s�w   Y  dS t|dƒr§|j
| |fi |¤Ž dS tdƒ‚)a—  Dump data to json/yaml strings or files.

    This method provides a unified api for dumping data as strings or to files.

    Args:
        obj (any): The python object to be dumped.
        file (str or :obj:`Path` or file-like object, optional): If not
            specified, then the object is dumped to a str, otherwise to a file
            specified by the filename or file-like object.
        file_format (str, optional): Same as :func:`load`.

    Examples:
        >>> dump('hello world', '/path/of/your/file')  # disk
        >>> dump('hello world', 'oss://path/of/your/file')  # oss

    Returns:
        bool: True for success, False otherwise.
    Nr   r   z0file_format must be specified since file is Noner   Úwritez."file" must be a filename str or a file-object)r   r   r   r   Ú
ValueErrorr   r   Údump_to_strr   r   Údumpr   Ú
write_textÚgetvaluer   r!   r   )r   r   r   r   r   r   r   r   r    r$   ;   s6   

ÿ
"þ"þ
r$   c                 K   s0   |t vrtd|› �ƒ‚t | }|j| fi |¤ŽS )a®  Dump data to json/yaml strings or files.

    This method provides a unified api for dumping data as strings or to files.

    Args:
        obj (any): The python object to be dumped.
        format (str, optional): Same as file_format :func:`load`.

    Examples:
        >>> dumps('hello world', 'json')  # json
        >>> dumps('hello world', 'yaml')  # yaml

    Returns:
        bool: True for success, False otherwise.
    r   )r   r   Údumps)r   Úformatr   r   r   r   r    r'   k   s   r'   )N)NN)Úior   r   Úpathlibr   r   r   r(   r   r   r   r   r$   r'   r   r   r   r    Ú<module>   s   ý

*0