



'''simple docstring'''

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				:    List[str]     						='''\
    Text data.
    Second line of data.'''

_A				:    Optional[int]     						='''file'''







@pytest.fixture(scope='session'					)
def 						__UpperCamelCase				(							_lowercase					) ->			Dict:
	_lowercase					:       Optional[int]       =		tmp_path_factory.mktemp('data'					) / (FILE_PATH + '.zstd')
	_lowercase					:       Union[str, Any]       =		bytes(_lowercase,				'utf-8'					)
	with zstd.open(_lowercase,				'wb'					) as f:
		f.write(_lowercase					)
	return path







@pytest.fixture
def 						__UpperCamelCase				(							_lowercase					) ->			Any:
	with open(os.path.join(tmpfs.local_root_dir,				_lowercase					),				'w'					) as f:
		f.write(_lowercase					)
	return FILE_PATH







@pytest.mark.parametrize('compression_format',				['gzip', 'xz', 'zstd']					)
def 						__UpperCamelCase				(							_lowercase,				_lowercase,				_lowercase,				_lowercase,				_lowercase,				_lowercase					) ->			Any:
	_lowercase					:       Any       =		{'gzip': gz_file, 'xz': xz_file, 'zstd': zstd_path}
	_lowercase					:       List[Any]       =		input_paths[compression_format]
	_lowercase					:       Union[str, Any]       =		tmp_path / 'cache'
	_lowercase					:       Union[str, Any]       =		DownloadConfig(cache_dir=_lowercase,				extract_compressed_file=_lowercase					)
	_lowercase					:       Optional[int]       =		cached_path(_lowercase,				download_config=_lowercase					)
	with open(_lowercase					) as f:
		_lowercase					:       List[str]       =		f.read()
	with open(_lowercase					) as f:
		_lowercase					:       Optional[int]       =		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				(							_lowercase,				_lowercase,				_lowercase,				_lowercase,				_lowercase					) ->			Tuple:
	_lowercase					:       Optional[Any]       =		'custom_cache'
	_lowercase					:       Tuple       =		'custom_extracted_dir'
	_lowercase					:       List[str]       =		tmp_path / 'custom_extracted_path'
	if default_extracted:
		_lowercase					:       Union[str, Any]       =		('downloads' if default_cache_dir else custom_cache_dir, 'extracted')
	else:
		monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_DIR',				_lowercase					)
		monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_PATH',				str(_lowercase					)					)
		_lowercase					:       List[Any]       =		custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

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







def 						__UpperCamelCase				(							_lowercase					) ->			Union[str, Any]:
	# absolute path
	_lowercase					:       Tuple       =		str(Path(_lowercase					).resolve()					)
	assert cached_path(_lowercase					) == text_file
	# relative path
	_lowercase					:       Dict       =		str(Path(_lowercase					).resolve().relative_to(Path(os.getcwd()					)					)					)
	assert cached_path(_lowercase					) == text_file







def 						__UpperCamelCase				(							_lowercase					) ->			Union[str, Any]:
	# absolute path
	_lowercase					:       List[Any]       =		str(tmp_path.resolve() / '__missing_file__.txt'					)
	with pytest.raises(_lowercase					):
		cached_path(_lowercase					)
	# relative path
	_lowercase					:       Tuple       =		'./__missing_file__.txt'
	with pytest.raises(_lowercase					):
		cached_path(_lowercase					)







def 						__UpperCamelCase				(							_lowercase					) ->			Tuple:
	_lowercase					:       List[Any]       =		get_from_cache(f'''tmp://{tmpfs_file}'''					)
	with open(_lowercase					) as f:
		_lowercase					:       Optional[Any]       =		f.read()
	assert output_file_content == FILE_CONTENT







@patch('datasets.config.HF_DATASETS_OFFLINE',				_lowercase					)
def 						__UpperCamelCase				(							) ->			Dict:
	with pytest.raises(_lowercase					):
		cached_path('https://huggingface.co'					)







@patch('datasets.config.HF_DATASETS_OFFLINE',				_lowercase					)
def 						__UpperCamelCase				(							_lowercase					) ->			List[str]:
	_lowercase					:       Dict       =		tmp_path_factory.mktemp('data'					) / 'file.html'
	with pytest.raises(_lowercase					):
		http_get('https://huggingface.co',				temp_file=_lowercase					)
	with pytest.raises(_lowercase					):
		http_head('https://huggingface.co'					)







@patch('datasets.config.HF_DATASETS_OFFLINE',				_lowercase					)
def 						__UpperCamelCase				(							_lowercase					) ->			Optional[Any]:
	_lowercase					:       str       =		tmp_path_factory.mktemp('data'					) / 'file.html'
	with pytest.raises(_lowercase					):
		ftp_get('ftp://huggingface.co',				temp_file=_lowercase					)
	with pytest.raises(_lowercase					):
		ftp_head('ftp://huggingface.co'					)







@patch('datasets.config.HF_DATASETS_OFFLINE',				_lowercase					)
def 						__UpperCamelCase				(							_lowercase					) ->			List[str]:
	_lowercase					:       Optional[int]       =		tmp_path_factory.mktemp('data'					) / 'file.html'
	with pytest.raises(_lowercase					):
		fsspec_get('s3://huggingface.co',				temp_file=_lowercase					)
	with pytest.raises(_lowercase					):
		fsspec_head('s3://huggingface.co'					)





'''simple docstring'''

_A				:    Dict     						='''
# Transformers installation
! pip install transformers datasets
# To install from source instead of the last release, comment the command above and uncomment the following one.
# ! pip install git+https://github.com/huggingface/transformers.git
'''

_A				:    Dict     						=[{'''type''': '''code''', '''content''': INSTALL_CONTENT}]
_A				:    Dict     						={
    '''{processor_class}''': '''FakeProcessorClass''',
    '''{model_class}''': '''FakeModelClass''',
    '''{object_class}''': '''FakeObjectClass''',
}
