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


lowerCamelCase_    :		Optional[int]							=	"""\
    Text data.
    Second line of data."""

lowerCamelCase_    :		int							=	"""file"""
@pytest.fixture(scope='session'						)
def 							lowerCAmelCase(     __lowerCamelCase						):
					__a		 =       tmp_path_factory.mktemp('data'						) / (FILE_PATH + '.zstd')
					__a		 =       bytes(__lowerCamelCase				,    'utf-8'						)
					with zstd.open(__lowerCamelCase				,    'wb'						) as f:
										f.write(__lowerCamelCase						)
					return path
@pytest.fixture
def 							lowerCAmelCase(     __lowerCamelCase						):
					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 							lowerCAmelCase(     __lowerCamelCase				,    __lowerCamelCase				,    __lowerCamelCase				,    __lowerCamelCase				,    __lowerCamelCase				,    __lowerCamelCase						):
					__a		 =       {'gzip': gz_file, 'xz': xz_file, 'zstd': zstd_path}
					__a		 =       input_paths[compression_format]
					__a		 =       tmp_path / 'cache'
					__a		 =       DownloadConfig(cache_dir=__lowerCamelCase				,    extract_compressed_file=__lowerCamelCase						)
					__a		 =       cached_path(__lowerCamelCase				,    download_config=__lowerCamelCase						)
					with open(__lowerCamelCase						) as f:
										__a		 =       f.read()
					with open(__lowerCamelCase						) as f:
										__a		 =       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 							lowerCAmelCase(     __lowerCamelCase				,    __lowerCamelCase				,    __lowerCamelCase				,    __lowerCamelCase				,    __lowerCamelCase						):
					__a		 =       'custom_cache'
					__a		 =       'custom_extracted_dir'
					__a		 =       tmp_path / 'custom_extracted_path'
					if default_extracted:
										__a		 =       ('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		 =       custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

					__a		 =       xz_file
					__a		 =       (
					    DownloadConfig(extract_compressed_file=__lowerCamelCase						)
					    if default_cache_dir
					    else DownloadConfig(cache_dir=tmp_path / custom_cache_dir				,    extract_compressed_file=__lowerCamelCase						)
					)
					__a		 =       cached_path(__lowerCamelCase				,    download_config=__lowerCamelCase						)
					assert Path(__lowerCamelCase						).parent.parts[-2:] == expected
def 							lowerCAmelCase(     __lowerCamelCase						):
					# absolute path
					__a		 =       str(Path(__lowerCamelCase						).resolve()						)
					assert cached_path(__lowerCamelCase						) == text_file
					# relative path
					__a		 =       str(Path(__lowerCamelCase						).resolve().relative_to(Path(os.getcwd()						)						)						)
					assert cached_path(__lowerCamelCase						) == text_file
def 							lowerCAmelCase(     __lowerCamelCase						):
					# absolute path
					__a		 =       str(tmp_path.resolve() / '__missing_file__.txt'						)
					with pytest.raises(__lowerCamelCase						):
										cached_path(__lowerCamelCase						)
					# relative path
					__a		 =       './__missing_file__.txt'
					with pytest.raises(__lowerCamelCase						):
										cached_path(__lowerCamelCase						)
def 							lowerCAmelCase(     __lowerCamelCase						):
					__a		 =       get_from_cache(f'''tmp://{tmpfs_file}'''						)
					with open(__lowerCamelCase						) as f:
										__a		 =       f.read()
					assert output_file_content == FILE_CONTENT
@patch('datasets.config.HF_DATASETS_OFFLINE'				,    __lowerCamelCase						)
def 							lowerCAmelCase(     ):
					with pytest.raises(__lowerCamelCase						):
										cached_path('https://huggingface.co'						)
@patch('datasets.config.HF_DATASETS_OFFLINE'				,    __lowerCamelCase						)
def 							lowerCAmelCase(     __lowerCamelCase						):
					__a		 =       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 							lowerCAmelCase(     __lowerCamelCase						):
					__a		 =       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 							lowerCAmelCase(     __lowerCamelCase						):
					__a		 =       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'						)





import copy
import json
import os
import tempfile

from transformers import is_torch_available

from .test_configuration_utils import config_common_kwargs


class 						a__							(       __snake_case		):



			def __init__(						self				,						UpperCAmelCase				,						UpperCAmelCase=None				,						UpperCAmelCase=True				,						UpperCAmelCase=None				,						**UpperCAmelCase   )			->   List[Any]:
								__a		 =       parent
								__a		 =       config_class
								__a		 =       has_text_modality
								__a		 =       kwargs
								__a		 =       common_properties



			def __SCREAMING_SNAKE_CASE							(						self   )			->   List[str]:
								__a		 =       self.config_class(**self.inputs_dict   )
								__a		 =       (
								    ['hidden_size', 'num_attention_heads', 'num_hidden_layers']
								    if self.common_properties is None
								    else self.common_properties
								)

								# Add common fields for text models
								if self.has_text_modality:
													common_properties.extend(['vocab_size']   )

								# Test that config has the common properties as getters
								for prop in common_properties:
													self.parent.assertTrue(hasattr(UpperCAmelCase				,						UpperCAmelCase   )				,						msg=f'''`{prop}` does not exist'''   )

								# Test that config has the common properties as setter
								for idx, name in enumerate(UpperCAmelCase   ):
													try:
																		setattr(UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase   )
																		self.parent.assertEqual(
																		    getattr(UpperCAmelCase				,						UpperCAmelCase   )				,						UpperCAmelCase				,						msg=f'''`{name} value {idx} expected, but was {getattr(UpperCAmelCase				,						UpperCAmelCase   )}'''   )
													except NotImplementedError:
																		# Some models might not be able to implement setters for common_properties
																		# In that case, a NotImplementedError is raised
																		pass

        # Test if config class can be called with Config(prop_name=..)
								for idx, name in enumerate(UpperCAmelCase   ):
													try:
																		__a		 =       self.config_class(**{name: idx}   )
																		self.parent.assertEqual(
																		    getattr(UpperCAmelCase				,						UpperCAmelCase   )				,						UpperCAmelCase				,						msg=f'''`{name} value {idx} expected, but was {getattr(UpperCAmelCase				,						UpperCAmelCase   )}'''   )
													except NotImplementedError:
																		# Some models might not be able to implement setters for common_properties
																		# In that case, a NotImplementedError is raised
																		pass



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Tuple:
								__a		 =       self.config_class(**self.inputs_dict   )
								__a		 =       json.loads(config.to_json_string()   )
								for key, value in self.inputs_dict.items():
													self.parent.assertEqual(obj[key]				,						UpperCAmelCase   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Tuple:
								__a		 =       self.config_class(**self.inputs_dict   )

								with tempfile.TemporaryDirectory() as tmpdirname:
													__a		 =       os.path.join(UpperCAmelCase				,						'config.json'   )
													config_first.to_json_file(UpperCAmelCase   )
													__a		 =       self.config_class.from_json_file(UpperCAmelCase   )

								self.parent.assertEqual(config_second.to_dict()				,						config_first.to_dict()   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Tuple:
								__a		 =       self.config_class(**self.inputs_dict   )

								with tempfile.TemporaryDirectory() as tmpdirname:
													config_first.save_pretrained(UpperCAmelCase   )
													__a		 =       self.config_class.from_pretrained(UpperCAmelCase   )

								self.parent.assertEqual(config_second.to_dict()				,						config_first.to_dict()   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   str:
								__a		 =       self.config_class(**self.inputs_dict   )

								__a		 =       'test'
								with tempfile.TemporaryDirectory() as tmpdirname:
													__a		 =       os.path.join(UpperCAmelCase				,						UpperCAmelCase   )
													config_first.save_pretrained(UpperCAmelCase   )
													__a		 =       self.config_class.from_pretrained(UpperCAmelCase				,						subfolder=UpperCAmelCase   )

								self.parent.assertEqual(config_second.to_dict()				,						config_first.to_dict()   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Union[str, Any]:
								__a		 =       self.config_class(**self.inputs_dict				,						num_labels=5   )
								self.parent.assertEqual(len(config.idalabel   )				,						5   )
								self.parent.assertEqual(len(config.labelaid   )				,						5   )

								__a		 =       3
								self.parent.assertEqual(len(config.idalabel   )				,						3   )
								self.parent.assertEqual(len(config.labelaid   )				,						3   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   List[str]:
								if self.config_class.is_composition:
													return
								__a		 =       self.config_class()
								self.parent.assertIsNotNone(UpperCAmelCase   )



			def __SCREAMING_SNAKE_CASE							(						self   )			->   Union[str, Any]:
								__a		 =       copy.deepcopy(UpperCAmelCase   )
								__a		 =       self.config_class(**UpperCAmelCase   )
								__a		 =       []
								for key, value in config_common_kwargs.items():
													if key == "torch_dtype":
																		if not is_torch_available():
																							continue
																		else:
																							import torch

																							if config.torch_dtype != torch.floataa:
																												wrong_values.append(('torch_dtype', config.torch_dtype, torch.floataa)   )
													elif getattr(UpperCAmelCase				,						UpperCAmelCase   ) != value:
																		wrong_values.append((key, getattr(UpperCAmelCase				,						UpperCAmelCase   ), value)   )

								if len(UpperCAmelCase   ) > 0:
													__a		 =       '\n'.join([f'''- {v[0]}: got {v[1]} instead of {v[2]}''' for v in wrong_values]   )
													raise ValueError(f'''The following keys were not properly set in the config:\n{errors}'''   )







			def __SCREAMING_SNAKE_CASE							(						self   )			->   Optional[Any]:
								self.create_and_test_config_common_properties()
								self.create_and_test_config_to_json_string()
								self.create_and_test_config_to_json_file()
								self.create_and_test_config_from_and_save_pretrained()
								self.create_and_test_config_from_and_save_pretrained_subfolder()
								self.create_and_test_config_with_num_labels()
								self.check_config_can_be_init_without_params()
								self.check_config_arguments_init()




