
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,
)


__a         =   '\\n    Text data.\n    Second line of data.'

__a         =   'file'




@pytest.fixture(scope='''session'''   )
def 							a		(				snake_case__:   Dict   ):


				'''simple docstring'''
				lowercase_	    =   tmp_path_factory.mktemp('''data'''   ) / (FILE_PATH + '''.zstd''')
				lowercase_	    =   bytes(snake_case__		,  '''utf-8'''   )
				with zstd.open(snake_case__		,  '''wb'''   ) as f:
								f.write(snake_case__   )
				return path




@pytest.fixture
def 							a		(				snake_case__:   int   ):


				'''simple docstring'''
				with open(os.path.join(tmpfs.local_root_dir		,  snake_case__   )		,  '''w'''   ) as f:
								f.write(snake_case__   )
				return FILE_PATH




@pytest.mark.parametrize('''compression_format'''		,  ['''gzip''', '''xz''', '''zstd''']   )
def 							a		(				snake_case__:   List[Any]		,  snake_case__:   Union[str, Any]		,  snake_case__:   Tuple		,  snake_case__:   Union[str, Any]		,  snake_case__:   List[Any]		,  snake_case__:   Optional[Any]   ):


				'''simple docstring'''
				lowercase_	    =   {'''gzip''': gz_file, '''xz''': xz_file, '''zstd''': zstd_path}
				lowercase_	    =   input_paths[compression_format]
				lowercase_	    =   tmp_path / '''cache'''
				lowercase_	    =   DownloadConfig(cache_dir=snake_case__		,  extract_compressed_file=snake_case__   )
				lowercase_	    =   cached_path(snake_case__		,  download_config=snake_case__   )
				with open(snake_case__   ) as f:
								lowercase_	    =   f.read()
				with open(snake_case__   ) 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 							a		(				snake_case__:   Tuple		,  snake_case__:   Optional[int]		,  snake_case__:   Optional[int]		,  snake_case__:   List[str]		,  snake_case__:   Dict   ):


				'''simple docstring'''
				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'''		,  snake_case__   )
								monkeypatch.setattr('''datasets.config.EXTRACTED_DATASETS_PATH'''		,  str(snake_case__   )   )
								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=snake_case__   )
				    if default_cache_dir
				    else DownloadConfig(cache_dir=tmp_path / custom_cache_dir		,  extract_compressed_file=snake_case__   )
				)
				lowercase_	    =   cached_path(snake_case__		,  download_config=snake_case__   )
				assert Path(snake_case__   ).parent.parts[-2:] == expected




def 							a		(				snake_case__:   Any   ):


				'''simple docstring'''
				# absolute path
				lowercase_	    =   str(Path(snake_case__   ).resolve()   )
				assert cached_path(snake_case__   ) == text_file
				# relative path
				lowercase_	    =   str(Path(snake_case__   ).resolve().relative_to(Path(os.getcwd()   )   )   )
				assert cached_path(snake_case__   ) == text_file




def 							a		(				snake_case__:   Optional[Any]   ):


				'''simple docstring'''
				# absolute path
				lowercase_	    =   str(tmp_path.resolve() / '''__missing_file__.txt'''   )
				with pytest.raises(snake_case__   ):
								cached_path(snake_case__   )
				# relative path
				lowercase_	    =   '''./__missing_file__.txt'''
				with pytest.raises(snake_case__   ):
								cached_path(snake_case__   )




def 							a		(				snake_case__:   Union[str, Any]   ):


				'''simple docstring'''
				lowercase_	    =   get_from_cache(F'''tmp://{tmpfs_file}'''   )
				with open(snake_case__   ) as f:
								lowercase_	    =   f.read()
				assert output_file_content == FILE_CONTENT




@patch('''datasets.config.HF_DATASETS_OFFLINE'''		,  snake_case__   )
def 							a		(				):


				'''simple docstring'''
				with pytest.raises(snake_case__   ):
								cached_path('''https://huggingface.co'''   )




@patch('''datasets.config.HF_DATASETS_OFFLINE'''		,  snake_case__   )
def 							a		(				snake_case__:   Optional[int]   ):


				'''simple docstring'''
				lowercase_	    =   tmp_path_factory.mktemp('''data'''   ) / '''file.html'''
				with pytest.raises(snake_case__   ):
								http_get('''https://huggingface.co'''		,  temp_file=snake_case__   )
				with pytest.raises(snake_case__   ):
								http_head('''https://huggingface.co'''   )




@patch('''datasets.config.HF_DATASETS_OFFLINE'''		,  snake_case__   )
def 							a		(				snake_case__:   int   ):


				'''simple docstring'''
				lowercase_	    =   tmp_path_factory.mktemp('''data'''   ) / '''file.html'''
				with pytest.raises(snake_case__   ):
								ftp_get('''ftp://huggingface.co'''		,  temp_file=snake_case__   )
				with pytest.raises(snake_case__   ):
								ftp_head('''ftp://huggingface.co'''   )




@patch('''datasets.config.HF_DATASETS_OFFLINE'''		,  snake_case__   )
def 							a		(				snake_case__:   Tuple   ):


				'''simple docstring'''
				lowercase_	    =   tmp_path_factory.mktemp('''data'''   ) / '''file.html'''
				with pytest.raises(snake_case__   ):
								fsspec_get('''s3://huggingface.co'''		,  temp_file=snake_case__   )
				with pytest.raises(snake_case__   ):
								fsspec_head('''s3://huggingface.co'''   )






import pytest

import datasets.config
from datasets.utils.info_utils import is_small_dataset




@pytest.mark.parametrize('''dataset_size'''		,  [None, 400 * 2**20, 600 * 2**20]   )
@pytest.mark.parametrize('''input_in_memory_max_size'''		,  ['''default''', 0, 100 * 2**20, 900 * 2**20]   )
def 							a		(				snake_case__:   int		,  snake_case__:   Dict		,  snake_case__:   Dict   ):


				'''simple docstring'''
				if input_in_memory_max_size != "default":
								monkeypatch.setattr(datasets.config		,  '''IN_MEMORY_MAX_SIZE'''		,  snake_case__   )
				lowercase_	    =   datasets.config.IN_MEMORY_MAX_SIZE
				if input_in_memory_max_size == "default":
								assert in_memory_max_size == 0
				else:
								assert in_memory_max_size == input_in_memory_max_size
				if dataset_size and in_memory_max_size:
								lowercase_	    =   dataset_size < in_memory_max_size
				else:
								lowercase_	    =   False
				lowercase_	    =   is_small_dataset(snake_case__   )
				assert result == expected




