'''simple docstring'''




import importlib
import os

import fsspec
import pytest
from fsspec import register_implementation
from fsspec.registry import _registry as _fsspec_registry

from datasets.filesystems import COMPRESSION_FILESYSTEMS, HfFileSystem, extract_path_from_uri, is_remote_filesystem

from .utils import require_lza, require_zstandard






def 							snake_case__				(     _A:   int					)       ->      Any:

		'''simple docstring'''


		assert "mock" in _fsspec_registry
		assert "bz2" in _fsspec_registry






def 							snake_case__				(     )       ->      Union[str, Any]:

		'''simple docstring'''


		assert "mock" not in _fsspec_registry
		assert "bz2" in _fsspec_registry






def 							snake_case__				(     )       ->      Optional[Any]:

		'''simple docstring'''


		lowerCAmelCase     =    """mock-s3-bucket"""
		lowerCAmelCase     =    f"s3://{mock_bucket}"
		lowerCAmelCase     =    extract_path_from_uri(_A					)
		assert dataset_path.startswith("""s3://"""					) is False

		lowerCAmelCase     =    """./local/path"""
		lowerCAmelCase     =    extract_path_from_uri(_A					)
		assert dataset_path == new_dataset_path






def 							snake_case__				(     _A:   str					)       ->      Any:

		'''simple docstring'''


		lowerCAmelCase     =    is_remote_filesystem(_A					)
		assert is_remote is True

		lowerCAmelCase     =    fsspec.filesystem("""file"""					)

		lowerCAmelCase     =    is_remote_filesystem(_A					)
		assert is_remote is False






@pytest.mark.parametrize("""compression_fs_class""" ,  _A					)
def 							snake_case__				(     _A:   str ,  _A:   Union[str, Any] ,  _A:   List[Any] ,  _A:   Optional[int] ,  _A:   Any ,  _A:   Optional[Any] ,  _A:   Any					)       ->      Any:

		'''simple docstring'''


		lowerCAmelCase     =    {"""gzip""": gz_file, """xz""": xz_file, """zstd""": zstd_file, """bz2""": bza_file, """lz4""": lza_file}
		lowerCAmelCase     =    input_paths[compression_fs_class.protocol]
		if input_path is None:
				lowerCAmelCase     =    f"for '{compression_fs_class.protocol}' compression protocol, "
				if compression_fs_class.protocol == "lz4":
						reason += require_lza.kwargs["reason"]
				elif compression_fs_class.protocol == "zstd":
						reason += require_zstandard.kwargs["reason"]
				pytest.skip(_A					)
		lowerCAmelCase     =    fsspec.filesystem(compression_fs_class.protocol ,  fo=_A					)
		assert isinstance(_A ,  _A					)
		lowerCAmelCase     =    os.path.basename(_A					)
		lowerCAmelCase     =    expected_filename[: expected_filename.rindex("""."""					)]
		assert fs.glob("""*"""					) == [expected_filename]
		with fs.open(_A ,  """r""" ,  encoding="""utf-8"""					) as f, open(_A ,  encoding="""utf-8"""					) as expected_file:
				assert f.read() == expected_file.read()






@pytest.mark.parametrize("""protocol""" ,  ["""zip""", """gzip"""]					)
def 							snake_case__				(     _A:   Tuple ,  _A:   Any ,  _A:   int					)       ->      Tuple:

		'''simple docstring'''


		lowerCAmelCase     =    {"""zip""": zip_jsonl_path, """gzip""": jsonl_gz_path}
		lowerCAmelCase     =    compressed_file_paths[protocol]
		lowerCAmelCase     =    """dataset.jsonl"""
		lowerCAmelCase     =    f"{protocol}://{member_file_path}::{compressed_file_path}"
		lowerCAmelCase   ,	*lowerCAmelCase     =    fsspec.get_fs_token_paths(_A					)
		assert fs.isfile(_A					)
		assert not fs.isfile("""non_existing_""" + member_file_path					)






@pytest.mark.integration
def 							snake_case__				(     _A:   Optional[Any] ,  _A:   Union[str, Any] ,  _A:   Optional[int] ,  _A:   List[Any]					)       ->      Optional[Any]:

		'''simple docstring'''


		lowerCAmelCase     =    hf_api.dataset_info(_A ,  token=_A					)
		lowerCAmelCase     =    HfFileSystem(repo_info=_A ,  token=_A					)
		assert sorted(hffs.glob("""*"""					)					) == [".gitattributes", "data"]
		assert hffs.isdir("""data"""					)
		assert hffs.isfile(""".gitattributes"""					) and hffs.isfile("""data/text_data.txt"""					)
		with open(_A					) as f:
				assert hffs.open("""data/text_data.txt""" ,  """r"""					).read() == f.read()






def 							snake_case__				(     )       ->      List[Any]:

		'''simple docstring'''


		lowerCAmelCase     =    """bz2"""

		# Import module
		import datasets.filesystems

		# Overwrite protocol and reload
		register_implementation(_A ,  _A ,  clobber=_A					)
		with pytest.warns(_A					) as warning_info:
				importlib.reload(datasets.filesystems					)

		assert len(_A					) == 1
		assert (
		    str(warning_info[0].message					)
		    == f"A filesystem protocol was already set for {protocol} and will be overwritten."
		)





'''simple docstring'''




from functools import lru_cache






@lru_cache
def 							snake_case__				(     _A:   int					)       ->      int:

		'''simple docstring'''


		if num < 0:
				raise ValueError("""Number should not be negative."""					)

		return 1 if num in (0, 1) else num * factorial(num - 1					)


if __name__ == "__main__":
						import doctest

						doctest.testmod()




