





import functools
import logging
import os
import sys
import threading
from logging import (
    CRITICAL,  # NOQA
    DEBUG,  # NOQA
    ERROR,  # NOQA
    FATAL,  # NOQA
    INFO,  # NOQA
    NOTSET,  # NOQA
    WARN,  # NOQA
    WARNING,  # NOQA
)
from typing import Optional

import huggingface_hub.utils as hf_hub_utils
from tqdm import auto as tqdm_lib


_lowerCamelCase   				=			threading.Lock()
_lowerCamelCase   				=			None

_lowerCamelCase   				=			{
    'debug': logging.DEBUG,
    'info': logging.INFO,
    'warning': logging.WARNING,
    'error': logging.ERROR,
    'critical': logging.CRITICAL,
}

_lowerCamelCase   				=			logging.WARNING

_lowerCamelCase   				=			True






def 				SCREAMING_SNAKE_CASE__							(       ):
		SCREAMING_SNAKE_CASE__			=      os.getenv("""TRANSFORMERS_VERBOSITY"""					,   UpperCamelCase__    )
		if env_level_str:
				if env_level_str in log_levels:
						return log_levels[env_level_str]
				else:
						logging.getLogger().warning(
						    f'''Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, '''
						    f'''has to be one of: { ', '.join(log_levels.keys()    ) }'''    )
		return _default_log_level






def 				SCREAMING_SNAKE_CASE__							(       ):
		return __name__.split("""."""    )[0]






def 				SCREAMING_SNAKE_CASE__							(       ):
		return logging.getLogger(_get_library_name()    )






def 				SCREAMING_SNAKE_CASE__							(       ):
		global _default_handler

		with _lock:
				if _default_handler:
						# This library has already configured the library root logger.
						return
				SCREAMING_SNAKE_CASE__			=      logging.StreamHandler()  # Set sys.stderr as stream.
				SCREAMING_SNAKE_CASE__			=      sys.stderr.flush

				# Apply our default configuration to the library root logger.
				SCREAMING_SNAKE_CASE__			=      _get_library_root_logger()
				library_root_logger.addHandler(_default_handler    )
				library_root_logger.setLevel(_get_default_logging_level()    )
				SCREAMING_SNAKE_CASE__			=      False






def 				SCREAMING_SNAKE_CASE__							(       ):
		global _default_handler

		with _lock:
				if not _default_handler:
						return

				SCREAMING_SNAKE_CASE__			=      _get_library_root_logger()
				library_root_logger.removeHandler(_default_handler    )
				library_root_logger.setLevel(logging.NOTSET    )
				SCREAMING_SNAKE_CASE__			=      None






def 				SCREAMING_SNAKE_CASE__							(       ):
		return log_levels






def 				SCREAMING_SNAKE_CASE__							(       UpperCamelCase__:							Optional[str] = None    ):

		if name is None:
				SCREAMING_SNAKE_CASE__			=      _get_library_name()

		_configure_library_root_logger()
		return logging.getLogger(UpperCamelCase__    )






def 				SCREAMING_SNAKE_CASE__							(       ):
		_configure_library_root_logger()
		return _get_library_root_logger().getEffectiveLevel()






def 				SCREAMING_SNAKE_CASE__							(       UpperCamelCase__:							int    ):
		_configure_library_root_logger()
		_get_library_root_logger().setLevel(UpperCamelCase__    )






def 				SCREAMING_SNAKE_CASE__							(       ):
		return set_verbosity(UpperCamelCase__    )






def 				SCREAMING_SNAKE_CASE__							(       ):
		return set_verbosity(UpperCamelCase__    )






def 				SCREAMING_SNAKE_CASE__							(       ):
		return set_verbosity(UpperCamelCase__    )






def 				SCREAMING_SNAKE_CASE__							(       ):
		return set_verbosity(UpperCamelCase__    )






def 				SCREAMING_SNAKE_CASE__							(       ):
		_configure_library_root_logger()

		assert _default_handler is not None
		_get_library_root_logger().removeHandler(_default_handler    )






def 				SCREAMING_SNAKE_CASE__							(       ):
		_configure_library_root_logger()

		assert _default_handler is not None
		_get_library_root_logger().addHandler(_default_handler    )






def 				SCREAMING_SNAKE_CASE__							(       UpperCamelCase__:							logging.Handler    ):
		_configure_library_root_logger()

		assert handler is not None
		_get_library_root_logger().addHandler(UpperCamelCase__    )






def 				SCREAMING_SNAKE_CASE__							(       UpperCamelCase__:							logging.Handler    ):
		_configure_library_root_logger()

		assert handler is not None and handler not in _get_library_root_logger().handlers
		_get_library_root_logger().removeHandler(UpperCamelCase__    )






def 				SCREAMING_SNAKE_CASE__							(       ):
		_configure_library_root_logger()
		SCREAMING_SNAKE_CASE__			=      False






def 				SCREAMING_SNAKE_CASE__							(       ):
		_configure_library_root_logger()
		SCREAMING_SNAKE_CASE__			=      True






def 				SCREAMING_SNAKE_CASE__							(       ):
		SCREAMING_SNAKE_CASE__			=      _get_library_root_logger().handlers

		for handler in handlers:
				SCREAMING_SNAKE_CASE__			=      logging.Formatter("""[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s"""    )
				handler.setFormatter(UpperCamelCase__    )






def 				SCREAMING_SNAKE_CASE__							(       ):
		SCREAMING_SNAKE_CASE__			=      _get_library_root_logger().handlers

		for handler in handlers:
				handler.setFormatter(UpperCamelCase__    )






def 				SCREAMING_SNAKE_CASE__							(       self:							str					,   *UpperCamelCase__:							Any					,   **UpperCamelCase__:							Dict    ):
		SCREAMING_SNAKE_CASE__			=      os.getenv("""TRANSFORMERS_NO_ADVISORY_WARNINGS"""					,   UpperCamelCase__    )
		if no_advisory_warnings:
				return
		self.warning(*UpperCamelCase__					,   **UpperCamelCase__    )


_lowerCamelCase   				=			warning_advice






@functools.lru_cache(UpperCamelCase__    )
def 				SCREAMING_SNAKE_CASE__							(       self:							Dict					,   *UpperCamelCase__:							str					,   **UpperCamelCase__:							Optional[int]    ):
		self.warning(*UpperCamelCase__					,   **UpperCamelCase__    )


_lowerCamelCase   				=			warning_once




class   UpperCamelCase_       :


							def __init__(							self    :List[str]							,				*__A    :Optional[Any]							,				**__A    :Tuple		)   ->   str:  # pylint: disable=unused-argument



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      args[0] if args else None


							def __iter__(							self    :Any		)   ->   List[str]:



									"""simple docstring"""



									return iter(self._iterator		)


							def __getattr__(							self    :Union[str, Any]							,				__A    :Union[str, Any]		)   ->   List[Any]:



									"""simple docstring"""



									def empty_fn(*__A    :Optional[Any]							,				**__A    :Optional[Any]		):  # pylint: disable=unused-argument
											return

									return empty_fn


							def __enter__(							self    :Any		)   ->   str:



									"""simple docstring"""



									return self


							def __exit__(							self    :List[str]							,				__A    :Optional[int]							,				__A    :List[str]							,				__A    :str		)   ->   Any:



									"""simple docstring"""



									return




class   UpperCamelCase_       :


							def __call__(							self    :List[Any]							,				*__A    :Tuple							,				**__A    :Any		)   ->   List[Any]:



									"""simple docstring"""



									if _tqdm_active:
											return tqdm_lib.tqdm(*__A							,				**__A		)
									else:
											return EmptyTqdm(*__A							,				**__A		)


							def      _snake_case  (							self    :Tuple							,				*__A    :List[Any]							,				**__A    :str		)   ->   str:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      None
									if _tqdm_active:
											return tqdm_lib.tqdm.set_lock(*__A							,				**__A		)


							def      _snake_case  (							self    :Tuple		)   ->   List[Any]:



									"""simple docstring"""



									if _tqdm_active:
											return tqdm_lib.tqdm.get_lock()



_lowerCamelCase   				=			_tqdm_cls()






def 				SCREAMING_SNAKE_CASE__							(       ):
		global _tqdm_active
		return bool(_tqdm_active    )






def 				SCREAMING_SNAKE_CASE__							(       ):
		global _tqdm_active
		SCREAMING_SNAKE_CASE__			=      True
		hf_hub_utils.enable_progress_bars()






def 				SCREAMING_SNAKE_CASE__							(       ):
		global _tqdm_active
		SCREAMING_SNAKE_CASE__			=      False
		hf_hub_utils.disable_progress_bars()






import os
from pathlib import Path
from unittest.mock import patch

import pytest
import zstandard as zstd

from datasets.download.download_config import DownloadConfig
from datasets.utils.file_utils import (
    OfflineModeIsEnabled,
    cached_path,
    fsspec_get,
    fsspec_head,
    ftp_get,
    ftp_head,
    get_from_cache,
    http_get,
    http_head,
)


_lowerCamelCase   				=			'\\n    Text data.\n    Second line of data.'

_lowerCamelCase   				=			'file'






@pytest.fixture(scope="""session"""    )
def 				SCREAMING_SNAKE_CASE__							(       UpperCamelCase__:							Any    ):
		SCREAMING_SNAKE_CASE__			=      tmp_path_factory.mktemp("""data"""    ) / (FILE_PATH + """.zstd""")
		SCREAMING_SNAKE_CASE__			=      bytes(UpperCamelCase__					,   """utf-8"""    )
		with zstd.open(UpperCamelCase__					,   """wb"""    ) as f:
				f.write(UpperCamelCase__    )
		return path






@pytest.fixture
def 				SCREAMING_SNAKE_CASE__							(       UpperCamelCase__:							List[str]    ):
		with open(os.path.join(tmpfs.local_root_dir					,   UpperCamelCase__    )					,   """w"""    ) as f:
				f.write(UpperCamelCase__    )
		return FILE_PATH






@pytest.mark.parametrize("""compression_format"""					,   ["""gzip""", """xz""", """zstd"""]    )
def 				SCREAMING_SNAKE_CASE__							(       UpperCamelCase__:							int					,   UpperCamelCase__:							Dict					,   UpperCamelCase__:							int					,   UpperCamelCase__:							str					,   UpperCamelCase__:							Optional[int]					,   UpperCamelCase__:							Optional[Any]    ):
		SCREAMING_SNAKE_CASE__			=      {"""gzip""": gz_file, """xz""": xz_file, """zstd""": zstd_path}
		SCREAMING_SNAKE_CASE__			=      input_paths[compression_format]
		SCREAMING_SNAKE_CASE__			=      tmp_path / """cache"""
		SCREAMING_SNAKE_CASE__			=      DownloadConfig(cache_dir=UpperCamelCase__					,   extract_compressed_file=UpperCamelCase__    )
		SCREAMING_SNAKE_CASE__			=      cached_path(UpperCamelCase__					,   download_config=UpperCamelCase__    )
		with open(UpperCamelCase__    ) as f:
				SCREAMING_SNAKE_CASE__			=      f.read()
		with open(UpperCamelCase__    ) as f:
				SCREAMING_SNAKE_CASE__			=      f.read()
		assert extracted_file_content == expected_file_content






@pytest.mark.parametrize("""default_extracted"""					,   [True, False]    )
@pytest.mark.parametrize("""default_cache_dir"""					,   [True, False]    )
def 				SCREAMING_SNAKE_CASE__							(       UpperCamelCase__:							Tuple					,   UpperCamelCase__:							List[str]					,   UpperCamelCase__:							Optional[int]					,   UpperCamelCase__:							Any					,   UpperCamelCase__:							Union[str, Any]    ):
		SCREAMING_SNAKE_CASE__			=      """custom_cache"""
		SCREAMING_SNAKE_CASE__			=      """custom_extracted_dir"""
		SCREAMING_SNAKE_CASE__			=      tmp_path / """custom_extracted_path"""
		if default_extracted:
				SCREAMING_SNAKE_CASE__			=      ("""downloads""" if default_cache_dir else custom_cache_dir, """extracted""")
		else:
				monkeypatch.setattr("""datasets.config.EXTRACTED_DATASETS_DIR"""					,   UpperCamelCase__    )
				monkeypatch.setattr("""datasets.config.EXTRACTED_DATASETS_PATH"""					,   str(UpperCamelCase__    )    )
				SCREAMING_SNAKE_CASE__			=      custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

		SCREAMING_SNAKE_CASE__			=      xz_file
		SCREAMING_SNAKE_CASE__			=      (
		    DownloadConfig(extract_compressed_file=UpperCamelCase__    )
		    if default_cache_dir
		    else DownloadConfig(cache_dir=tmp_path / custom_cache_dir					,   extract_compressed_file=UpperCamelCase__    )
		)
		SCREAMING_SNAKE_CASE__			=      cached_path(UpperCamelCase__					,   download_config=UpperCamelCase__    )
		assert Path(UpperCamelCase__    ).parent.parts[-2:] == expected






def 				SCREAMING_SNAKE_CASE__							(       UpperCamelCase__:							Optional[int]    ):
		# absolute path
		SCREAMING_SNAKE_CASE__			=      str(Path(UpperCamelCase__    ).resolve()    )
		assert cached_path(UpperCamelCase__    ) == text_file
		# relative path
		SCREAMING_SNAKE_CASE__			=      str(Path(UpperCamelCase__    ).resolve().relative_to(Path(os.getcwd()    )    )    )
		assert cached_path(UpperCamelCase__    ) == text_file






def 				SCREAMING_SNAKE_CASE__							(       UpperCamelCase__:							List[str]    ):
		# absolute path
		SCREAMING_SNAKE_CASE__			=      str(tmp_path.resolve() / """__missing_file__.txt"""    )
		with pytest.raises(UpperCamelCase__    ):
				cached_path(UpperCamelCase__    )
		# relative path
		SCREAMING_SNAKE_CASE__			=      """./__missing_file__.txt"""
		with pytest.raises(UpperCamelCase__    ):
				cached_path(UpperCamelCase__    )






def 				SCREAMING_SNAKE_CASE__							(       UpperCamelCase__:							List[str]    ):
		SCREAMING_SNAKE_CASE__			=      get_from_cache(f'''tmp://{tmpfs_file}'''    )
		with open(UpperCamelCase__    ) as f:
				SCREAMING_SNAKE_CASE__			=      f.read()
		assert output_file_content == FILE_CONTENT






@patch("""datasets.config.HF_DATASETS_OFFLINE"""					,   UpperCamelCase__    )
def 				SCREAMING_SNAKE_CASE__							(       ):
		with pytest.raises(UpperCamelCase__    ):
				cached_path("""https://huggingface.co"""    )






@patch("""datasets.config.HF_DATASETS_OFFLINE"""					,   UpperCamelCase__    )
def 				SCREAMING_SNAKE_CASE__							(       UpperCamelCase__:							Optional[Any]    ):
		SCREAMING_SNAKE_CASE__			=      tmp_path_factory.mktemp("""data"""    ) / """file.html"""
		with pytest.raises(UpperCamelCase__    ):
				http_get("""https://huggingface.co"""					,   temp_file=UpperCamelCase__    )
		with pytest.raises(UpperCamelCase__    ):
				http_head("""https://huggingface.co"""    )






@patch("""datasets.config.HF_DATASETS_OFFLINE"""					,   UpperCamelCase__    )
def 				SCREAMING_SNAKE_CASE__							(       UpperCamelCase__:							List[Any]    ):
		SCREAMING_SNAKE_CASE__			=      tmp_path_factory.mktemp("""data"""    ) / """file.html"""
		with pytest.raises(UpperCamelCase__    ):
				ftp_get("""ftp://huggingface.co"""					,   temp_file=UpperCamelCase__    )
		with pytest.raises(UpperCamelCase__    ):
				ftp_head("""ftp://huggingface.co"""    )






@patch("""datasets.config.HF_DATASETS_OFFLINE"""					,   UpperCamelCase__    )
def 				SCREAMING_SNAKE_CASE__							(       UpperCamelCase__:							int    ):
		SCREAMING_SNAKE_CASE__			=      tmp_path_factory.mktemp("""data"""    ) / """file.html"""
		with pytest.raises(UpperCamelCase__    ):
				fsspec_get("""s3://huggingface.co"""					,   temp_file=UpperCamelCase__    )
		with pytest.raises(UpperCamelCase__    ):
				fsspec_head("""s3://huggingface.co"""    )