



import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


SCREAMING_SNAKE_CASE__      :			List[str]							=		"""hf-internal-testing/tiny-random-bert"""
SCREAMING_SNAKE_CASE__      :			Any							=		os.path.join(TRANSFORMERS_CACHE, """models--hf-internal-testing--tiny-random-bert""")
SCREAMING_SNAKE_CASE__      :			str							=		"""9b8c223d42b2188cb49d29af482996f9d0f3e5a6"""
class 						lowerCamelCase_				(      unittest.TestCase ):







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:List[Any]        =      cached_file(__lowerCAmelCase       ,					__lowerCAmelCase     )
								# Should have downloaded the file in here
								self.assertTrue(os.path.isdir(__lowerCAmelCase     )     )
								# Cache should contain at least those three subfolders:
								for subfolder in ["blobs", "refs", "snapshots"]:
												self.assertTrue(os.path.isdir(os.path.join(__lowerCAmelCase       ,					__lowerCAmelCase     )     )     )
								with open(os.path.join(__lowerCAmelCase       ,					'''refs'''       ,					'''main'''     )     ) as f:
												__magic_name__		:List[str]        =      f.read()
								self.assertEqual(__lowerCAmelCase       ,					os.path.join(__lowerCAmelCase       ,					'''snapshots'''       ,					__lowerCAmelCase       ,					__lowerCAmelCase     )     )
								self.assertTrue(os.path.isfile(__lowerCAmelCase     )     )

								# File is cached at the same place the second time.
								__magic_name__		:Optional[int]        =      cached_file(__lowerCAmelCase       ,					__lowerCAmelCase     )
								self.assertEqual(__lowerCAmelCase       ,					__lowerCAmelCase     )

								# Using a specific revision to test the full commit hash.
								__magic_name__		:str        =      cached_file(__lowerCAmelCase       ,					__lowerCAmelCase       ,					revision='''9b8c223'''     )
								self.assertEqual(__lowerCAmelCase       ,					os.path.join(__lowerCAmelCase       ,					'''snapshots'''       ,					__lowerCAmelCase       ,					__lowerCAmelCase     )     )







				def 			A	(						self     ):
								"""simple docstring"""

								with self.assertRaisesRegex(__lowerCAmelCase       ,					'''is not a valid model identifier'''     ):
												__magic_name__		:List[str]        =      cached_file('''tiny-random-bert'''       ,					__lowerCAmelCase     )

								with self.assertRaisesRegex(__lowerCAmelCase       ,					'''is not a valid git identifier'''     ):
												__magic_name__		:Optional[Any]        =      cached_file(__lowerCAmelCase       ,					__lowerCAmelCase       ,					revision='''aaaa'''     )

								with self.assertRaisesRegex(__lowerCAmelCase       ,					'''does not appear to have a file named'''     ):
												__magic_name__		:Optional[Any]        =      cached_file(__lowerCAmelCase       ,					'''conf'''     )







				def 			A	(						self     ):
								"""simple docstring"""

								with self.assertRaisesRegex(__lowerCAmelCase       ,					'''does not appear to have a file named'''     ):
												__magic_name__		:Any        =      cached_file(__lowerCAmelCase       ,					'''conf'''     )

								with open(os.path.join(__lowerCAmelCase       ,					'''refs'''       ,					'''main'''     )     ) as f:
												__magic_name__		:Optional[int]        =      f.read()
								self.assertTrue(os.path.isfile(os.path.join(__lowerCAmelCase       ,					'''.no_exist'''       ,					__lowerCAmelCase       ,					'''conf'''     )     )     )

								__magic_name__		:Dict        =      cached_file(__lowerCAmelCase       ,					'''conf'''       ,					_raise_exceptions_for_missing_entries=__lowerCAmelCase     )
								self.assertIsNone(__lowerCAmelCase     )

								__magic_name__		:str        =      cached_file(__lowerCAmelCase       ,					'''conf'''       ,					local_files_only=__lowerCAmelCase       ,					_raise_exceptions_for_missing_entries=__lowerCAmelCase     )
								self.assertIsNone(__lowerCAmelCase     )

								__magic_name__		:Tuple        =      mock.Mock()
								__magic_name__		:Dict        =      5_0_0
								__magic_name__		:Union[str, Any]        =      {}
								__magic_name__		:Dict        =      HTTPError
								__magic_name__		:List[str]        =      {}

								# Under the mock environment we get a 500 error when trying to reach the tokenizer.
								with mock.patch('''requests.Session.request'''       ,					return_value=__lowerCAmelCase     ) as mock_head:
												__magic_name__		:List[Any]        =      cached_file(__lowerCAmelCase       ,					'''conf'''       ,					_raise_exceptions_for_connection_errors=__lowerCAmelCase     )
												self.assertIsNone(__lowerCAmelCase     )
												# This check we did call the fake head request
												mock_head.assert_called()







				def 			A	(						self     ):
								"""simple docstring"""
								self.assertTrue(has_file('''hf-internal-testing/tiny-bert-pt-only'''       ,					__lowerCAmelCase     )     )
								self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only'''       ,					__lowerCAmelCase     )     )
								self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only'''       ,					__lowerCAmelCase     )     )







				def 			A	(						self     ):
								"""simple docstring"""
								self.assertIsNone(get_file_from_repo('''bert-base-cased'''       ,					'''ahah.txt'''     )     )

								# The function raises if the repository does not exist.
								with self.assertRaisesRegex(__lowerCAmelCase       ,					'''is not a valid model identifier'''     ):
												get_file_from_repo('''bert-base-case'''       ,					__lowerCAmelCase     )

								# The function raises if the revision does not exist.
								with self.assertRaisesRegex(__lowerCAmelCase       ,					'''is not a valid git identifier'''     ):
												get_file_from_repo('''bert-base-cased'''       ,					__lowerCAmelCase       ,					revision='''ahaha'''     )

								__magic_name__		:Dict        =      get_file_from_repo('''bert-base-cased'''       ,					__lowerCAmelCase     )
								# The name is the cached name which is not very easy to test, so instead we load the content.
								__magic_name__		:Any        =      json.loads(open(__lowerCAmelCase       ,					'''r'''     ).read()     )
								self.assertEqual(config['''hidden_size''']       ,					7_6_8     )







				def 			A	(						self     ):
								"""simple docstring"""

								with tempfile.TemporaryDirectory() as tmp_dir:
												__magic_name__		:Dict        =      Path(__lowerCAmelCase     ) / '''a.txt'''
												filename.touch()
												self.assertEqual(get_file_from_repo(__lowerCAmelCase       ,					'''a.txt'''     )       ,					str(__lowerCAmelCase     )     )

												self.assertIsNone(get_file_from_repo(__lowerCAmelCase       ,					'''b.txt'''     )     )














from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available


SCREAMING_SNAKE_CASE__      :			Union[str, Any]							=		{"""tokenization_herbert""": ["""HerbertTokenizer"""]}

try:
					if not is_tokenizers_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					SCREAMING_SNAKE_CASE__      :			Optional[Any]							=		["""HerbertTokenizerFast"""]


if TYPE_CHECKING:
					from .tokenization_herbert import HerbertTokenizer

					try:
										if not is_tokenizers_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_herbert_fast import HerbertTokenizerFast

else:
					import sys

					SCREAMING_SNAKE_CASE__      :			Union[str, Any]							=		_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)






