





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

UpperCAmelCase     :     Optional[Any]							=	'''file'''
@pytest.fixture(scope="session"						)
def 					_A	(		SCREAMING_SNAKE_CASE					:       Any						):





  """simple docstring"""



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





  """simple docstring"""



  with open(os.path.join(tmpfs.local_root_dir	,	snake_case_						)	,	"w"						) as f:
    f.write(snake_case_						)
  return FILE_PATH
@pytest.mark.parametrize("compression_format"	,	["gzip", "xz", "zstd"]						)
def 					_A	(		SCREAMING_SNAKE_CASE					:       Optional[Any]	,	SCREAMING_SNAKE_CASE					:       Optional[int]	,	SCREAMING_SNAKE_CASE					:       Optional[int]	,	SCREAMING_SNAKE_CASE					:       List[Any]	,	SCREAMING_SNAKE_CASE					:       str	,	SCREAMING_SNAKE_CASE					:       str						):





  """simple docstring"""



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





  """simple docstring"""



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

  a__     : int  =xz_file
  a__     : List[str]  =(
      DownloadConfig(extract_compressed_file=snake_case_						)
      if default_cache_dir
      else DownloadConfig(cache_dir=tmp_path / custom_cache_dir	,	extract_compressed_file=snake_case_						)
  )
  a__     : List[Any]  =cached_path(snake_case_	,	download_config=snake_case_						)
  assert Path(snake_case_						).parent.parts[-2:] == expected
def 					_A	(		SCREAMING_SNAKE_CASE					:       int						):





  """simple docstring"""



  a__     : Tuple  =str(Path(snake_case_						).resolve()						)
  assert cached_path(snake_case_						) == text_file
  # relative path
  a__     : str  =str(Path(snake_case_						).resolve().relative_to(Path(os.getcwd()						)						)						)
  assert cached_path(snake_case_						) == text_file
def 					_A	(		SCREAMING_SNAKE_CASE					:       Tuple						):





  """simple docstring"""



  a__     : Any  =str(tmp_path.resolve() / "__missing_file__.txt"						)
  with pytest.raises(snake_case_						):
    cached_path(snake_case_						)
  # relative path
  a__     : Any  ="""./__missing_file__.txt"""
  with pytest.raises(snake_case_						):
    cached_path(snake_case_						)
def 					_A	(		SCREAMING_SNAKE_CASE					:       str						):





  """simple docstring"""



  a__     : str  =get_from_cache(f'''tmp://{tmpfs_file}'''						)
  with open(snake_case_						) as f:
    a__     : Any  =f.read()
  assert output_file_content == FILE_CONTENT
@patch("datasets.config.HF_DATASETS_OFFLINE"	,	snake_case_						)
def 					_A	(		):





  """simple docstring"""



  with pytest.raises(snake_case_						):
    cached_path("https://huggingface.co"						)
@patch("datasets.config.HF_DATASETS_OFFLINE"	,	snake_case_						)
def 					_A	(		SCREAMING_SNAKE_CASE					:       Any						):





  """simple docstring"""



  a__     : Any  =tmp_path_factory.mktemp("data"						) / """file.html"""
  with pytest.raises(snake_case_						):
    http_get("https://huggingface.co"	,	temp_file=snake_case_						)
  with pytest.raises(snake_case_						):
    http_head("https://huggingface.co"						)
@patch("datasets.config.HF_DATASETS_OFFLINE"	,	snake_case_						)
def 					_A	(		SCREAMING_SNAKE_CASE					:       Union[str, Any]						):





  """simple docstring"""



  a__     : List[Any]  =tmp_path_factory.mktemp("data"						) / """file.html"""
  with pytest.raises(snake_case_						):
    ftp_get("ftp://huggingface.co"	,	temp_file=snake_case_						)
  with pytest.raises(snake_case_						):
    ftp_head("ftp://huggingface.co"						)





@patch("datasets.config.HF_DATASETS_OFFLINE"	,	snake_case_						)
def 					_A	(		SCREAMING_SNAKE_CASE					:       Any						):





  """simple docstring"""



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













from typing import Optional

from .. import Features, NamedSplit
from ..packaged_modules.text.text import Text
from ..utils.typing import NestedDataStructureLike, PathLike
from .abc import AbstractDatasetReader







class     __lowerCAmelCase	(   UpperCamelCase__):







      def __init__(   self		,				lowerCAmelCase__		,				lowerCAmelCase__ = None		,				lowerCAmelCase__ = None		,				lowerCAmelCase__ = None		,				lowerCAmelCase__ = False		,				lowerCAmelCase__ = False		,				lowerCAmelCase__ = None		,				**lowerCAmelCase__		,				)       ->						int:



        '''simple docstring'''




        super().__init__(
            lowerCAmelCase__		,				split=lowerCAmelCase__		,				features=lowerCAmelCase__		,				cache_dir=lowerCAmelCase__		,				keep_in_memory=lowerCAmelCase__		,				streaming=lowerCAmelCase__		,				num_proc=lowerCAmelCase__		,				**lowerCAmelCase__		,				)
        a__     : Tuple  =path_or_paths if isinstance(lowerCAmelCase__		,				lowerCAmelCase__							) else {self.split: path_or_paths}
        a__     : List[str]  =Text(
            cache_dir=lowerCAmelCase__		,				data_files=lowerCAmelCase__		,				features=lowerCAmelCase__		,				**lowerCAmelCase__		,				)







      def        _lowercase  (   self							)       ->						List[Any]:



        '''simple docstring'''




        if self.streaming:
          a__     : str  =self.builder.as_streaming_dataset(split=self.split							)
        # Build regular (map-style) dataset
        else:
          a__     : Dict  =None
          a__     : Optional[Any]  =None
          a__     : Union[str, Any]  =None
          a__     : Tuple  =None

          self.builder.download_and_prepare(
              download_config=lowerCAmelCase__		,				download_mode=lowerCAmelCase__		,				verification_mode=lowerCAmelCase__		,				base_path=lowerCAmelCase__		,				num_proc=self.num_proc		,				)
          a__     : Tuple  =self.builder.as_dataset(
              split=self.split		,				verification_mode=lowerCAmelCase__		,				in_memory=self.keep_in_memory							)
        return dataset






