"""simple docstring"""






import importlib
import inspect
from functools import wraps
from typing import TYPE_CHECKING, Optional, Union

from .download.streaming_download_manager import (
    xbasename,
    xdirname,
    xet_parse,
    xexists,
    xgetsize,
    xglob,
    xgzip_open,
    xisdir,
    xisfile,
    xjoin,
    xlistdir,
    xnumpy_load,
    xopen,
    xpandas_read_csv,
    xpandas_read_excel,
    xPath,
    xrelpath,
    xsio_loadmat,
    xsplit,
    xsplitext,
    xwalk,
    xxml_dom_minidom_parse,
)
from .utils.logging import get_logger
from .utils.patching import patch_submodule
from .utils.py_utils import get_imports


lowerCAmelCase__    :    int			=      get_logger(__name__)


if TYPE_CHECKING:
						from .builder import DatasetBuilder



def     a_		(   lowerCamelCase   ,     lowerCamelCase = None			):
				UpperCAmelCase__			 =				importlib.import_module(lowerCamelCase			)

				# TODO(QL): always update the module to add subsequent new authentication
				if hasattr(lowerCamelCase   ,     '_patched_for_streaming'			) and module._patched_for_streaming:
								return

				def wrap_auth(lowerCamelCase			):
								@wraps(lowerCamelCase			)
								def wrapper(*lowerCamelCase   ,     **lowerCamelCase			):
												return function(*lowerCamelCase   ,     use_auth_token=lowerCamelCase   ,     **lowerCamelCase			)

								UpperCAmelCase__			 =				'wrap_auth'
								return wrapper

				# open files in a streaming fashion
				patch_submodule(lowerCamelCase   ,     'open'   ,     wrap_auth(lowerCamelCase			)			).start()
				patch_submodule(lowerCamelCase   ,     'os.listdir'   ,     wrap_auth(lowerCamelCase			)			).start()
				patch_submodule(lowerCamelCase   ,     'os.walk'   ,     wrap_auth(lowerCamelCase			)			).start()
				patch_submodule(lowerCamelCase   ,     'glob.glob'   ,     wrap_auth(lowerCamelCase			)			).start()
				# allow to navigate in remote zip files
				patch_submodule(lowerCamelCase   ,     'os.path.join'   ,     lowerCamelCase			).start()
				patch_submodule(lowerCamelCase   ,     'os.path.dirname'   ,     lowerCamelCase			).start()
				patch_submodule(lowerCamelCase   ,     'os.path.basename'   ,     lowerCamelCase			).start()
				patch_submodule(lowerCamelCase   ,     'os.path.relpath'   ,     lowerCamelCase			).start()
				patch_submodule(lowerCamelCase   ,     'os.path.split'   ,     lowerCamelCase			).start()
				patch_submodule(lowerCamelCase   ,     'os.path.splitext'   ,     lowerCamelCase			).start()
				# allow checks on paths
				patch_submodule(lowerCamelCase   ,     'os.path.exists'   ,     wrap_auth(lowerCamelCase			)			).start()
				patch_submodule(lowerCamelCase   ,     'os.path.isdir'   ,     wrap_auth(lowerCamelCase			)			).start()
				patch_submodule(lowerCamelCase   ,     'os.path.isfile'   ,     wrap_auth(lowerCamelCase			)			).start()
				patch_submodule(lowerCamelCase   ,     'os.path.getsize'   ,     wrap_auth(lowerCamelCase			)			).start()
				patch_submodule(lowerCamelCase   ,     'pathlib.Path'   ,     lowerCamelCase			).start()
				# file readers
				patch_submodule(lowerCamelCase   ,     'gzip.open'   ,     wrap_auth(lowerCamelCase			)			).start()
				patch_submodule(lowerCamelCase   ,     'numpy.load'   ,     wrap_auth(lowerCamelCase			)			).start()
				patch_submodule(lowerCamelCase   ,     'pandas.read_csv'   ,     wrap_auth(lowerCamelCase			)   ,     attrs=['__version__']			).start()
				patch_submodule(lowerCamelCase   ,     'pandas.read_excel'   ,     wrap_auth(lowerCamelCase			)   ,     attrs=['__version__']			).start()
				patch_submodule(lowerCamelCase   ,     'scipy.io.loadmat'   ,     wrap_auth(lowerCamelCase			)   ,     attrs=['__version__']			).start()
				patch_submodule(lowerCamelCase   ,     'xml.etree.ElementTree.parse'   ,     wrap_auth(lowerCamelCase			)			).start()
				patch_submodule(lowerCamelCase   ,     'xml.dom.minidom.parse'   ,     wrap_auth(lowerCamelCase			)			).start()
				UpperCAmelCase__			 =				True







def     a_		(   lowerCamelCase			):
				extend_module_for_streaming(builder.__module__   ,     use_auth_token=builder.use_auth_token			)
				# if needed, we also have to extend additional internal imports (like wmt14 -> wmt_utils)
				if not builder.__module__.startswith('datasets.'			):  # check that it's not a packaged builder like csv
								for imports in get_imports(inspect.getfile(builder.__class__			)			):
												if imports[0] == "internal":
																UpperCAmelCase__			 =				imports[1]
																UpperCAmelCase__			 =				'.'.join(builder.__module__.split('.'			)[:-1] + [internal_import_name]			)
																extend_module_for_streaming(lowerCamelCase   ,     use_auth_token=builder.use_auth_token			)

    # builders can inherit from other builders that might use streaming functionality
    # (for example, ImageFolder and AudioFolder inherit from FolderBuilder which implements examples generation)
    # but these parents builders are not patched automatically as they are not instantiated, so we patch them here
				from .builder import DatasetBuilder

				UpperCAmelCase__			 =				[
				    cls.__module__
				    for cls in type(lowerCamelCase			).__mro__[1:]  # make sure it's not the same module we've already patched
				    if issubclass(cls   ,     lowerCamelCase			) and cls.__module__ != DatasetBuilder.__module__
				]  # check it's not a standard builder from datasets.builder
				for module in parent_builder_modules:
								extend_module_for_streaming(lowerCamelCase   ,     use_auth_token=builder.use_auth_token			)




"""simple docstring"""






import os
import unittest

import pytest
from parameterized import parameterized

from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device


# skipping in unittest tests

lowerCAmelCase__    :    Union[str, Any]			=      [(1,)]



def     a_		(   ):
				UpperCAmelCase__			 =				bool(os.getenv('RUN_SLOW'   ,     0			)			)
				if run_slow:
								assert True
				else:
								assert False, "should have been skipped"



def     a_		(   ):
				UpperCAmelCase__			 =				bool(os.getenv('RUN_SLOW'   ,     0			)			)
				if run_slow and torch_device == "cuda":
								assert True
				else:
								assert False, "should have been skipped"
@require_torch
class 							snake_case			(					unittest.TestCase							):




					"""simple docstring"""

					@slow
					@require_torch_gpu
					def 						__lowerCAmelCase		(							self  :      Optional[Any]     ):
									check_slow_torch_cuda()







					@require_torch_gpu
					@slow
					def 						__lowerCAmelCase		(							self  :      Any     ):
									check_slow_torch_cuda()







					@parameterized.expand(lowerCamelCase__     )
					@slow
					def 						__lowerCAmelCase		(							self  :      Union[str, Any]       ,lowerCamelCase__  :      str=None     ):
									check_slow()






@slow
@require_torch_gpu
def     a_		(   ):
				check_slow_torch_cuda()



@require_torch_gpu
@slow
def     a_		(   ):
				check_slow_torch_cuda()



@slow
@pytest.mark.parametrize('param'   ,     [1]			)
def     a_		(   lowerCamelCase			):
				check_slow()







@pytest.mark.parametrize('param'   ,     [1]			)
@slow
def     a_		(   lowerCamelCase			):
				check_slow()



