o
    ߥi                     @   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   H/home/ubuntu/.local/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 sw   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