





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__						:			Optional[Any]									='''\\n    Text data.\n    Second line of data.'''

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





@pytest.fixture(scope="""session"""				)
def      _lowercase					(						_UpperCAmelCase				)						->  Dict:
				lowerCamelCase	 =tmp_path_factory.mktemp("""data"""				) / (FILE_PATH + '.zstd')
				lowerCamelCase	 =bytes(lowerCAmelCase__			,      """utf-8"""				)
				with zstd.open(lowerCAmelCase__			,      """wb"""				) as f:
								f.write(lowerCAmelCase__				)
				return path





@pytest.fixture
def      _lowercase					(						_UpperCAmelCase				)						->  Optional[Any]:







				with open(os.path.join(tmpfs.local_root_dir			,      lowerCAmelCase__				)			,      """w"""				) as f:
								f.write(lowerCAmelCase__				)
				return FILE_PATH





@pytest.mark.parametrize("""compression_format"""			,      ["""gzip""", """xz""", """zstd"""]				)
def      _lowercase					(						_UpperCAmelCase			,      _UpperCAmelCase			,      _UpperCAmelCase			,      _UpperCAmelCase			,      _UpperCAmelCase			,      _UpperCAmelCase				)						->  Any:
				lowerCamelCase	 ={'gzip': gz_file, 'xz': xz_file, 'zstd': zstd_path}
				lowerCamelCase	 =input_paths[compression_format]
				lowerCamelCase	 =tmp_path / 'cache'
				lowerCamelCase	 =DownloadConfig(cache_dir=lowerCAmelCase__			,      extract_compressed_file=lowerCAmelCase__				)
				lowerCamelCase	 =cached_path(lowerCAmelCase__			,      download_config=lowerCAmelCase__				)
				with open(lowerCAmelCase__				) as f:
								lowerCamelCase	 =f.read()
				with open(lowerCAmelCase__				) as f:
								lowerCamelCase	 =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      _lowercase					(						_UpperCAmelCase			,      _UpperCAmelCase			,      _UpperCAmelCase			,      _UpperCAmelCase			,      _UpperCAmelCase				)						->  str:
				lowerCamelCase	 ='custom_cache'
				lowerCamelCase	 ='custom_extracted_dir'
				lowerCamelCase	 =tmp_path / 'custom_extracted_path'
				if default_extracted:
								lowerCamelCase	 =('downloads' if default_cache_dir else custom_cache_dir, 'extracted')
				else:
								monkeypatch.setattr("""datasets.config.EXTRACTED_DATASETS_DIR"""			,      lowerCAmelCase__				)
								monkeypatch.setattr("""datasets.config.EXTRACTED_DATASETS_PATH"""			,      str(lowerCAmelCase__				)				)
								lowerCamelCase	 =custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

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





def      _lowercase					(						_UpperCAmelCase				)						->  int:
				lowerCamelCase	 =str(Path(lowerCAmelCase__				).resolve()				)
				assert cached_path(lowerCAmelCase__				) == text_file
				# relative path
				lowerCamelCase	 =str(Path(lowerCAmelCase__				).resolve().relative_to(Path(os.getcwd()				)				)				)
				assert cached_path(lowerCAmelCase__				) == text_file





def      _lowercase					(						_UpperCAmelCase				)						->  Dict:
				lowerCamelCase	 =str(tmp_path.resolve() / """__missing_file__.txt"""				)
				with pytest.raises(lowerCAmelCase__				):
								cached_path(lowerCAmelCase__				)
				# relative path
				lowerCamelCase	 ='./__missing_file__.txt'
				with pytest.raises(lowerCAmelCase__				):
								cached_path(lowerCAmelCase__				)





def      _lowercase					(						_UpperCAmelCase				)						->  Dict:
				lowerCamelCase	 =get_from_cache(F"""tmp://{tmpfs_file}"""				)
				with open(lowerCAmelCase__				) as f:
								lowerCamelCase	 =f.read()
				assert output_file_content == FILE_CONTENT





@patch("""datasets.config.HF_DATASETS_OFFLINE"""			,      lowerCAmelCase__				)
def      _lowercase					(						)						->  Dict:







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





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





@patch("""datasets.config.HF_DATASETS_OFFLINE"""			,      lowerCAmelCase__				)
def      _lowercase					(						_UpperCAmelCase				)						->  Optional[int]:
				lowerCamelCase	 =tmp_path_factory.mktemp("""data"""				) / 'file.html'
				with pytest.raises(lowerCAmelCase__				):
								ftp_get("""ftp://huggingface.co"""			,      temp_file=lowerCAmelCase__				)
				with pytest.raises(lowerCAmelCase__				):
								ftp_head("""ftp://huggingface.co"""				)





@patch("""datasets.config.HF_DATASETS_OFFLINE"""			,      lowerCAmelCase__				)
def      _lowercase					(						_UpperCAmelCase				)						->  List[str]:
				lowerCamelCase	 =tmp_path_factory.mktemp("""data"""				) / 'file.html'
				with pytest.raises(lowerCAmelCase__				):
								fsspec_get("""s3://huggingface.co"""			,      temp_file=lowerCAmelCase__				)
				with pytest.raises(lowerCAmelCase__				):
								fsspec_head("""s3://huggingface.co"""				)












from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
							if not (is_transformers_available() and is_torch_available()):
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							from ...utils.dummy_torch_and_transformers_objects import KandinskyPipeline, KandinskyPriorPipeline
else:
							from .pipeline_kandinsky import KandinskyPipeline
							from .pipeline_kandinsky_imgaimg import KandinskyImgaImgPipeline
							from .pipeline_kandinsky_inpaint import KandinskyInpaintPipeline
							from .pipeline_kandinsky_prior import KandinskyPriorPipeline, KandinskyPriorPipelineOutput
							from .text_encoder import MultilingualCLIP





