



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

_lowerCAmelCase										=     '''file'''
@pytest.fixture(scope="""session"""      )
def        _SCREAMING_SNAKE_CASE  (	UpperCamelCase      ):



    """simple docstring"""





    lowerCAmelCase__			:  Tuple											= tmp_path_factory.mktemp("""data"""      ) / (FILE_PATH + """.zstd""")
    lowerCAmelCase__			:  Optional[Any]											= bytes(UpperCamelCase							,   """utf-8"""      )
    with zstd.open(UpperCamelCase							,   """wb"""      ) as f:
        f.write(UpperCamelCase      )
    return path
@pytest.fixture
def        _SCREAMING_SNAKE_CASE  (	UpperCamelCase      ):



    """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        _SCREAMING_SNAKE_CASE  (	UpperCamelCase							,   UpperCamelCase							,   UpperCamelCase							,   UpperCamelCase							,   UpperCamelCase							,   UpperCamelCase      ):



    """simple docstring"""





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



    """simple docstring"""





    lowerCAmelCase__			:  Tuple											= """custom_cache"""
    lowerCAmelCase__			:  int											= """custom_extracted_dir"""
    lowerCAmelCase__			:  int											= tmp_path / """custom_extracted_path"""
    if default_extracted:
        lowerCAmelCase__			:  List[str]											= ("""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      )      )
        lowerCAmelCase__			:  Any											= custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

    lowerCAmelCase__			:  Any											= xz_file
    lowerCAmelCase__			:  Any											= (
        DownloadConfig(extract_compressed_file=UpperCamelCase      )
        if default_cache_dir
        else DownloadConfig(cache_dir=tmp_path / custom_cache_dir							,   extract_compressed_file=UpperCamelCase      )
    )
    lowerCAmelCase__			:  List[Any]											= cached_path(UpperCamelCase							,   download_config=UpperCamelCase      )
    assert Path(UpperCamelCase      ).parent.parts[-2:] == expected
def        _SCREAMING_SNAKE_CASE  (	UpperCamelCase      ):



    """simple docstring"""





    lowerCAmelCase__			:  int											= str(Path(UpperCamelCase      ).resolve()      )
    assert cached_path(UpperCamelCase      ) == text_file
    # relative path
    lowerCAmelCase__			:  List[str]											= str(Path(UpperCamelCase      ).resolve().relative_to(Path(os.getcwd()      )      )      )
    assert cached_path(UpperCamelCase      ) == text_file
def        _SCREAMING_SNAKE_CASE  (	UpperCamelCase      ):



    """simple docstring"""





    lowerCAmelCase__			:  Union[str, Any]											= str(tmp_path.resolve() / """__missing_file__.txt"""      )
    with pytest.raises(UpperCamelCase      ):
        cached_path(UpperCamelCase      )
    # relative path
    lowerCAmelCase__			:  int											= """./__missing_file__.txt"""
    with pytest.raises(UpperCamelCase      ):
        cached_path(UpperCamelCase      )
def        _SCREAMING_SNAKE_CASE  (	UpperCamelCase      ):



    """simple docstring"""





    lowerCAmelCase__			:  int											= get_from_cache(f"""tmp://{tmpfs_file}"""      )
    with open(UpperCamelCase      ) as f:
        lowerCAmelCase__			:  Dict											= f.read()
    assert output_file_content == FILE_CONTENT
@patch("""datasets.config.HF_DATASETS_OFFLINE"""							,   UpperCamelCase      )
def        _SCREAMING_SNAKE_CASE  (	):



    """simple docstring"""





    with pytest.raises(UpperCamelCase      ):
        cached_path("""https://huggingface.co"""      )
@patch("""datasets.config.HF_DATASETS_OFFLINE"""							,   UpperCamelCase      )
def        _SCREAMING_SNAKE_CASE  (	UpperCamelCase      ):



    """simple docstring"""





    lowerCAmelCase__			:  List[str]											= 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        _SCREAMING_SNAKE_CASE  (	UpperCamelCase      ):



    """simple docstring"""





    lowerCAmelCase__			:  str											= 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        _SCREAMING_SNAKE_CASE  (	UpperCamelCase      ):



    """simple docstring"""





    lowerCAmelCase__			:  str											= 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"""      )







'''simple docstring'''





_lowerCAmelCase										=     '''Input must be a string of 8 numbers plus letter'''
_lowerCAmelCase										=     '''TRWAGMYFPDXBNJZSQVHLCKE'''
def        _SCREAMING_SNAKE_CASE  (	UpperCamelCase      ):



    """simple docstring"""





    if not isinstance(UpperCamelCase							,   UpperCamelCase      ):
        lowerCAmelCase__			:  List[Any]											= f"""Expected string as input, found {type(UpperCamelCase      ).__name__}"""
        raise TypeError(UpperCamelCase      )

    lowerCAmelCase__			:  Union[str, Any]											= spanish_id.replace("""-"""							,   """"""      ).upper()
    if len(UpperCamelCase      ) != 9:
        raise ValueError(UpperCamelCase      )

    try:
        lowerCAmelCase__			:  Optional[int]											= int(spanish_id_clean[0:8]      )
        lowerCAmelCase__			:  int											= spanish_id_clean[8]
    except ValueError as ex:
        raise ValueError(UpperCamelCase      ) from ex

    if letter.isdigit():
        raise ValueError(UpperCamelCase      )

    return letter == LOOKUP_LETTERS[number % 23]


if __name__ == "__main__":
 import doctest

 doctest.testmod()


