


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


__snake_case      = "hf-internal-testing/tiny-random-bert"
__snake_case      = os.path.join(TRANSFORMERS_CACHE, "models--hf-internal-testing--tiny-random-bert")
__snake_case      = "9b8c223d42b2188cb49d29af482996f9d0f3e5a6"




class  UpperCAmelCase       (							unittest.TestCase     ):




			def 	lowerCamelCase_       (    self				:		Dict    ):







						"""simple docstring"""







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

						# File is cached at the same place the second time.
						UpperCamelCase								=       cached_file(__magic_name__							,							__magic_name__    )
						self.assertEqual(__magic_name__							,							__magic_name__    )

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




			def 	lowerCamelCase_       (    self				:		Optional[int]    ):







						"""simple docstring"""







						with self.assertRaisesRegex(__magic_name__							,							"""is not a valid model identifier"""    ):
									UpperCamelCase								=       cached_file("""tiny-random-bert"""							,							__magic_name__    )

						with self.assertRaisesRegex(__magic_name__							,							"""is not a valid git identifier"""    ):
									UpperCamelCase								=       cached_file(__magic_name__							,							__magic_name__							,							revision="""aaaa"""    )

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




			def 	lowerCamelCase_       (    self				:		str    ):







						"""simple docstring"""







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

						with open(os.path.join(__magic_name__							,							"""refs"""							,							"""main"""    )    ) as f:
									UpperCamelCase								=       f.read()
						self.assertTrue(os.path.isfile(os.path.join(__magic_name__							,							""".no_exist"""							,							__magic_name__							,							"""conf"""    )    )    )

						UpperCamelCase								=       cached_file(__magic_name__							,							"""conf"""							,							_raise_exceptions_for_missing_entries=__magic_name__    )
						self.assertIsNone(__magic_name__    )

						UpperCamelCase								=       cached_file(__magic_name__							,							"""conf"""							,							local_files_only=__magic_name__							,							_raise_exceptions_for_missing_entries=__magic_name__    )
						self.assertIsNone(__magic_name__    )

						UpperCamelCase								=       mock.Mock()
						UpperCamelCase								=       5_0_0
						UpperCamelCase								=       {}
						UpperCamelCase								=       HTTPError
						UpperCamelCase								=       {}

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




			def 	lowerCamelCase_       (    self				:		Union[str, Any]    ):







						"""simple docstring"""







						self.assertTrue(has_file("""hf-internal-testing/tiny-bert-pt-only"""							,							__magic_name__    )    )
						self.assertFalse(has_file("""hf-internal-testing/tiny-bert-pt-only"""							,							__magic_name__    )    )
						self.assertFalse(has_file("""hf-internal-testing/tiny-bert-pt-only"""							,							__magic_name__    )    )




			def 	lowerCamelCase_       (    self				:		int    ):







						"""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(__magic_name__							,							"""is not a valid model identifier"""    ):
									get_file_from_repo("""bert-base-case"""							,							__magic_name__    )

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

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





			def 	lowerCamelCase_       (    self				:		Optional[Any]    ):







						"""simple docstring"""







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

									self.assertIsNone(get_file_from_repo(__magic_name__							,							"""b.txt"""    )    )






'''simple docstring'''
import enum
import shutil
import sys


lowercase__				, lowercase__											=shutil.get_terminal_size()

lowercase__											={'UP': 'A', 'DOWN': 'B', 'RIGHT': 'C', 'LEFT': 'D'}







class   a_ (      enum.Enum					):
			lowerCamelCase__	:			int       =    0
			lowerCamelCase__	:			Union[str, Any]       =    1







def      UpperCamelCase_				(      A__  ,      A__=""						):
	sys.stdout.write(str(A__						) + end						)
	sys.stdout.flush()






def      UpperCamelCase_				(      A__  ,      A__  ,      A__=""						):
	forceWrite(F'''\u001b[{color}m{content}\u001b[0m'''  ,      A__						)






def      UpperCamelCase_				(      ):
	forceWrite("""\r"""						)






def      UpperCamelCase_				(      A__  ,      A__						):
	forceWrite(F'''\033[{num_lines}{CURSOR_TO_CHAR[direction.upper()]}'''						)






def      UpperCamelCase_				(      ):
	forceWrite(""" """ * TERMINAL_WIDTH						)
	reset_cursor()






def      UpperCamelCase_				(      ):
	reset_cursor()
	forceWrite("""-""" * TERMINAL_WIDTH						)
