




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

lowerCamelCase_			=						'file'
@pytest.fixture(scope="session"  )
def 			SCREAMING_SNAKE_CASE_			(							__A    :			Dict  )			->			str:
   _SCREAMING_SNAKE_CASE	=				tmp_path_factory.mktemp("data"  ) / (FILE_PATH + ".zstd")
   _SCREAMING_SNAKE_CASE	=				bytes(__A	,					"utf-8"  )
   with zstd.open(__A	,					"wb"  ) as f:
      f.write(__A  )
   return path
@pytest.fixture
def 			SCREAMING_SNAKE_CASE_			(							__A    :			Any  )			->			Any:
   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 			SCREAMING_SNAKE_CASE_			(							__A    :			Any	,					__A    :			Union[str, Any]	,					__A    :			List[str]	,					__A    :			List[str]	,					__A    :			Union[str, Any]	,					__A    :			Union[str, Any]  )			->			Optional[int]:
   _SCREAMING_SNAKE_CASE	=				{"gzip": gz_file, "xz": xz_file, "zstd": zstd_path}
   _SCREAMING_SNAKE_CASE	=				input_paths[compression_format]
   _SCREAMING_SNAKE_CASE	=				tmp_path / "cache"
   _SCREAMING_SNAKE_CASE	=				DownloadConfig(cache_dir=__A	,					extract_compressed_file=__A  )
   _SCREAMING_SNAKE_CASE	=				cached_path(__A	,					download_config=__A  )
   with open(__A  ) as f:
      _SCREAMING_SNAKE_CASE	=				f.read()
   with open(__A  ) as f:
      _SCREAMING_SNAKE_CASE	=				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_			(							__A    :			Optional[int]	,					__A    :			Optional[int]	,					__A    :			str	,					__A    :			str	,					__A    :			Optional[int]  )			->			List[str]:
   _SCREAMING_SNAKE_CASE	=				"custom_cache"
   _SCREAMING_SNAKE_CASE	=				"custom_extracted_dir"
   _SCREAMING_SNAKE_CASE	=				tmp_path / "custom_extracted_path"
   if default_extracted:
      _SCREAMING_SNAKE_CASE	=				("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  )  )
      _SCREAMING_SNAKE_CASE	=				custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

   _SCREAMING_SNAKE_CASE	=				xz_file
   _SCREAMING_SNAKE_CASE	=				(
       DownloadConfig(extract_compressed_file=__A  )
       if default_cache_dir
       else DownloadConfig(cache_dir=tmp_path / custom_cache_dir	,					extract_compressed_file=__A  )
   )
   _SCREAMING_SNAKE_CASE	=				cached_path(__A	,					download_config=__A  )
   assert Path(__A  ).parent.parts[-2:] == expected
def 			SCREAMING_SNAKE_CASE_			(							__A    :			int  )			->			List[str]:
   # absolute path
   _SCREAMING_SNAKE_CASE	=				str(Path(__A  ).resolve()  )
   assert cached_path(__A  ) == text_file
   # relative path
   _SCREAMING_SNAKE_CASE	=				str(Path(__A  ).resolve().relative_to(Path(os.getcwd()  )  )  )
   assert cached_path(__A  ) == text_file
def 			SCREAMING_SNAKE_CASE_			(							__A    :			str  )			->			Any:
   # absolute path
   _SCREAMING_SNAKE_CASE	=				str(tmp_path.resolve() / "__missing_file__.txt"  )
   with pytest.raises(__A  ):
      cached_path(__A  )
   # relative path
   _SCREAMING_SNAKE_CASE	=				"./__missing_file__.txt"
   with pytest.raises(__A  ):
      cached_path(__A  )
def 			SCREAMING_SNAKE_CASE_			(							__A    :			Optional[int]  )			->			Optional[Any]:
   _SCREAMING_SNAKE_CASE	=				get_from_cache(f"""tmp://{tmpfs_file}"""  )
   with open(__A  ) as f:
      _SCREAMING_SNAKE_CASE	=				f.read()
   assert output_file_content == FILE_CONTENT
@patch("datasets.config.HF_DATASETS_OFFLINE"	,					__A  )
def 			SCREAMING_SNAKE_CASE_			(							)			->			int:
   with pytest.raises(__A  ):
      cached_path("https://huggingface.co"  )
@patch("datasets.config.HF_DATASETS_OFFLINE"	,					__A  )
def 			SCREAMING_SNAKE_CASE_			(							__A    :			Union[str, Any]  )			->			Optional[Any]:
   _SCREAMING_SNAKE_CASE	=				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 			SCREAMING_SNAKE_CASE_			(							__A    :			Dict  )			->			Optional[Any]:
   _SCREAMING_SNAKE_CASE	=				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 			SCREAMING_SNAKE_CASE_			(							__A    :			List[str]  )			->			List[str]:
   _SCREAMING_SNAKE_CASE	=				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"  )







'''simple docstring'''



import unittest

from diffusers.pipelines.pipeline_utils import is_safetensors_compatible







class 		lowercase_   (			unittest.TestCase       ):


   """simple docstring"""

   def 							lowerCAmelCase_      (   self :     List[Any]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "safety_checker/pytorch_model.bin",
          "safety_checker/model.safetensors",
          "vae/diffusion_pytorch_model.bin",
          "vae/diffusion_pytorch_model.safetensors",
          "text_encoder/pytorch_model.bin",
          "text_encoder/model.safetensors",
          "unet/diffusion_pytorch_model.bin",
          "unet/diffusion_pytorch_model.safetensors",
      ]
      self.assertTrue(is_safetensors_compatible(__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     Dict   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "unet/diffusion_pytorch_model.bin",
          "unet/diffusion_pytorch_model.safetensors",
      ]
      self.assertTrue(is_safetensors_compatible(__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     Tuple   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "safety_checker/pytorch_model.bin",
          "safety_checker/model.safetensors",
          "vae/diffusion_pytorch_model.bin",
          "vae/diffusion_pytorch_model.safetensors",
          "text_encoder/pytorch_model.bin",
          "text_encoder/model.safetensors",
          "unet/diffusion_pytorch_model.bin",
          # Removed: 'unet/diffusion_pytorch_model.safetensors',
      ]
      self.assertFalse(is_safetensors_compatible(__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     List[str]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "text_encoder/pytorch_model.bin",
          "text_encoder/model.safetensors",
      ]
      self.assertTrue(is_safetensors_compatible(__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     int   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "safety_checker/pytorch_model.bin",
          "safety_checker/model.safetensors",
          "vae/diffusion_pytorch_model.bin",
          "vae/diffusion_pytorch_model.safetensors",
          "text_encoder/pytorch_model.bin",
          # Removed: 'text_encoder/model.safetensors',
          "unet/diffusion_pytorch_model.bin",
          "unet/diffusion_pytorch_model.safetensors",
      ]
      self.assertFalse(is_safetensors_compatible(__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     Optional[int]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "safety_checker/pytorch_model.fp16.bin",
          "safety_checker/model.fp16.safetensors",
          "vae/diffusion_pytorch_model.fp16.bin",
          "vae/diffusion_pytorch_model.fp16.safetensors",
          "text_encoder/pytorch_model.fp16.bin",
          "text_encoder/model.fp16.safetensors",
          "unet/diffusion_pytorch_model.fp16.bin",
          "unet/diffusion_pytorch_model.fp16.safetensors",
      ]
      _SCREAMING_SNAKE_CASE	=				"fp16"
      self.assertTrue(is_safetensors_compatible(__lowerCamelCase  ,				variant=__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     Any   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "unet/diffusion_pytorch_model.fp16.bin",
          "unet/diffusion_pytorch_model.fp16.safetensors",
      ]
      _SCREAMING_SNAKE_CASE	=				"fp16"
      self.assertTrue(is_safetensors_compatible(__lowerCamelCase  ,				variant=__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     Tuple   ):

      """simple docstring"""
      # pass variant but use the non-variant filenames
      _SCREAMING_SNAKE_CASE	=				[
          "unet/diffusion_pytorch_model.bin",
          "unet/diffusion_pytorch_model.safetensors",
      ]
      _SCREAMING_SNAKE_CASE	=				"fp16"
      self.assertTrue(is_safetensors_compatible(__lowerCamelCase  ,				variant=__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     int   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "safety_checker/pytorch_model.fp16.bin",
          "safety_checker/model.fp16.safetensors",
          "vae/diffusion_pytorch_model.fp16.bin",
          "vae/diffusion_pytorch_model.fp16.safetensors",
          "text_encoder/pytorch_model.fp16.bin",
          "text_encoder/model.fp16.safetensors",
          "unet/diffusion_pytorch_model.fp16.bin",
          # Removed: 'unet/diffusion_pytorch_model.fp16.safetensors',
      ]
      _SCREAMING_SNAKE_CASE	=				"fp16"
      self.assertFalse(is_safetensors_compatible(__lowerCamelCase  ,				variant=__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     Dict   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "text_encoder/pytorch_model.fp16.bin",
          "text_encoder/model.fp16.safetensors",
      ]
      _SCREAMING_SNAKE_CASE	=				"fp16"
      self.assertTrue(is_safetensors_compatible(__lowerCamelCase  ,				variant=__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     str   ):

      """simple docstring"""
      # pass variant but use the non-variant filenames
      _SCREAMING_SNAKE_CASE	=				[
          "text_encoder/pytorch_model.bin",
          "text_encoder/model.safetensors",
      ]
      _SCREAMING_SNAKE_CASE	=				"fp16"
      self.assertTrue(is_safetensors_compatible(__lowerCamelCase  ,				variant=__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     Optional[Any]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "safety_checker/pytorch_model.fp16.bin",
          "safety_checker/model.fp16.safetensors",
          "vae/diffusion_pytorch_model.fp16.bin",
          "vae/diffusion_pytorch_model.fp16.safetensors",
          "text_encoder/pytorch_model.fp16.bin",
          # 'text_encoder/model.fp16.safetensors',
          "unet/diffusion_pytorch_model.fp16.bin",
          "unet/diffusion_pytorch_model.fp16.safetensors",
      ]
      _SCREAMING_SNAKE_CASE	=				"fp16"
      self.assertFalse(is_safetensors_compatible(__lowerCamelCase  ,				variant=__lowerCamelCase   )   )

