





'''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 :  List[Any]										=  """\
    Text data.
    Second line of data."""

__lowerCamelCase :  Tuple										=  """file"""



@pytest.fixture(scope="session"  )
def 	A_       (  _lowerCAmelCase  )		->				List[str]:
 UpperCamelCase			:      Dict 			=     tmp_path_factory.mktemp("data"  ) / (FILE_PATH + ".zstd")
 UpperCamelCase			:      List[Any] 			=     bytes(lowerCAmelCase__							,     "utf-8"  )
 with zstd.open(lowerCAmelCase__							,     "wb"  ) as f:
  f.write(lowerCAmelCase__  )
 return path



@pytest.fixture
def 	A_       (  _lowerCAmelCase  )		->				Optional[int]:
 with open(os.path.join(tmpfs.local_root_dir							,     lowerCAmelCase__  )							,     "w"  ) as f:
  f.write(lowerCAmelCase__  )
 return FILE_PATH



@pytest.mark.parametrize("compression_format"							,     ["gzip", "xz", "zstd"]  )
def 	A_       (  _lowerCAmelCase							,     _lowerCAmelCase							,     _lowerCAmelCase							,     _lowerCAmelCase							,     _lowerCAmelCase							,     _lowerCAmelCase  )		->				str:
 UpperCamelCase			:      int 			=     {"gzip": gz_file, "xz": xz_file, "zstd": zstd_path}
 UpperCamelCase			:      Union[str, Any] 			=     input_paths[compression_format]
 UpperCamelCase			:      List[Any] 			=     tmp_path / "cache"
 UpperCamelCase			:      Union[str, Any] 			=     DownloadConfig(cache_dir=lowerCAmelCase__							,     extract_compressed_file=lowerCAmelCase__  )
 UpperCamelCase			:      str 			=     cached_path(lowerCAmelCase__							,     download_config=lowerCAmelCase__  )
 with open(lowerCAmelCase__  ) as f:
  UpperCamelCase			:      Optional[int] 			=     f.read()
 with open(lowerCAmelCase__  ) as f:
  UpperCamelCase			:      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 	A_       (  _lowerCAmelCase							,     _lowerCAmelCase							,     _lowerCAmelCase							,     _lowerCAmelCase							,     _lowerCAmelCase  )		->				Any:
 UpperCamelCase			:      Dict 			=     "custom_cache"
 UpperCamelCase			:      Dict 			=     "custom_extracted_dir"
 UpperCamelCase			:      List[str] 			=     tmp_path / "custom_extracted_path"
 if default_extracted:
  UpperCamelCase			:      Optional[int] 			=     ("downloads" if default_cache_dir else custom_cache_dir, "extracted")
 else:
  monkeypatch.setattr("datasets.config.EXTRACTED_DATASETS_DIR"							,     lowerCAmelCase__  )
  monkeypatch.setattr("datasets.config.EXTRACTED_DATASETS_PATH"							,     str(lowerCAmelCase__  )  )
  UpperCamelCase			:      List[str] 			=     custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

 UpperCamelCase			:      Optional[int] 			=     xz_file
 UpperCamelCase			:      Tuple 			=     (
     DownloadConfig(extract_compressed_file=lowerCAmelCase__  )
     if default_cache_dir
     else DownloadConfig(cache_dir=tmp_path / custom_cache_dir							,     extract_compressed_file=lowerCAmelCase__  )
 )
 UpperCamelCase			:      Dict 			=     cached_path(lowerCAmelCase__							,     download_config=lowerCAmelCase__  )
 assert Path(lowerCAmelCase__  ).parent.parts[-2:] == expected



def 	A_       (  _lowerCAmelCase  )		->				Any:
 # absolute path
 UpperCamelCase			:      str 			=     str(Path(lowerCAmelCase__  ).resolve()  )
 assert cached_path(lowerCAmelCase__  ) == text_file
 # relative path
 UpperCamelCase			:      Dict 			=     str(Path(lowerCAmelCase__  ).resolve().relative_to(Path(os.getcwd()  )  )  )
 assert cached_path(lowerCAmelCase__  ) == text_file



def 	A_       (  _lowerCAmelCase  )		->				int:
 # absolute path
 UpperCamelCase			:      List[Any] 			=     str(tmp_path.resolve() / "__missing_file__.txt"  )
 with pytest.raises(lowerCAmelCase__  ):
  cached_path(lowerCAmelCase__  )
 # relative path
 UpperCamelCase			:      Tuple 			=     "./__missing_file__.txt"
 with pytest.raises(lowerCAmelCase__  ):
  cached_path(lowerCAmelCase__  )



def 	A_       (  _lowerCAmelCase  )		->				Optional[Any]:
 UpperCamelCase			:      Union[str, Any] 			=     get_from_cache(F"""tmp://{tmpfs_file}"""  )
 with open(lowerCAmelCase__  ) as f:
  UpperCamelCase			:      str 			=     f.read()
 assert output_file_content == FILE_CONTENT



@patch("datasets.config.HF_DATASETS_OFFLINE"							,     lowerCAmelCase__  )
def 	A_       (  )		->				Optional[Any]:
 with pytest.raises(lowerCAmelCase__  ):
  cached_path("https://huggingface.co"  )



@patch("datasets.config.HF_DATASETS_OFFLINE"							,     lowerCAmelCase__  )
def 	A_       (  _lowerCAmelCase  )		->				Optional[int]:
 UpperCamelCase			:      str 			=     tmp_path_factory.mktemp("data"  ) / "file.html"
 with pytest.raises(lowerCAmelCase__  ):
  http_get("https://huggingface.co"							,     temp_file=lowerCAmelCase__  )
 with pytest.raises(lowerCAmelCase__  ):
  http_head("https://huggingface.co"  )



@patch("datasets.config.HF_DATASETS_OFFLINE"							,     lowerCAmelCase__  )
def 	A_       (  _lowerCAmelCase  )		->				Union[str, Any]:
 UpperCamelCase			:      Tuple 			=     tmp_path_factory.mktemp("data"  ) / "file.html"
 with pytest.raises(lowerCAmelCase__  ):
  ftp_get("ftp://huggingface.co"							,     temp_file=lowerCAmelCase__  )
 with pytest.raises(lowerCAmelCase__  ):
  ftp_head("ftp://huggingface.co"  )



@patch("datasets.config.HF_DATASETS_OFFLINE"							,     lowerCAmelCase__  )
def 	A_       (  _lowerCAmelCase  )		->				Optional[int]:
 UpperCamelCase			:      str 			=     tmp_path_factory.mktemp("data"  ) / "file.html"
 with pytest.raises(lowerCAmelCase__  ):
  fsspec_get("s3://huggingface.co"							,     temp_file=lowerCAmelCase__  )
 with pytest.raises(lowerCAmelCase__  ):
  fsspec_head("s3://huggingface.co"  )







from math import loga



def 	A_       (  _lowerCAmelCase  )		->				int:
 if a < 0:
  raise ValueError("Input value must be a positive integer"  )
 elif isinstance(_lowerCAmelCase							,     _lowerCAmelCase  ):
  raise TypeError("Input value must be a 'int' type"  )
 return 0 if (a == 0) else int(loga(a & -a  )  )


if __name__ == "__main__":
    import doctest

    doctest.testmod()
