

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


__snake_case				:      str   		=			"""\
    Text data.
    Second line of data."""

__snake_case				:      Optional[Any]   		=			"""file"""


@pytest.fixture(scope="""session""")
def 					_UpperCAmelCase				(						a__):

			'''simple docstring'''



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


@pytest.fixture
def 					_UpperCAmelCase				(						a__):

			'''simple docstring'''



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


@pytest.mark.parametrize("""compression_format"""  ,	["""gzip""", """xz""", """zstd"""])
def 					_UpperCAmelCase				(						a__  ,	a__  ,	a__  ,	a__  ,	a__  ,	a__):

			'''simple docstring'''



			a_						:						Optional[Any] 							= {"""gzip""": gz_file, """xz""": xz_file, """zstd""": zstd_path}
			a_						:						Union[str, Any] 							= input_paths[compression_format]
			a_						:						int 							= tmp_path / """cache"""
			a_						:						int 							= DownloadConfig(cache_dir=a__  ,	extract_compressed_file=a__)
			a_						:						str 							= cached_path(a__  ,	download_config=a__)
			with open(a__) as f:
						a_						:						Dict 							= f.read()
			with open(a__) as f:
						a_						:						Dict 							= 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				(						a__  ,	a__  ,	a__  ,	a__  ,	a__):

			'''simple docstring'''



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

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


def 					_UpperCAmelCase				(						a__):

			'''simple docstring'''



			a_						:						int 							= str(Path(a__).resolve())
			assert cached_path(a__) == text_file
			# relative path
			a_						:						List[Any] 							= str(Path(a__).resolve().relative_to(Path(os.getcwd())))
			assert cached_path(a__) == text_file


def 					_UpperCAmelCase				(						a__):

			'''simple docstring'''



			a_						:						Tuple 							= str(tmp_path.resolve() / """__missing_file__.txt""")
			with pytest.raises(a__):
						cached_path(a__)
			# relative path
			a_						:						Dict 							= """./__missing_file__.txt"""
			with pytest.raises(a__):
						cached_path(a__)


def 					_UpperCAmelCase				(						a__):

			'''simple docstring'''



			a_						:						List[str] 							= get_from_cache(f'''tmp://{tmpfs_file}''')
			with open(a__) as f:
						a_						:						Any 							= f.read()
			assert output_file_content == FILE_CONTENT


@patch("""datasets.config.HF_DATASETS_OFFLINE"""  ,	a__)
def 					_UpperCAmelCase				(						):

			'''simple docstring'''



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


@patch("""datasets.config.HF_DATASETS_OFFLINE"""  ,	a__)
def 					_UpperCAmelCase				(						a__):

			'''simple docstring'''



			a_						:						int 							= tmp_path_factory.mktemp("""data""") / """file.html"""
			with pytest.raises(a__):
						http_get("""https://huggingface.co"""  ,	temp_file=a__)
			with pytest.raises(a__):
						http_head("""https://huggingface.co""")


@patch("""datasets.config.HF_DATASETS_OFFLINE"""  ,	a__)
def 					_UpperCAmelCase				(						a__):

			'''simple docstring'''



			a_						:						Tuple 							= tmp_path_factory.mktemp("""data""") / """file.html"""
			with pytest.raises(a__):
						ftp_get("""ftp://huggingface.co"""  ,	temp_file=a__)
			with pytest.raises(a__):
						ftp_head("""ftp://huggingface.co""")



@patch("""datasets.config.HF_DATASETS_OFFLINE"""  ,	a__)
def 					_UpperCAmelCase				(						a__):

			'''simple docstring'''



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





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
)


__snake_case				:      int   		=			{
    """configuration_llama""": ["""LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP""", """LlamaConfig"""],
}

try:
	if not is_sentencepiece_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__snake_case				:      int   		=			["""LlamaTokenizer"""]

try:
	if not is_tokenizers_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__snake_case				:      int   		=			["""LlamaTokenizerFast"""]

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__snake_case				:      Union[str, Any]   		=			[
	    """LlamaForCausalLM""",
	    """LlamaModel""",
	    """LlamaPreTrainedModel""",
	    """LlamaForSequenceClassification""",
	]


if TYPE_CHECKING:
	from .configuration_llama import LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP, LlamaConfig

	try:
		if not is_sentencepiece_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_llama import LlamaTokenizer

	try:
		if not is_tokenizers_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_llama_fast import LlamaTokenizerFast

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_llama import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaPreTrainedModel


else:
	import sys

	__snake_case				:      Dict   		=			_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


