



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


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

A_		:     Any        =     '''file'''


@pytest.fixture(scope="session"					)
def 	UpperCAmelCase__      (      UpperCAmelCase__ :List[Any]					):






 '''simple docstring'''

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


@pytest.fixture
def 	UpperCAmelCase__      (      UpperCAmelCase__ :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 	UpperCAmelCase__      (      UpperCAmelCase__ :Any ,					UpperCAmelCase__ :Optional[Any] ,					UpperCAmelCase__ :Optional[Any] ,					UpperCAmelCase__ :Dict ,					UpperCAmelCase__ :Tuple ,					UpperCAmelCase__ :Optional[Any]					):






 '''simple docstring'''

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






 '''simple docstring'''

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

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


def 	UpperCAmelCase__      (      UpperCAmelCase__ :int					):






 '''simple docstring'''

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


def 	UpperCAmelCase__      (      UpperCAmelCase__ :Optional[int]					):






 '''simple docstring'''

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


def 	UpperCAmelCase__      (      UpperCAmelCase__ :Union[str, Any]					):






 '''simple docstring'''

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


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






 '''simple docstring'''

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


@patch("datasets.config.HF_DATASETS_OFFLINE" ,					UpperCAmelCase__					)
def 	UpperCAmelCase__      (      UpperCAmelCase__ :Dict					):






 '''simple docstring'''

 a						=		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 	UpperCAmelCase__      (      UpperCAmelCase__ :Optional[Any]					):






 '''simple docstring'''

 a						=		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 	UpperCAmelCase__      (      UpperCAmelCase__ :Optional[int]					):






 '''simple docstring'''

 a						=		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"					)






def 	UpperCAmelCase__      (      UpperCAmelCase__ :Any					):






 '''simple docstring'''

 if not head:
  return True
 # split the list to two parts
 a		,  a						=		head.next, head
 while fast and fast.next:
  a						=		fast.next.next
  a						=		slow.next
 a						=		slow.next
 a						=		None  # Don't forget here! But forget still works!
 # reverse the second part
 a						=		None
 while second:
  a						=		second.next
  a						=		node
  a						=		second
  a						=		nxt
 # compare two parts
 # second part has the same or one less node
 while node:
  if node.val != head.val:
   return False
  a						=		node.next
  a						=		head.next
 return True


def 	UpperCAmelCase__      (      UpperCAmelCase__ :str					):






 '''simple docstring'''

 if not head or not head.next:
  return True

 # 1. Get the midpoint (slow)
 a						=		a						=		a						=		head
 while fast and fast.next:
  a		,  a						=		fast.next.next, slow.next

 # 2. Push the second half into the stack
 a						=		[slow.val]
 while slow.next:
  a						=		slow.next
  stack.append(slow.val					)

 # 3. Comparison
 while stack:
  if stack.pop() != cur.val:
   return False
  a						=		cur.next

 return True




def 	UpperCAmelCase__      (      UpperCAmelCase__ :Any					):






 '''simple docstring'''

 if not head or not head.next:
  return True
 a						=		{}
 a						=		0
 while head:
  if head.val in d:
   d[head.val].append(UpperCAmelCase__					)
  else:
   a						=		[pos]
  a						=		head.next
  pos += 1
 a						=		pos - 1
 a						=		0
 for v in d.values():
  if len(UpperCAmelCase__					) % 2 != 0:
   middle += 1
  else:
   a						=		0
   for i in range(0 ,					len(UpperCAmelCase__					)					):
    if v[i] + v[len(UpperCAmelCase__					) - 1 - step] != checksum:
     return False
    step += 1
  if middle > 1:
   return False
 return True

