

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

_lowerCamelCase      :					List[str]					    =      '''file'''





@pytest.fixture(scope='''session'''			)
def      A__					(			__A  :						Optional[int]			)					->Optional[int]:
      __A    =tmp_path_factory.mktemp('''data'''			) / (FILE_PATH + '''.zstd''')
      __A    =bytes(__A     ,				'''utf-8'''			)
      with zstd.open(__A     ,				'''wb'''			) as f:
            f.write(__A			)
      return path





@pytest.fixture
def      A__					(			__A  :						Tuple			)					->Dict:
      with open(os.path.join(tmpfs.local_root_dir     ,				__A			)     ,				'''w'''			) as f:
            f.write(__A			)
      return FILE_PATH





@pytest.mark.parametrize('''compression_format'''     ,				['''gzip''', '''xz''', '''zstd''']			)
def      A__					(			__A  :						int     ,				__A  :						int     ,				__A  :						Any     ,				__A  :						Any     ,				__A  :						int     ,				__A  :						int			)					->Union[str, Any]:
      __A    ={'''gzip''': gz_file, '''xz''': xz_file, '''zstd''': zstd_path}
      __A    =input_paths[compression_format]
      __A    =tmp_path / '''cache'''
      __A    =DownloadConfig(cache_dir=__A     ,				extract_compressed_file=__A			)
      __A    =cached_path(__A     ,				download_config=__A			)
      with open(__A			) as f:
            __A    =f.read()
      with open(__A			) 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      A__					(			__A  :						List[Any]     ,				__A  :						str     ,				__A  :						int     ,				__A  :						Dict     ,				__A  :						int			)					->int:
      __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'''     ,				__A			)
            monkeypatch.setattr('''datasets.config.EXTRACTED_DATASETS_PATH'''     ,				str(__A			)			)
            __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=__A			)
          if default_cache_dir
          else DownloadConfig(cache_dir=tmp_path / custom_cache_dir     ,				extract_compressed_file=__A			)
      )
      __A    =cached_path(__A     ,				download_config=__A			)
      assert Path(__A			).parent.parts[-2:] == expected





def      A__					(			__A  :						Dict			)					->Optional[int]:
      # absolute path
      __A    =str(Path(__A			).resolve()			)
      assert cached_path(__A			) == text_file
      # relative path
      __A    =str(Path(__A			).resolve().relative_to(Path(os.getcwd()			)			)			)
      assert cached_path(__A			) == text_file





def      A__					(			__A  :						Optional[int]			)					->Any:
      # absolute path
      __A    =str(tmp_path.resolve() / '''__missing_file__.txt'''			)
      with pytest.raises(__A			):
            cached_path(__A			)
      # relative path
      __A    ='''./__missing_file__.txt'''
      with pytest.raises(__A			):
            cached_path(__A			)





def      A__					(			__A  :						Tuple			)					->Optional[Any]:
      __A    =get_from_cache(F'''tmp://{tmpfs_file}'''			)
      with open(__A			) as f:
            __A    =f.read()
      assert output_file_content == FILE_CONTENT





@patch('''datasets.config.HF_DATASETS_OFFLINE'''     ,				__A			)
def      A__					(			)					->str:
      with pytest.raises(__A			):
            cached_path('''https://huggingface.co'''			)





@patch('''datasets.config.HF_DATASETS_OFFLINE'''     ,				__A			)
def      A__					(			__A  :						Optional[int]			)					->Optional[Any]:
      __A    =tmp_path_factory.mktemp('''data'''			) / '''file.html'''
      with pytest.raises(__A			):
            http_get('''https://huggingface.co'''     ,				temp_file=__A			)
      with pytest.raises(__A			):
            http_head('''https://huggingface.co'''			)





@patch('''datasets.config.HF_DATASETS_OFFLINE'''     ,				__A			)
def      A__					(			__A  :						List[str]			)					->Optional[int]:
      __A    =tmp_path_factory.mktemp('''data'''			) / '''file.html'''
      with pytest.raises(__A			):
            ftp_get('''ftp://huggingface.co'''     ,				temp_file=__A			)
      with pytest.raises(__A			):
            ftp_head('''ftp://huggingface.co'''			)





@patch('''datasets.config.HF_DATASETS_OFFLINE'''     ,				__A			)
def      A__					(			__A  :						List[Any]			)					->Optional[int]:
      __A    =tmp_path_factory.mktemp('''data'''			) / '''file.html'''
      with pytest.raises(__A			):
            fsspec_get('''s3://huggingface.co'''     ,				temp_file=__A			)
      with pytest.raises(__A			):
            fsspec_head('''s3://huggingface.co'''			)





import inspect
import re
from hashlib import shaaaa
from typing import Dict, List

from .arrow import arrow
from .audiofolder import audiofolder
from .csv import csv
from .imagefolder import imagefolder
from .json import json
from .pandas import pandas
from .parquet import parquet
from .sql import sql  # noqa F401
from .text import text





def      A__					(			__A  :						List[str]			)					->str:
      __A    =[]
      for line in lines:
            __A    =re.sub(r'''#.*'''     ,				''''''     ,				__A			)  # remove comments
            if line:
                  filtered_lines.append(__A			)
      __A    ='''\n'''.join(__A			)

      # Make a hash from all this code
      __A    =full_str.encode('''utf-8'''			)
      return shaaaa(__A			).hexdigest()


# get importable module names and hash for caching
_lowerCamelCase      :					Dict					    =      {
    '''csv''': (csv.__name__, _hash_python_lines(inspect.getsource(csv).splitlines())),
    '''json''': (json.__name__, _hash_python_lines(inspect.getsource(json).splitlines())),
    '''pandas''': (pandas.__name__, _hash_python_lines(inspect.getsource(pandas).splitlines())),
    '''parquet''': (parquet.__name__, _hash_python_lines(inspect.getsource(parquet).splitlines())),
    '''arrow''': (arrow.__name__, _hash_python_lines(inspect.getsource(arrow).splitlines())),
    '''text''': (text.__name__, _hash_python_lines(inspect.getsource(text).splitlines())),
    '''imagefolder''': (imagefolder.__name__, _hash_python_lines(inspect.getsource(imagefolder).splitlines())),
    '''audiofolder''': (audiofolder.__name__, _hash_python_lines(inspect.getsource(audiofolder).splitlines())),
}

# Used to infer the module to use based on the data files extensions
_lowerCamelCase      :					int					    =      {
    '''.csv''': ('''csv''', {}),
    '''.tsv''': ('''csv''', {'''sep''': '''\t'''}),
    '''.json''': ('''json''', {}),
    '''.jsonl''': ('''json''', {}),
    '''.parquet''': ('''parquet''', {}),
    '''.arrow''': ('''arrow''', {}),
    '''.txt''': ('''text''', {}),
}
_EXTENSION_TO_MODULE.update({ext: ('''imagefolder''', {}) for ext in imagefolder.ImageFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext.upper(): ('''imagefolder''', {}) for ext in imagefolder.ImageFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext: ('''audiofolder''', {}) for ext in audiofolder.AudioFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext.upper(): ('''audiofolder''', {}) for ext in audiofolder.AudioFolder.EXTENSIONS})
_lowerCamelCase      :					List[str]					    =      {'''imagefolder''', '''audiofolder'''}

# Used to filter data files based on extensions given a module name
_lowerCamelCase      :					Dict[str, List[str]]					    =      {}
for _ext, (_module, _) in _EXTENSION_TO_MODULE.items():
   _MODULE_TO_EXTENSIONS.setdefault(_module, []).append(_ext)

_MODULE_TO_EXTENSIONS["imagefolder"].append('''.zip''')


_MODULE_TO_EXTENSIONS["audiofolder"].append('''.zip''')


