
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__							=				"""\
    Text data.
    Second line of data."""

SCREAMING_SNAKE_CASE__							=				"""file"""




@pytest.fixture(scope='session'			)
def 							__SCREAMING_SNAKE_CASE   (		SCREAMING_SNAKE_CASE       :      Dict			)     ->						int:
				__lowercase								=					tmp_path_factory.mktemp('data'			) / (FILE_PATH + """.zstd""")
				__lowercase								=					bytes(_lowerCAmelCase    ,							'utf-8'			)
				with zstd.open(_lowerCAmelCase    ,							'wb'			) as f:
								f.write(_lowerCAmelCase			)
				return path




@pytest.fixture
def 							__SCREAMING_SNAKE_CASE   (		SCREAMING_SNAKE_CASE       :      Optional[int]			)     ->						str:






				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 							__SCREAMING_SNAKE_CASE   (		SCREAMING_SNAKE_CASE       :      List[Any]    ,							SCREAMING_SNAKE_CASE       :      Any    ,							SCREAMING_SNAKE_CASE       :      Union[str, Any]    ,							SCREAMING_SNAKE_CASE       :      int    ,							SCREAMING_SNAKE_CASE       :      List[str]    ,							SCREAMING_SNAKE_CASE       :      List[str]			)     ->						Tuple:
				__lowercase								=					{"""gzip""": gz_file, """xz""": xz_file, """zstd""": zstd_path}
				__lowercase								=					input_paths[compression_format]
				__lowercase								=					tmp_path / """cache"""
				__lowercase								=					DownloadConfig(cache_dir=_lowerCAmelCase    ,							extract_compressed_file=_lowerCAmelCase			)
				__lowercase								=					cached_path(_lowerCAmelCase    ,							download_config=_lowerCAmelCase			)
				with open(_lowerCAmelCase			) as f:
								__lowercase								=					f.read()
				with open(_lowerCAmelCase			) as f:
								__lowercase								=					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 							__SCREAMING_SNAKE_CASE   (		SCREAMING_SNAKE_CASE       :      Union[str, Any]    ,							SCREAMING_SNAKE_CASE       :      List[str]    ,							SCREAMING_SNAKE_CASE       :      Dict    ,							SCREAMING_SNAKE_CASE       :      Any    ,							SCREAMING_SNAKE_CASE       :      str			)     ->						Dict:
				__lowercase								=					"""custom_cache"""
				__lowercase								=					"""custom_extracted_dir"""
				__lowercase								=					tmp_path / """custom_extracted_path"""
				if default_extracted:
								__lowercase								=					("""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			)			)
								__lowercase								=					custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

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




def 							__SCREAMING_SNAKE_CASE   (		SCREAMING_SNAKE_CASE       :      Optional[Any]			)     ->						Dict:
				__lowercase								=					str(Path(_lowerCAmelCase			).resolve()			)
				assert cached_path(_lowerCAmelCase			) == text_file
				# relative path
				__lowercase								=					str(Path(_lowerCAmelCase			).resolve().relative_to(Path(os.getcwd()			)			)			)
				assert cached_path(_lowerCAmelCase			) == text_file




def 							__SCREAMING_SNAKE_CASE   (		SCREAMING_SNAKE_CASE       :      List[Any]			)     ->						List[Any]:
				__lowercase								=					str(tmp_path.resolve() / '__missing_file__.txt'			)
				with pytest.raises(_lowerCAmelCase			):
								cached_path(_lowerCAmelCase			)
				# relative path
				__lowercase								=					"""./__missing_file__.txt"""
				with pytest.raises(_lowerCAmelCase			):
								cached_path(_lowerCAmelCase			)




def 							__SCREAMING_SNAKE_CASE   (		SCREAMING_SNAKE_CASE       :      Dict			)     ->						List[str]:
				__lowercase								=					get_from_cache(F"""tmp://{tmpfs_file}"""			)
				with open(_lowerCAmelCase			) as f:
								__lowercase								=					f.read()
				assert output_file_content == FILE_CONTENT




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






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




@patch('datasets.config.HF_DATASETS_OFFLINE'    ,							_lowerCAmelCase			)
def 							__SCREAMING_SNAKE_CASE   (		SCREAMING_SNAKE_CASE       :      Dict			)     ->						List[Any]:
				__lowercase								=					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 							__SCREAMING_SNAKE_CASE   (		SCREAMING_SNAKE_CASE       :      List[Any]			)     ->						Tuple:
				__lowercase								=					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 							__SCREAMING_SNAKE_CASE   (		SCREAMING_SNAKE_CASE       :      Dict			)     ->						Any:
				__lowercase								=					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"""]		)


