





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


_SCREAMING_SNAKE_CASE									=				"\\n    Text data.\n    Second line of data."

_SCREAMING_SNAKE_CASE									=				"file"


@pytest.fixture(scope="session"     )
def 						__a(SCREAMING_SNAKE_CASE_	:       Union[str, Any]     ):



    '''simple docstring'''





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


@pytest.fixture
def 						__a(SCREAMING_SNAKE_CASE_	:       Dict     ):



    '''simple docstring'''





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


@pytest.mark.parametrize("compression_format"		,							["gzip", "xz", "zstd"]     )
def 						__a(SCREAMING_SNAKE_CASE_	:       Dict		,							SCREAMING_SNAKE_CASE_	:       List[str]		,							SCREAMING_SNAKE_CASE_	:       Any		,							SCREAMING_SNAKE_CASE_	:       str		,							SCREAMING_SNAKE_CASE_	:       List[str]		,							SCREAMING_SNAKE_CASE_	:       Dict     ):



    '''simple docstring'''





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



    '''simple docstring'''





    _lowerCAmelCase						      =   "custom_cache"
    _lowerCAmelCase						      =   "custom_extracted_dir"
    _lowerCAmelCase						      =   tmp_path / "custom_extracted_path"
    if default_extracted:
        _lowerCAmelCase						      =   ("downloads" if default_cache_dir else custom_cache_dir, "extracted")
    else:
        monkeypatch.setattr("datasets.config.EXTRACTED_DATASETS_DIR"		,							SCREAMING_SNAKE_CASE_     )
        monkeypatch.setattr("datasets.config.EXTRACTED_DATASETS_PATH"		,							str(SCREAMING_SNAKE_CASE_     )     )
        _lowerCAmelCase						      =   custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

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


def 						__a(SCREAMING_SNAKE_CASE_	:       Union[str, Any]     ):



    '''simple docstring'''





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


def 						__a(SCREAMING_SNAKE_CASE_	:       int     ):



    '''simple docstring'''





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


def 						__a(SCREAMING_SNAKE_CASE_	:       Any     ):



    '''simple docstring'''





    _lowerCAmelCase						      =   get_from_cache(F'''tmp://{tmpfs_file}'''     )
    with open(SCREAMING_SNAKE_CASE_     ) as f:
        _lowerCAmelCase						      =   f.read()
    assert output_file_content == FILE_CONTENT


@patch("datasets.config.HF_DATASETS_OFFLINE"		,							SCREAMING_SNAKE_CASE_     )
def 						__a():



    '''simple docstring'''





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


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



    '''simple docstring'''





    _lowerCAmelCase						      =   tmp_path_factory.mktemp("data"     ) / "file.html"
    with pytest.raises(SCREAMING_SNAKE_CASE_     ):
        http_get("https://huggingface.co"		,							temp_file=SCREAMING_SNAKE_CASE_     )
    with pytest.raises(SCREAMING_SNAKE_CASE_     ):
        http_head("https://huggingface.co"     )


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



    '''simple docstring'''





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





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



    '''simple docstring'''





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









'''simple docstring'''


def 						__a(SCREAMING_SNAKE_CASE_	:       int		,							SCREAMING_SNAKE_CASE_	:       int     ):



    '''simple docstring'''





    return numa ^ numa < 0


if __name__ == "__main__":
       import doctest

       doctest.testmod()


