
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__    :		Dict          =   """\
    Text data.
    Second line of data."""

lowerCamelCase__    :		int          =   """file"""

@pytest.fixture(scope="""session"""   )
def 		UpperCamelCase	(      lowercase_   )		->	Dict:


  '''simple docstring'''






  lowercase__  :   Union[str, Any]    		=      tmp_path_factory.mktemp("""data"""   ) / (FILE_PATH + """.zstd""")
  lowercase__  :   int    		=      bytes(lowercase_ , """utf-8"""   )
  with zstd.open(lowercase_ , """wb"""   ) as f:
    f.write(lowercase_   )
  return path

@pytest.fixture
def 		UpperCamelCase	(      lowercase_   )		->	Dict:


  '''simple docstring'''






  with open(os.path.join(tmpfs.local_root_dir , lowercase_   ) , """w"""   ) as f:
    f.write(lowercase_   )
  return FILE_PATH

@pytest.mark.parametrize("""compression_format""" , ["""gzip""", """xz""", """zstd"""]   )
def 		UpperCamelCase	(      lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_   )		->	str:


  '''simple docstring'''






  lowercase__  :   List[Any]    		=      {"""gzip""": gz_file, """xz""": xz_file, """zstd""": zstd_path}
  lowercase__  :   int    		=      input_paths[compression_format]
  lowercase__  :   Optional[Any]    		=      tmp_path / """cache"""
  lowercase__  :   Tuple    		=      DownloadConfig(cache_dir=lowercase_ , extract_compressed_file=lowercase_   )
  lowercase__  :   str    		=      cached_path(lowercase_ , download_config=lowercase_   )
  with open(lowercase_   ) as f:
    lowercase__  :   Dict    		=      f.read()
  with open(lowercase_   ) as f:
    lowercase__  :   List[str]    		=      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 		UpperCamelCase	(      lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_   )		->	Tuple:


  '''simple docstring'''






  lowercase__  :   Union[str, Any]    		=      """custom_cache"""
  lowercase__  :   Union[str, Any]    		=      """custom_extracted_dir"""
  lowercase__  :   List[Any]    		=      tmp_path / """custom_extracted_path"""
  if default_extracted:
    lowercase__  :   Dict    		=      ("""downloads""" if default_cache_dir else custom_cache_dir, """extracted""")
  else:
    monkeypatch.setattr("""datasets.config.EXTRACTED_DATASETS_DIR""" , lowercase_   )
    monkeypatch.setattr("""datasets.config.EXTRACTED_DATASETS_PATH""" , str(lowercase_   )   )
    lowercase__  :   Tuple    		=      custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

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

def 		UpperCamelCase	(      lowercase_   )		->	Tuple:


  '''simple docstring'''






  lowercase__  :   Optional[int]    		=      str(Path(lowercase_   ).resolve()   )
  assert cached_path(lowercase_   ) == text_file
  # relative path
  lowercase__  :   Any    		=      str(Path(lowercase_   ).resolve().relative_to(Path(os.getcwd()   )   )   )
  assert cached_path(lowercase_   ) == text_file

def 		UpperCamelCase	(      lowercase_   )		->	Union[str, Any]:


  '''simple docstring'''






  lowercase__  :   Optional[int]    		=      str(tmp_path.resolve() / """__missing_file__.txt"""   )
  with pytest.raises(lowercase_   ):
    cached_path(lowercase_   )
  # relative path
  lowercase__  :   Any    		=      """./__missing_file__.txt"""
  with pytest.raises(lowercase_   ):
    cached_path(lowercase_   )

def 		UpperCamelCase	(      lowercase_   )		->	Union[str, Any]:


  '''simple docstring'''






  lowercase__  :   Union[str, Any]    		=      get_from_cache(F'tmp://{tmpfs_file}'   )
  with open(lowercase_   ) as f:
    lowercase__  :   Tuple    		=      f.read()
  assert output_file_content == FILE_CONTENT

@patch("""datasets.config.HF_DATASETS_OFFLINE""" , lowercase_   )
def 		UpperCamelCase	(      )		->	Tuple:


  '''simple docstring'''






  with pytest.raises(lowercase_   ):
    cached_path("""https://huggingface.co"""   )

@patch("""datasets.config.HF_DATASETS_OFFLINE""" , lowercase_   )
def 		UpperCamelCase	(      lowercase_   )		->	List[str]:


  '''simple docstring'''






  lowercase__  :   List[str]    		=      tmp_path_factory.mktemp("""data"""   ) / """file.html"""
  with pytest.raises(lowercase_   ):
    http_get("""https://huggingface.co""" , temp_file=lowercase_   )
  with pytest.raises(lowercase_   ):
    http_head("""https://huggingface.co"""   )

@patch("""datasets.config.HF_DATASETS_OFFLINE""" , lowercase_   )
def 		UpperCamelCase	(      lowercase_   )		->	Any:


  '''simple docstring'''






  lowercase__  :   Any    		=      tmp_path_factory.mktemp("""data"""   ) / """file.html"""
  with pytest.raises(lowercase_   ):
    ftp_get("""ftp://huggingface.co""" , temp_file=lowercase_   )
  with pytest.raises(lowercase_   ):
    ftp_head("""ftp://huggingface.co"""   )



@patch("""datasets.config.HF_DATASETS_OFFLINE""" , lowercase_   )
def 		UpperCamelCase	(      lowercase_   )		->	Any:


  '''simple docstring'''






  lowercase__  :   Any    		=      tmp_path_factory.mktemp("""data"""   ) / """file.html"""
  with pytest.raises(lowercase_   ):
    fsspec_get("""s3://huggingface.co""" , temp_file=lowercase_   )
  with pytest.raises(lowercase_   ):
    fsspec_head("""s3://huggingface.co"""   )








from abc import ABC, abstractmethod
from argparse import ArgumentParser



class       _snake_case   (	UpperCAmelCase_							):


 @staticmethod
 @abstractmethod
 def      lowercase__     (					SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   raise NotImplementedError()



 @abstractmethod
 def      lowercase__     (					self):


   '''simple docstring'''




   raise NotImplementedError()






