





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


lowercase_          =		'hf-internal-testing/tiny-random-bert'
lowercase_          =		os.path.join(TRANSFORMERS_CACHE, """models--hf-internal-testing--tiny-random-bert""")
lowercase_          =		'9b8c223d42b2188cb49d29af482996f9d0f3e5a6'
class     SCREAMING_SNAKE_CASE       (unittest.TestCase				):


   def 	SCREAMING_SNAKE_CASE_							(   self      :    str	)->       List[Any]:





     """simple docstring"""







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

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

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


   def 	SCREAMING_SNAKE_CASE_							(   self      :    int	)->       Tuple:





     """simple docstring"""







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

     with self.assertRaisesRegex(a      ,					'is not a valid git identifier'	):
       lowercase__           =  cached_file(a      ,					a      ,					revision='aaaa'	)

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


   def 	SCREAMING_SNAKE_CASE_							(   self      :    List[str]	)->       Any:





     """simple docstring"""







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

     with open(os.path.join(a      ,					'refs'      ,					'main'	)	) as f:
       lowercase__           =  f.read()
     self.assertTrue(os.path.isfile(os.path.join(a      ,					'.no_exist'      ,					a      ,					'conf'	)	)	)

     lowercase__           =  cached_file(a      ,					'conf'      ,					_raise_exceptions_for_missing_entries=a	)
     self.assertIsNone(a	)

     lowercase__           =  cached_file(a      ,					'conf'      ,					local_files_only=a      ,					_raise_exceptions_for_missing_entries=a	)
     self.assertIsNone(a	)

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

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


   def 	SCREAMING_SNAKE_CASE_							(   self      :    str	)->       Union[str, Any]:





     """simple docstring"""







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


   def 	SCREAMING_SNAKE_CASE_							(   self      :    List[str]	)->       List[str]:





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

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

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


   def 	SCREAMING_SNAKE_CASE_							(   self      :    Any	)->       Optional[Any]:





     """simple docstring"""







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

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









def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		)    ->       int:

  if exponent == 1:
    return base
  if exponent % 2 == 0:
    lowercase__           =  _modexpt(_SCREAMING_SNAKE_CASE		,						exponent // 2		,						_SCREAMING_SNAKE_CASE		) % modulo_value
    return (x * x) % modulo_value
  else:
    return (base * _modexpt(_SCREAMING_SNAKE_CASE		,						exponent - 1		,						_SCREAMING_SNAKE_CASE		)) % modulo_value




def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE = 1777		,						_SCREAMING_SNAKE_CASE = 1855		,						_SCREAMING_SNAKE_CASE = 8		)    ->       int:
  lowercase__           =  base
  for _ in range(1		,						_SCREAMING_SNAKE_CASE		):
    lowercase__           =  _modexpt(_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		,						10**digits		)

  return result


if __name__ == "__main__":
      print(f'''{solution() = }''')


