






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__			:    str         =				'hf-internal-testing/tiny-random-bert'
A__			:    Optional[int]         =				os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert')
A__			:    List[str]         =				'9b8c223d42b2188cb49d29af482996f9d0f3e5a6'
class    _UpperCAmelCase      ( unittest.TestCase			):

				"""simple docstring"""




				def        lowercase__     (  self		:					List[Any]						):

								'''simple docstring'''
								lowercase__     				=    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:
												lowercase__     				=    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.
								lowercase__     				=    cached_file(lowerCamelCase,						lowerCamelCase						)
								self.assertEqual(lowerCamelCase,						lowerCamelCase						)

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




				def        lowercase__     (  self		:					Optional[Any]						):

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

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

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




				def        lowercase__     (  self		:					Any						):

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

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

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

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

								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=lowerCamelCase						) as mock_head:
												lowercase__     				=    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        lowercase__     (  self		:					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        lowercase__     (  self		:					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(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'''						)

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




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

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







from __future__ import annotations

from collections import deque
from collections.abc import Iterator
from dataclasses import dataclass
@dataclass
class    _UpperCAmelCase      :

				"""simple docstring"""




				lowercase__       							= 42
				lowercase__       							= 42



class    _UpperCAmelCase      :

				"""simple docstring"""




				def __init__(  self		:					str,						lowerCamelCase		:					int						):

								'''simple docstring'''
								lowercase__     				=    [[] for _ in range(lowerCamelCase						)]
								lowercase__     				=    size




				def __getitem__(  self		:					Optional[Any],						lowerCamelCase		:					int						):

								'''simple docstring'''
								return iter(self._graph[vertex]						)




				@property
				def        lowercase__     (  self		:					str						):

								'''simple docstring'''
								return self._size




				def        lowercase__     (  self		:					Union[str, Any],						lowerCamelCase		:					int,						lowerCamelCase		:					int,						lowerCamelCase		:					int						):

								'''simple docstring'''
								if weight not in (0, 1):
												raise ValueError('''Edge weight must be either 0 or 1.'''						)

								if to_vertex < 0 or to_vertex >= self.size:
												raise ValueError('''Vertex indexes must be in [0; size).'''						)

								self._graph[from_vertex].append(Edge(lowerCamelCase,						lowerCamelCase						)						)




				def        lowercase__     (  self		:					Optional[int],						lowerCamelCase		:					int,						lowerCamelCase		:					int						):

								'''simple docstring'''
								lowercase__     				=    deque([start_vertex]						)
								lowercase__     				=    [None] * self.size
								lowercase__     				=    0

								while queue:
												lowercase__     				=    queue.popleft()
												lowercase__     				=    distances[current_vertex]
												if current_distance is None:
																continue

												for edge in self[current_vertex]:
																lowercase__     				=    current_distance + edge.weight
																lowercase__     				=    distances[edge.destination_vertex]
																if (
																    isinstance(lowerCamelCase,						lowerCamelCase						)
																    and new_distance >= dest_vertex_distance
																):
																				continue
																lowercase__     				=    new_distance
																if edge.weight == 0:
																				queue.appendleft(edge.destination_vertex						)
																else:
																				queue.append(edge.destination_vertex						)

								if distances[finish_vertex] is None:
												raise ValueError('''No path from start_vertex to finish_vertex.'''						)

								return distances[finish_vertex]


if __name__ == "__main__":
							import doctest

							doctest.testmod()




