




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				:				List[Any]       							=		'hf-internal-testing/tiny-random-bert'
_snake_case				:				Union[str, Any]       							=		os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert')
_snake_case				:				str       							=		'9b8c223d42b2188cb49d29af482996f9d0f3e5a6'

class       A   (		unittest.TestCase   ):







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


														"""simple docstring"""


														_a     					=    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:
																					_a     					=    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.
														_a     					=    cached_file(lowerCAmelCase_							,      lowerCAmelCase_ )
														self.assertEqual(lowerCAmelCase_							,      lowerCAmelCase_ )

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







							def        __lowerCAmelCase      (  self	:  str )      ->							str:


														"""simple docstring"""


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

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

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







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


														"""simple docstring"""


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

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

														_a     					=    cached_file(lowerCAmelCase_							,      '''conf'''							,      _raise_exceptions_for_missing_entries=lowerCAmelCase_ )
														self.assertIsNone(lowerCAmelCase_ )

														_a     					=    cached_file(lowerCAmelCase_							,      '''conf'''							,      local_files_only=lowerCAmelCase_							,      _raise_exceptions_for_missing_entries=lowerCAmelCase_ )
														self.assertIsNone(lowerCAmelCase_ )

														_a     					=    mock.Mock()
														_a     					=    5_00
														_a     					=    {}
														_a     					=    HTTPError
														_a     					=    {}

														# 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:
																					_a     					=    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        __lowerCAmelCase      (  self	:  Dict )      ->							str:


														"""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        __lowerCAmelCase      (  self	:  int )      ->							Optional[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(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''' )

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







							def        __lowerCAmelCase      (  self	:  Tuple )      ->							Dict:


														"""simple docstring"""


														with tempfile.TemporaryDirectory() as tmp_dir:
																					_a     					=    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''' ) )







'''simple docstring'''


from ..utils import DummyObject, requires_backends

class       A   (		metaclass=_a   ):
							lowercase_          =    ['torch', 'scipy']







							def __init__(  self	:  Tuple							,      *lowerCAmelCase_	:  str							,      **lowerCAmelCase_	:  Any )      ->							Tuple:


														"""simple docstring"""


														requires_backends(self							,      ['''torch''', '''scipy'''] )







							@classmethod
							def        __lowerCAmelCase      (  cls	:  Dict							,      *lowerCAmelCase_	:  Dict							,      **lowerCAmelCase_	:  str )      ->							Tuple:


														"""simple docstring"""


														requires_backends(cls							,      ['''torch''', '''scipy'''] )







							@classmethod
							def        __lowerCAmelCase      (  cls	:  Optional[int]							,      *lowerCAmelCase_	:  str							,      **lowerCAmelCase_	:  List[str] )      ->							str:


														"""simple docstring"""


														requires_backends(cls							,      ['''torch''', '''scipy'''] )

