




"""simple docstring"""





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   =       """hf-internal-testing/tiny-random-bert"""
A   =       os.path.join(TRANSFORMERS_CACHE, """models--hf-internal-testing--tiny-random-bert""")
A   =       """9b8c223d42b2188cb49d29af482996f9d0f3e5a6"""



class      a__				(	unittest.TestCase   ):


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


						"""simple docstring"""




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

						# File is cached at the same place the second time.
						__UpperCAmelCase	: str		=					cached_file(UpperCamelCase_      ,  UpperCamelCase_)
						self.assertEqual(UpperCamelCase_      ,  UpperCamelCase_)

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


			def       a_     (      self      :    int):


						"""simple docstring"""




						with self.assertRaisesRegex(UpperCamelCase_      ,  "is not a valid model identifier"):
									__UpperCAmelCase	: Any		=					cached_file("tiny-random-bert"      ,  UpperCamelCase_)

						with self.assertRaisesRegex(UpperCamelCase_      ,  "is not a valid git identifier"):
									__UpperCAmelCase	: List[str]		=					cached_file(UpperCamelCase_      ,  UpperCamelCase_      ,  revision="aaaa")

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


			def       a_     (      self      :    Any):


						"""simple docstring"""




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

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

						__UpperCAmelCase	: Optional[int]		=					cached_file(UpperCamelCase_      ,  "conf"      ,  _raise_exceptions_for_missing_entries=UpperCamelCase_)
						self.assertIsNone(UpperCamelCase_)

						__UpperCAmelCase	: Any		=					cached_file(UpperCamelCase_      ,  "conf"      ,  local_files_only=UpperCamelCase_      ,  _raise_exceptions_for_missing_entries=UpperCamelCase_)
						self.assertIsNone(UpperCamelCase_)

						__UpperCAmelCase	: int		=					mock.Mock()
						__UpperCAmelCase	: Tuple		=					500
						__UpperCAmelCase	: Optional[int]		=					{}
						__UpperCAmelCase	: Union[str, Any]		=					HTTPError
						__UpperCAmelCase	: Optional[Any]		=					{}

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


			def       a_     (      self      :    List[Any]):


						"""simple docstring"""




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


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

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

						__UpperCAmelCase	: str		=					get_file_from_repo("bert-base-cased"      ,  UpperCamelCase_)
						# The name is the cached name which is not very easy to test, so instead we load the content.
						__UpperCAmelCase	: List[Any]		=					json.loads(open(UpperCamelCase_      ,  "r").read())
						self.assertEqual(config["hidden_size"]      ,  768)


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


						"""simple docstring"""




						with tempfile.TemporaryDirectory() as tmp_dir:
									__UpperCAmelCase	: int		=					Path(UpperCamelCase_) / "a.txt"
									filename.touch()
									self.assertEqual(get_file_from_repo(UpperCamelCase_      ,  "a.txt")      ,  str(UpperCamelCase_))

									self.assertIsNone(get_file_from_repo(UpperCamelCase_      ,  "b.txt"))








"""simple docstring"""





from typing import Optional

import pyspark

from .. import Features, NamedSplit
from ..download import DownloadMode
from ..packaged_modules.spark.spark import Spark
from .abc import AbstractDatasetReader



class      a__				(	__magic_name__   ):


			def __init__(      self      :    List[Any]      ,  UpperCamelCase_      :    pyspark.sql.DataFrame      ,  UpperCamelCase_      :    Optional[NamedSplit] = None      ,  UpperCamelCase_      :    Optional[Features] = None      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    str = None      ,  UpperCamelCase_      :    bool = False      ,  UpperCamelCase_      :    str = None      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    str = "arrow"      ,  **UpperCamelCase_      :    Tuple      ,  ):


						"""simple docstring"""




						super().__init__(
						    split=UpperCamelCase_      ,  features=UpperCamelCase_      ,  cache_dir=UpperCamelCase_      ,  keep_in_memory=UpperCamelCase_      ,  streaming=UpperCamelCase_      ,  **UpperCamelCase_      ,  )
						__UpperCAmelCase	: List[Any]		=					load_from_cache_file
						__UpperCAmelCase	: List[Any]		=					file_format
						__UpperCAmelCase	: str		=					Spark(
						    df=UpperCamelCase_      ,  features=UpperCamelCase_      ,  cache_dir=UpperCamelCase_      ,  working_dir=UpperCamelCase_      ,  **UpperCamelCase_      ,  )


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


						"""simple docstring"""




						if self.streaming:
									return self.builder.as_streaming_dataset(split=self.split)
						__UpperCAmelCase	: List[Any]		=					None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD
						self.builder.download_and_prepare(
						    download_mode=UpperCamelCase_      ,  file_format=self._file_format      ,  )
						return self.builder.as_dataset(split=self.split)


