


'''simple docstring'''





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												=						'''\
    Text data.
    Second line of data.'''

__lowerCamelCase												=						'''file'''


@pytest.fixture(scope='''session'''						)
def        a__     (					UpperCamelCase_						:      str						):
   UpperCAmelCase__	:Optional[Any]      =      tmp_path_factory.mktemp('''data'''						) / (FILE_PATH + '''.zstd''')
   UpperCAmelCase__	:List[Any]      =      bytes(UpperCamelCase_,   '''utf-8'''						)
   with zstd.open(UpperCamelCase_,   '''wb'''						) as f:
      f.write(UpperCamelCase_						)
   return path


@pytest.fixture
def        a__     (					UpperCamelCase_						:      Tuple						):
   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        a__     (					UpperCamelCase_						:      Optional[int],   UpperCamelCase_						:      Any,   UpperCamelCase_						:      Union[str, Any],   UpperCamelCase_						:      Tuple,   UpperCamelCase_						:      str,   UpperCamelCase_						:      List[Any]						):
   UpperCAmelCase__	:str      =      {'''gzip''': gz_file, '''xz''': xz_file, '''zstd''': zstd_path}
   UpperCAmelCase__	:Optional[int]      =      input_paths[compression_format]
   UpperCAmelCase__	:Any      =      tmp_path / '''cache'''
   UpperCAmelCase__	:Any      =      DownloadConfig(cache_dir=UpperCamelCase_,   extract_compressed_file=UpperCamelCase_						)
   UpperCAmelCase__	:Any      =      cached_path(UpperCamelCase_,   download_config=UpperCamelCase_						)
   with open(UpperCamelCase_						) as f:
      UpperCAmelCase__	:Optional[int]      =      f.read()
   with open(UpperCamelCase_						) as f:
      UpperCAmelCase__	:Union[str, Any]      =      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        a__     (					UpperCamelCase_						:      List[Any],   UpperCamelCase_						:      str,   UpperCamelCase_						:      Union[str, Any],   UpperCamelCase_						:      int,   UpperCamelCase_						:      Any						):
   UpperCAmelCase__	:Union[str, Any]      =      '''custom_cache'''
   UpperCAmelCase__	:List[Any]      =      '''custom_extracted_dir'''
   UpperCAmelCase__	:Tuple      =      tmp_path / '''custom_extracted_path'''
   if default_extracted:
      UpperCAmelCase__	:List[str]      =      ('''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_						)						)
      UpperCAmelCase__	:Optional[Any]      =      custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

   UpperCAmelCase__	:List[Any]      =      xz_file
   UpperCAmelCase__	:Optional[int]      =      (
       DownloadConfig(extract_compressed_file=UpperCamelCase_						)
       if default_cache_dir
       else DownloadConfig(cache_dir=tmp_path / custom_cache_dir,   extract_compressed_file=UpperCamelCase_						)
   )
   UpperCAmelCase__	:Union[str, Any]      =      cached_path(UpperCamelCase_,   download_config=UpperCamelCase_						)
   assert Path(UpperCamelCase_						).parent.parts[-2:] == expected


def        a__     (					UpperCamelCase_						:      Any						):
   # absolute path
   UpperCAmelCase__	:Tuple      =      str(Path(UpperCamelCase_						).resolve()						)
   assert cached_path(UpperCamelCase_						) == text_file
   # relative path
   UpperCAmelCase__	:Union[str, Any]      =      str(Path(UpperCamelCase_						).resolve().relative_to(Path(os.getcwd()						)						)						)
   assert cached_path(UpperCamelCase_						) == text_file


def        a__     (					UpperCamelCase_						:      Union[str, Any]						):
   # absolute path
   UpperCAmelCase__	:Dict      =      str(tmp_path.resolve() / '''__missing_file__.txt'''						)
   with pytest.raises(UpperCamelCase_						):
      cached_path(UpperCamelCase_						)
   # relative path
   UpperCAmelCase__	:Dict      =      '''./__missing_file__.txt'''
   with pytest.raises(UpperCamelCase_						):
      cached_path(UpperCamelCase_						)


def        a__     (					UpperCamelCase_						:      Dict						):
   UpperCAmelCase__	:Union[str, Any]      =      get_from_cache(F'''tmp://{tmpfs_file}'''						)
   with open(UpperCamelCase_						) as f:
      UpperCAmelCase__	:Union[str, Any]      =      f.read()
   assert output_file_content == FILE_CONTENT


@patch('''datasets.config.HF_DATASETS_OFFLINE''',   UpperCamelCase_						)
def        a__     (					):
   with pytest.raises(UpperCamelCase_						):
      cached_path('''https://huggingface.co'''						)


@patch('''datasets.config.HF_DATASETS_OFFLINE''',   UpperCamelCase_						)
def        a__     (					UpperCamelCase_						:      Optional[int]						):
   UpperCAmelCase__	:str      =      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        a__     (					UpperCamelCase_						:      Optional[int]						):
   UpperCAmelCase__	:Union[str, Any]      =      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        a__     (					UpperCamelCase_						:      Optional[Any]						):
   UpperCAmelCase__	:Dict      =      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'''						)







'''simple docstring'''





from ..utils import DummyObject, requires_backends




class 				UpperCAmelCase			(			metaclass=_snake_case						):
 UpperCAmelCase				=					["speech"]


 def __init__(      self							:	List[Any]			,       *__lowerCamelCase							:	List[Any]			,       **__lowerCamelCase							:	List[str] ):
    requires_backends(self			,       ['''speech'''] )




class 				UpperCAmelCase			(			metaclass=_snake_case						):
 UpperCAmelCase				=					["speech"]


 def __init__(      self							:	int			,       *__lowerCamelCase							:	List[Any]			,       **__lowerCamelCase							:	List[str] ):
    requires_backends(self			,       ['''speech'''] )



