



import os
from pathlib import Path
from unittest.mock import patch

import pytest
import zstandard as zstd

from datasets.download.download_config import DownloadConfig
from datasets.utils.file_utils import (
    OfflineModeIsEnabled,
    cached_path,
    fsspec_get,
    fsspec_head,
    ftp_get,
    ftp_head,
    get_from_cache,
    http_get,
    http_head,
)


SCREAMING_SNAKE_CASE_:Dict             =						"""\
    Text data.
    Second line of data."""

SCREAMING_SNAKE_CASE_:List[str]             =						"""file"""




@pytest.fixture(scope="""session"""		)
def 	__UpperCamelCase	(  _lowerCAmelCase		)       ->  int:



    """simple docstring"""






    A :       Optional[int]          =   tmp_path_factory.mktemp("""data"""		) / (FILE_PATH + """.zstd""")
    A :       Optional[Any]          =   bytes(_lowerCAmelCase      ,     """utf-8"""		)
    with zstd.open(_lowerCAmelCase      ,     """wb"""		) as f:
        f.write(_lowerCAmelCase		)
    return path




@pytest.fixture
def 	__UpperCamelCase	(  _lowerCAmelCase		)       ->  str:



    """simple docstring"""






    with open(os.path.join(tmpfs.local_root_dir      ,     _lowerCAmelCase		)      ,     """w"""		) as f:
        f.write(_lowerCAmelCase		)
    return FILE_PATH




@pytest.mark.parametrize("""compression_format"""      ,     ["""gzip""", """xz""", """zstd"""]		)
def 	__UpperCamelCase	(  _lowerCAmelCase      ,     _lowerCAmelCase      ,     _lowerCAmelCase      ,     _lowerCAmelCase      ,     _lowerCAmelCase      ,     _lowerCAmelCase		)       ->  Tuple:



    """simple docstring"""






    A :       List[Any]          =   {"""gzip""": gz_file, """xz""": xz_file, """zstd""": zstd_path}
    A :       Optional[Any]          =   input_paths[compression_format]
    A :       int          =   tmp_path / """cache"""
    A :       str          =   DownloadConfig(cache_dir=_lowerCAmelCase      ,     extract_compressed_file=_lowerCAmelCase		)
    A :       Dict          =   cached_path(_lowerCAmelCase      ,     download_config=_lowerCAmelCase		)
    with open(_lowerCAmelCase		) as f:
        A :       str          =   f.read()
    with open(_lowerCAmelCase		) as f:
        A :       List[str]          =   f.read()
    assert extracted_file_content == expected_file_content




@pytest.mark.parametrize("""default_extracted"""      ,     [True, False]		)
@pytest.mark.parametrize("""default_cache_dir"""      ,     [True, False]		)
def 	__UpperCamelCase	(  _lowerCAmelCase      ,     _lowerCAmelCase      ,     _lowerCAmelCase      ,     _lowerCAmelCase      ,     _lowerCAmelCase		)       ->  Dict:



    """simple docstring"""






    A :       Union[str, Any]          =   """custom_cache"""
    A :       str          =   """custom_extracted_dir"""
    A :       int          =   tmp_path / """custom_extracted_path"""
    if default_extracted:
        A :       List[str]          =   ("""downloads""" if default_cache_dir else custom_cache_dir, """extracted""")
    else:
        monkeypatch.setattr("""datasets.config.EXTRACTED_DATASETS_DIR"""      ,     _lowerCAmelCase		)
        monkeypatch.setattr("""datasets.config.EXTRACTED_DATASETS_PATH"""      ,     str(_lowerCAmelCase		)		)
        A :       Dict          =   custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

    A :       Optional[int]          =   xz_file
    A :       Dict          =   (
        DownloadConfig(extract_compressed_file=_lowerCAmelCase		)
        if default_cache_dir
        else DownloadConfig(cache_dir=tmp_path / custom_cache_dir      ,     extract_compressed_file=_lowerCAmelCase		)
    )
    A :       Optional[Any]          =   cached_path(_lowerCAmelCase      ,     download_config=_lowerCAmelCase		)
    assert Path(_lowerCAmelCase		).parent.parts[-2:] == expected




def 	__UpperCamelCase	(  _lowerCAmelCase		)       ->  Dict:



    """simple docstring"""






    A :       Dict          =   str(Path(_lowerCAmelCase		).resolve()		)
    assert cached_path(_lowerCAmelCase		) == text_file
    # relative path
    A :       List[str]          =   str(Path(_lowerCAmelCase		).resolve().relative_to(Path(os.getcwd()		)		)		)
    assert cached_path(_lowerCAmelCase		) == text_file




def 	__UpperCamelCase	(  _lowerCAmelCase		)       ->  List[Any]:



    """simple docstring"""






    A :       List[str]          =   str(tmp_path.resolve() / """__missing_file__.txt"""		)
    with pytest.raises(_lowerCAmelCase		):
        cached_path(_lowerCAmelCase		)
    # relative path
    A :       Optional[int]          =   """./__missing_file__.txt"""
    with pytest.raises(_lowerCAmelCase		):
        cached_path(_lowerCAmelCase		)




def 	__UpperCamelCase	(  _lowerCAmelCase		)       ->  List[str]:



    """simple docstring"""






    A :       Union[str, Any]          =   get_from_cache(f'''tmp://{tmpfs_file}'''		)
    with open(_lowerCAmelCase		) as f:
        A :       Any          =   f.read()
    assert output_file_content == FILE_CONTENT




@patch("""datasets.config.HF_DATASETS_OFFLINE"""      ,     _lowerCAmelCase		)
def 	__UpperCamelCase	(  )       ->  Optional[Any]:



    """simple docstring"""






    with pytest.raises(_lowerCAmelCase		):
        cached_path("""https://huggingface.co"""		)




@patch("""datasets.config.HF_DATASETS_OFFLINE"""      ,     _lowerCAmelCase		)
def 	__UpperCamelCase	(  _lowerCAmelCase		)       ->  List[Any]:



    """simple docstring"""






    A :       List[str]          =   tmp_path_factory.mktemp("""data"""		) / """file.html"""
    with pytest.raises(_lowerCAmelCase		):
        http_get("""https://huggingface.co"""      ,     temp_file=_lowerCAmelCase		)
    with pytest.raises(_lowerCAmelCase		):
        http_head("""https://huggingface.co"""		)




@patch("""datasets.config.HF_DATASETS_OFFLINE"""      ,     _lowerCAmelCase		)
def 	__UpperCamelCase	(  _lowerCAmelCase		)       ->  Tuple:



    """simple docstring"""






    A :       int          =   tmp_path_factory.mktemp("""data"""		) / """file.html"""
    with pytest.raises(_lowerCAmelCase		):
        ftp_get("""ftp://huggingface.co"""      ,     temp_file=_lowerCAmelCase		)
    with pytest.raises(_lowerCAmelCase		):
        ftp_head("""ftp://huggingface.co"""		)




@patch("""datasets.config.HF_DATASETS_OFFLINE"""      ,     _lowerCAmelCase		)
def 	__UpperCamelCase	(  _lowerCAmelCase		)       ->  Any:



    """simple docstring"""






    A :       Dict          =   tmp_path_factory.mktemp("""data"""		) / """file.html"""
    with pytest.raises(_lowerCAmelCase		):
        fsspec_get("""s3://huggingface.co"""      ,     temp_file=_lowerCAmelCase		)
    with pytest.raises(_lowerCAmelCase		):
        fsspec_head("""s3://huggingface.co"""		)







from ..utils import DummyObject, requires_backends






class SCREAMING_SNAKE_CASE__     (		metaclass=SCREAMING_SNAKE_CASE__    ):




    '''simple docstring'''






    __lowerCamelCase			: List[str]					=      ["torch", "transformers", "onnx"]






    def __init__(       self,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(self,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






class SCREAMING_SNAKE_CASE__     (		metaclass=SCREAMING_SNAKE_CASE__    ):




    '''simple docstring'''






    __lowerCamelCase			: Dict					=      ["torch", "transformers", "onnx"]






    def __init__(       self,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(self,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






class SCREAMING_SNAKE_CASE__     (		metaclass=SCREAMING_SNAKE_CASE__    ):




    '''simple docstring'''






    __lowerCamelCase			: Any					=      ["torch", "transformers", "onnx"]






    def __init__(       self,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(self,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






class SCREAMING_SNAKE_CASE__     (		metaclass=SCREAMING_SNAKE_CASE__    ):




    '''simple docstring'''






    __lowerCamelCase			: Optional[int]					=      ["torch", "transformers", "onnx"]






    def __init__(       self,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(self,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






class SCREAMING_SNAKE_CASE__     (		metaclass=SCREAMING_SNAKE_CASE__    ):




    '''simple docstring'''






    __lowerCamelCase			: Optional[int]					=      ["torch", "transformers", "onnx"]






    def __init__(       self,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(self,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






class SCREAMING_SNAKE_CASE__     (		metaclass=SCREAMING_SNAKE_CASE__    ):




    '''simple docstring'''






    __lowerCamelCase			: Optional[int]					=      ["torch", "transformers", "onnx"]






    def __init__(       self,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(self,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)






    @classmethod
    def 			_lowerCAmelCase					(       cls,		*lowerCamelCase__,		**lowerCamelCase__		):
        requires_backends(cls,		["""torch""", """transformers""", """onnx"""]		)


