





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


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






class 					lowercase__ (			unittest.TestCase       ):

   '''simple docstring'''


   def    UpperCamelCase__						(					self							)				->   str:






       """simple docstring"""
       UpperCamelCase__							:       List[Any]      =   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__							:       str      =   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__							:       Optional[Any]      =   cached_file(__magic_name__,	__magic_name__							)
       self.assertEqual(__magic_name__,	__magic_name__							)

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




   def    UpperCamelCase__						(					self							)				->   str:






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

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

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




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






       """simple docstring"""
       with self.assertRaisesRegex(__magic_name__,	'''does not appear to have a file named'''							):
           UpperCamelCase__							:       Union[str, Any]      =   cached_file(__magic_name__,	'''conf'''							)

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

       UpperCamelCase__							:       Tuple      =   cached_file(__magic_name__,	'''conf''',	_raise_exceptions_for_missing_entries=__magic_name__							)
       self.assertIsNone(__magic_name__							)

       UpperCamelCase__							:       Optional[int]      =   cached_file(__magic_name__,	'''conf''',	local_files_only=__magic_name__,	_raise_exceptions_for_missing_entries=__magic_name__							)
       self.assertIsNone(__magic_name__							)

       UpperCamelCase__							:       Optional[int]      =   mock.Mock()
       UpperCamelCase__							:       List[Any]      =   500
       UpperCamelCase__							:       Optional[int]      =   {}
       UpperCamelCase__							:       Tuple      =   HTTPError
       UpperCamelCase__							:       int      =   {}

       # 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__							:       Optional[int]      =   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    UpperCamelCase__						(					self							)				->   int:






       """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    UpperCamelCase__						(					self							)				->   Optional[Any]:






       """simple docstring"""
       # `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(__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__							:       Optional[Any]      =   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__							:       Tuple      =   json.loads(open(__magic_name__,	'''r'''							).read()							)
       self.assertEqual(config['''hidden_size'''],	768							)




   def    UpperCamelCase__						(					self							)				->   Dict:






       """simple docstring"""
       with tempfile.TemporaryDirectory() as tmp_dir:
           UpperCamelCase__							:       Dict      =   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'''							)							)








# Lint as: python3
import itertools
import os
import re


UpperCAmelCase_  							=				re.compile(R'([A-Z]+)([A-Z][a-z])')
UpperCAmelCase_  							=				re.compile(R'([a-z\d])([A-Z])')

UpperCAmelCase_  							=				re.compile(R'(?<!_)_(?!_)')
UpperCAmelCase_  							=				re.compile(R'(_{2,})')

UpperCAmelCase_  							=				R'^\w+(\.\w+)*$'

UpperCAmelCase_  							=				R'<>:/\|?*'




def 	lowerCAmelCase_					(						__UpperCAmelCase:    Optional[int]   )   ->			List[Any]:
    UpperCamelCase__							:       int      =   _uppercase_uppercase_re.sub(r'''\1_\2'''	,     __UpperCAmelCase   )
    UpperCamelCase__							:       Tuple      =   _lowercase_uppercase_re.sub(r'''\1_\2'''	,     __UpperCAmelCase   )
    return name.lower()




def 	lowerCAmelCase_					(						__UpperCAmelCase:    Any   )   ->			Any:
    UpperCamelCase__							:       Optional[int]      =   _single_underscore_re.split(__UpperCAmelCase   )
    UpperCamelCase__							:       Dict      =   [_multiple_underscores_re.split(__UpperCAmelCase   ) for n in name]
    return "".join(n.capitalize() for n in itertools.chain.from_iterable(__UpperCAmelCase   ) if n != ''''''   )




def 	lowerCAmelCase_					(						__UpperCAmelCase:    int   )   ->			Union[str, Any]:
    if os.path.basename(__UpperCAmelCase   ) != name:
        raise ValueError(f"Should be a dataset name, not a path: {name}"   )
    return camelcase_to_snakecase(__UpperCAmelCase   )




def 	lowerCAmelCase_					(						__UpperCAmelCase:    Dict	,     __UpperCAmelCase:    Optional[int]   )   ->			Optional[Any]:
    if os.path.basename(__UpperCAmelCase   ) != name:
        raise ValueError(f"Should be a dataset name, not a path: {name}"   )
    if not re.match(_split_re	,     __UpperCAmelCase   ):
        raise ValueError(f"Split name should match '{_split_re}'' but got '{split}'."   )
    return f"{filename_prefix_for_name(__UpperCAmelCase   )}-{split}"




def 	lowerCAmelCase_					(						__UpperCAmelCase:    str	,     __UpperCAmelCase:    Dict	,     __UpperCAmelCase:    List[Any]	,     __UpperCAmelCase:    List[Any]=None   )   ->			Union[str, Any]:
    UpperCamelCase__							:       Optional[Any]      =   filename_prefix_for_split(__UpperCAmelCase	,     __UpperCAmelCase   )
    if filetype_suffix:
        prefix += f".{filetype_suffix}"
    UpperCamelCase__							:       Dict      =   os.path.join(__UpperCAmelCase	,     __UpperCAmelCase   )
    return f"{filepath}*"





def 	lowerCAmelCase_					(						__UpperCAmelCase:    List[str]	,     __UpperCAmelCase:    List[str]	,     __UpperCAmelCase:    int	,     __UpperCAmelCase:    str=None	,     __UpperCAmelCase:    Tuple=None   )   ->			Tuple:
    UpperCamelCase__							:       str      =   filename_prefix_for_split(__UpperCAmelCase	,     __UpperCAmelCase   )
    UpperCamelCase__							:       Tuple      =   os.path.join(__UpperCAmelCase	,     __UpperCAmelCase   )

    if shard_lengths:
        UpperCamelCase__							:       Optional[Any]      =   len(__UpperCAmelCase   )
        UpperCamelCase__							:       List[str]      =   [f"{prefix}-{shard_id:05d}-of-{num_shards:05d}" for shard_id in range(__UpperCAmelCase   )]
        if filetype_suffix:
            UpperCamelCase__							:       Union[str, Any]      =   [filename + f".{filetype_suffix}" for filename in filenames]
        return filenames
    else:
        UpperCamelCase__							:       Tuple      =   prefix
        if filetype_suffix:
            filename += f".{filetype_suffix}"
        return [filename]

