




'''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,
)


UpperCamelCase_		:     List[Any]          =		'''\
    Text data.
    Second line of data.'''

UpperCamelCase_		:     Union[str, Any]          =		'''file'''





@pytest.fixture(scope="session"       )
def       __a      (  _UpperCamelCase:       Any       )		->					Optional[Any]:

     """simple docstring"""




     _snake_case				= tmp_path_factory.mktemp("data"       ) / (FILE_PATH + ".zstd")
     _snake_case				= bytes(_UpperCamelCase   ,		"utf-8"       )
     with zstd.open(_UpperCamelCase   ,		"wb"       ) as f:
          f.write(_UpperCamelCase       )
     return path





@pytest.fixture
def       __a      (  _UpperCamelCase:       Union[str, Any]       )		->					Optional[Any]:

     """simple docstring"""




     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[Any]   ,		_UpperCamelCase:       Any   ,		_UpperCamelCase:       str   ,		_UpperCamelCase:       Optional[int]   ,		_UpperCamelCase:       List[str]   ,		_UpperCamelCase:       List[str]       )		->					Optional[Any]:

     """simple docstring"""




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

     """simple docstring"""




     _snake_case				= "custom_cache"
     _snake_case				= "custom_extracted_dir"
     _snake_case				= tmp_path / "custom_extracted_path"
     if default_extracted:
          _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       )       )
          _snake_case				= custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

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





def       __a      (  _UpperCamelCase:       Union[str, Any]       )		->					Dict:

     """simple docstring"""




     _snake_case				= str(Path(_UpperCamelCase       ).resolve()       )
     assert cached_path(_UpperCamelCase       ) == text_file
     # relative path
     _snake_case				= str(Path(_UpperCamelCase       ).resolve().relative_to(Path(os.getcwd()       )       )       )
     assert cached_path(_UpperCamelCase       ) == text_file





def       __a      (  _UpperCamelCase:       List[str]       )		->					Optional[int]:

     """simple docstring"""




     _snake_case				= str(tmp_path.resolve() / "__missing_file__.txt"       )
     with pytest.raises(_UpperCamelCase       ):
          cached_path(_UpperCamelCase       )
     # relative path
     _snake_case				= "./__missing_file__.txt"
     with pytest.raises(_UpperCamelCase       ):
          cached_path(_UpperCamelCase       )





def       __a      (  _UpperCamelCase:       List[str]       )		->					List[Any]:

     """simple docstring"""




     _snake_case				= get_from_cache(F"""tmp://{tmpfs_file}"""       )
     with open(_UpperCamelCase       ) as f:
          _snake_case				= f.read()
     assert output_file_content == FILE_CONTENT





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

     """simple docstring"""




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





@patch("datasets.config.HF_DATASETS_OFFLINE"   ,		_UpperCamelCase       )
def       __a      (  _UpperCamelCase:       Any       )		->					Union[str, Any]:

     """simple docstring"""




     _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       __a      (  _UpperCamelCase:       Dict       )		->					Tuple:

     """simple docstring"""




     _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       __a      (  _UpperCamelCase:       Dict       )		->					Tuple:

     """simple docstring"""




     _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"       )








'''simple docstring'''





from typing import Optional
from urllib.parse import quote

import huggingface_hub as hfh
from packaging import version





def       __a      (  _UpperCamelCase:       str   ,		_UpperCamelCase:       str   ,		_UpperCamelCase:       Optional[str] = None       )		->					str:

     """simple docstring"""




     if version.parse(hfh.__version__       ).release < version.parse("0.11.0"       ).release:
          # old versions of hfh don't url-encode the file path
          _snake_case				= quote(_UpperCamelCase       )
     return hfh.hf_hub_url(_UpperCamelCase   ,		_UpperCamelCase   ,		repo_type="dataset"   ,		revision=_UpperCamelCase       )


