






"""simple docstring"""
import time
from contextlib import contextmanager
from pathlib import Path

import pytest
import requests
from huggingface_hub.hf_api import HfApi, HfFolder


lowerCamelCase_       			=   '''__DUMMY_TRANSFORMERS_USER__'''
lowerCamelCase_       			=   '''Dummy User'''
lowerCamelCase_       			=   '''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'''

lowerCamelCase_       			=   '''https://hub-ci.huggingface.co'''
lowerCamelCase_       			=   CI_HUB_ENDPOINT + '''/datasets/{repo_id}/resolve/{revision}/{path}'''
lowerCamelCase_       			=   CI_HUB_ENDPOINT + '''/{repo_id}/resolve/{revision}/{filename}'''
lowerCamelCase_       			=   Path('''~/.huggingface/hub_ci_token''').expanduser()

@pytest.fixture
def        snake_case   (     A__		):
			monkeypatch.setattr(
			    "huggingface_hub.file_download.HUGGINGFACE_CO_URL_TEMPLATE"			,A__		)

@pytest.fixture
def        snake_case   (     A__		):
			monkeypatch.setattr("datasets.config.HF_ENDPOINT"			,A__		)
			monkeypatch.setattr("datasets.config.HUB_DATASETS_URL"			,A__		)

@pytest.fixture
def        snake_case   (     A__		):
			monkeypatch.setattr("huggingface_hub.hf_api.HfFolder.path_token"			,A__		)

@pytest.fixture
def        snake_case   (     A__			,A__		):
			HfFolder.save_token(A__		)
			yield
			HfFolder.delete_token()

@pytest.fixture(scope="session"		)
def        snake_case   (     ):
			return HfApi(endpoint=A__		)

@pytest.fixture(scope="session"		)
def        snake_case   (     A__		):
			UpperCAmelCase_  :				Union[str, Any]      =      HfFolder.get_token()
			HfFolder.save_token(A__		)
			yield CI_HUB_USER_TOKEN
			if previous_token is not None:
						HfFolder.save_token(A__		)

@pytest.fixture
def        snake_case   (     A__		):
			def _cleanup_repo(A__		):
						hf_api.delete_repo(A__			,token=A__			,repo_type="dataset"		)

			return _cleanup_repo

@pytest.fixture
def        snake_case   (     A__		):
			@contextmanager
			def _temporary_repo(A__		):
						try:
									yield repo_id
						finally:
									cleanup_repo(A__		)

			return _temporary_repo

@pytest.fixture(scope="session"		)
def        snake_case   (     A__			,A__			,A__		):
			UpperCAmelCase_  :				Optional[Any]      =      F"""repo_txt_data-{int(time.time() * 10e3		)}"""
			UpperCAmelCase_  :				Any      =      F"""{CI_HUB_USER}/{repo_name}"""
			hf_api.create_repo(A__			,token=A__			,repo_type="dataset"			,private=A__		)
			hf_api.upload_file(
			    token=A__			,path_or_fileobj=str(A__		)			,path_in_repo="data/text_data.txt"			,repo_id=A__			,repo_type="dataset"			,)
			yield repo_id
			try:
						hf_api.delete_repo(A__			,token=A__			,repo_type="dataset"		)
			except (requests.exceptions.HTTPError, ValueError):  # catch http error and token invalid error
						pass

@pytest.fixture()
def        snake_case   (     A__			,A__			,A__		):
			return hf_private_dataset_repo_txt_data_

@pytest.fixture(scope="session"		)
def        snake_case   (     A__			,A__			,A__		):
			UpperCAmelCase_  :				Optional[int]      =      F"""repo_zipped_txt_data-{int(time.time() * 10e3		)}"""
			UpperCAmelCase_  :				int      =      F"""{CI_HUB_USER}/{repo_name}"""
			hf_api.create_repo(A__			,token=A__			,repo_type="dataset"			,private=A__		)
			hf_api.upload_file(
			    token=A__			,path_or_fileobj=str(A__		)			,path_in_repo="data.zip"			,repo_id=A__			,repo_type="dataset"			,)
			yield repo_id
			try:
						hf_api.delete_repo(A__			,token=A__			,repo_type="dataset"		)
			except (requests.exceptions.HTTPError, ValueError):  # catch http error and token invalid error
						pass

@pytest.fixture()
def        snake_case   (     A__			,A__			,A__		):
			return hf_private_dataset_repo_zipped_txt_data_

@pytest.fixture(scope="session"		)
def        snake_case   (     A__			,A__			,A__		):
			UpperCAmelCase_  :				Optional[Any]      =      F"""repo_zipped_img_data-{int(time.time() * 10e3		)}"""
			UpperCAmelCase_  :				List[str]      =      F"""{CI_HUB_USER}/{repo_name}"""
			hf_api.create_repo(A__			,token=A__			,repo_type="dataset"			,private=A__		)
			hf_api.upload_file(
			    token=A__			,path_or_fileobj=str(A__		)			,path_in_repo="data.zip"			,repo_id=A__			,repo_type="dataset"			,)
			yield repo_id
			try:
						hf_api.delete_repo(A__			,token=A__			,repo_type="dataset"		)
			except (requests.exceptions.HTTPError, ValueError):  # catch http error and token invalid error
						pass




@pytest.fixture()
def        snake_case   (     A__			,A__			,A__		):
			return hf_private_dataset_repo_zipped_img_data_











"""simple docstring"""
import doctest
import logging
import os
import unittest
from pathlib import Path
from typing import List, Union

import transformers
from transformers.testing_utils import require_tf, require_torch, slow


lowerCamelCase_       			=   logging.getLogger()







@unittest.skip('''Temporarily disable the doc tests.'''  )
@require_torch
@require_tf
@slow
class     UpperCamelCase_ (unittest.TestCase  ):





	def   _SCREAMING_SNAKE_CASE   (   self      :				Union[str, Any]			,			lowerCAmelCase_      :				Path			,			lowerCAmelCase_      :				Union[str, None] = None			,			lowerCAmelCase_      :				Union[List[str], None] = None			,			lowerCAmelCase_      :				Union[str, List[str], None] = None			,			lowerCAmelCase_      :				bool = True			,			)		->    Tuple:
				UpperCAmelCase_  :				Optional[Any]      =      [file for file in os.listdir(lowerCAmelCase_		) if os.path.isfile(os.path.join(lowerCAmelCase_			,			lowerCAmelCase_		)		)]

				if identifier is not None:
							UpperCAmelCase_  :				Optional[int]      =      [file for file in files if identifier in file]

				if n_identifier is not None:
							if isinstance(lowerCAmelCase_			,			lowerCAmelCase_		):
										for n_ in n_identifier:
													UpperCAmelCase_  :				List[Any]      =      [file for file in files if n_ not in file]
							else:
										UpperCAmelCase_  :				Optional[Any]      =      [file for file in files if n_identifier not in file]

				UpperCAmelCase_  :				List[Any]      =      ignore_files or []
				ignore_files.append("__init__.py"		)
				UpperCAmelCase_  :				Optional[Any]      =      [file for file in files if file not in ignore_files]

				for file in files:
							# Open all files
							print("Testing"			,			lowerCAmelCase_		)

							if only_modules:
										UpperCAmelCase_  :				str      =      file.split("."		)[0]
										try:
													UpperCAmelCase_  :				Tuple      =      getattr(lowerCAmelCase_			,			lowerCAmelCase_		)
													UpperCAmelCase_  :				int      =      doctest.DocTestSuite(lowerCAmelCase_		)
													UpperCAmelCase_  :				str      =      unittest.TextTestRunner().run(lowerCAmelCase_		)
													self.assertIs(len(result.failures		)			,			0		)
										except AttributeError:
													logger.info(f"""{module_identifier} is not a module."""		)
							else:
										UpperCAmelCase_  :				List[Any]      =      doctest.testfile(str(".." / directory / file		)			,			optionflags=doctest.ELLIPSIS		)
										self.assertIs(result.failed			,			0		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				Optional[int]		)		->    List[Any]:
				UpperCAmelCase_  :				Optional[int]      =      Path("src/transformers"		)
				UpperCAmelCase_  :				List[Any]      =      "modeling"
				UpperCAmelCase_  :				Tuple      =      [
				    "modeling_ctrl.py",
				    "modeling_tf_ctrl.py",
				]
				self.analyze_directory(lowerCAmelCase_			,			identifier=lowerCAmelCase_			,			ignore_files=lowerCAmelCase_		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				Union[str, Any]		)		->    List[Any]:
				UpperCAmelCase_  :				List[str]      =      Path("src/transformers"		)
				UpperCAmelCase_  :				int      =      "tokenization"
				self.analyze_directory(lowerCAmelCase_			,			identifier=lowerCAmelCase_		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				Optional[Any]		)		->    Optional[Any]:
				UpperCAmelCase_  :				int      =      Path("src/transformers"		)
				UpperCAmelCase_  :				Any      =      "configuration"
				self.analyze_directory(lowerCAmelCase_			,			identifier=lowerCAmelCase_		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				Any		)		->    Any:
				UpperCAmelCase_  :				int      =      Path("src/transformers"		)
				UpperCAmelCase_  :				List[Any]      =      ["configuration", "modeling", "tokenization"]
				self.analyze_directory(lowerCAmelCase_			,			n_identifier=lowerCAmelCase_		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				List[str]		)		->    Dict:
				UpperCAmelCase_  :				List[str]      =      Path("docs/source"		)
				UpperCAmelCase_  :				int      =      ["favicon.ico"]
				self.analyze_directory(lowerCAmelCase_			,			ignore_files=lowerCAmelCase_			,			only_modules=lowerCAmelCase_		)



