




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


__A						:							int							=  """hf-internal-testing/tiny-random-bert"""
__A						:							List[str]							=  os.path.join(TRANSFORMERS_CACHE, """models--hf-internal-testing--tiny-random-bert""")
__A						:							Any							=  """9b8c223d42b2188cb49d29af482996f9d0f3e5a6"""




class 			UpperCAmelCase_      (   unittest.TestCase       ):




							'''simple docstring'''



							def    _UpperCAmelCase     (					self       :	Dict			)			->					List[str]:
										SCREAMING_SNAKE_CASE           =					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:
													SCREAMING_SNAKE_CASE           =					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.
										SCREAMING_SNAKE_CASE           =					cached_file(lowerCAmelCase_       ,     lowerCAmelCase_			)
										self.assertEqual(lowerCAmelCase_       ,     lowerCAmelCase_			)

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





							def    _UpperCAmelCase     (					self       :	List[Any]			)			->					Union[str, Any]:
										with self.assertRaisesRegex(lowerCAmelCase_       ,     """is not a valid model identifier"""			):
													SCREAMING_SNAKE_CASE           =					cached_file("""tiny-random-bert"""       ,     lowerCAmelCase_			)

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

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





							def    _UpperCAmelCase     (					self       :	List[Any]			)			->					Dict:
										with self.assertRaisesRegex(lowerCAmelCase_       ,     """does not appear to have a file named"""			):
													SCREAMING_SNAKE_CASE           =					cached_file(lowerCAmelCase_       ,     """conf"""			)

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

										SCREAMING_SNAKE_CASE           =					cached_file(lowerCAmelCase_       ,     """conf"""       ,     _raise_exceptions_for_missing_entries=lowerCAmelCase_			)
										self.assertIsNone(lowerCAmelCase_			)

										SCREAMING_SNAKE_CASE           =					cached_file(lowerCAmelCase_       ,     """conf"""       ,     local_files_only=lowerCAmelCase_       ,     _raise_exceptions_for_missing_entries=lowerCAmelCase_			)
										self.assertIsNone(lowerCAmelCase_			)

										SCREAMING_SNAKE_CASE           =					mock.Mock()
										SCREAMING_SNAKE_CASE           =					500
										SCREAMING_SNAKE_CASE           =					{}
										SCREAMING_SNAKE_CASE           =					HTTPError
										SCREAMING_SNAKE_CASE           =					{}

										# 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:
													SCREAMING_SNAKE_CASE           =					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    _UpperCAmelCase     (					self       :	Union[str, Any]			)			->					Optional[Any]:
										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    _UpperCAmelCase     (					self       :	List[Any]			)			->					Tuple:
										# `get_file_from_repo` returns None if the file does not exist
										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"""			)

										SCREAMING_SNAKE_CASE           =					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.
										SCREAMING_SNAKE_CASE           =					json.loads(open(lowerCAmelCase_       ,     """r"""			).read()			)
										self.assertEqual(config["""hidden_size"""]       ,     768			)







							def    _UpperCAmelCase     (					self       :	List[Any]			)			->					List[Any]:
										with tempfile.TemporaryDirectory() as tmp_dir:
													SCREAMING_SNAKE_CASE           =					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"""			)			)












def 					lowerCamelCase_    (    SCREAMING_SNAKE_CASE   ):
			'''simple docstring'''
			if not all(char in """01""" for char in bin_string   ):
						raise ValueError("""Non-binary value was passed to the function"""   )
			if not bin_string:
						raise ValueError("""Empty string was passed to the function"""   )
			SCREAMING_SNAKE_CASE           =					""""""
			while len(SCREAMING_SNAKE_CASE   ) % 3 != 0:
						SCREAMING_SNAKE_CASE           =					"""0""" + bin_string
			SCREAMING_SNAKE_CASE           =					[
			    bin_string[index : index + 3]
			    for index in range(len(SCREAMING_SNAKE_CASE   )   )
			    if index % 3 == 0
			]
			for bin_group in bin_string_in_3_list:
						SCREAMING_SNAKE_CASE           =					0
						for index, val in enumerate(SCREAMING_SNAKE_CASE   ):
									oct_val += int(2 ** (2 - index) * int(SCREAMING_SNAKE_CASE   )   )
						oct_string += str(SCREAMING_SNAKE_CASE   )
			return oct_string


if __name__ == "__main__":
		from doctest import testmod

		testmod()






